Java Questions

Posted on 2004-09-12
Last Modified: 2010-03-31

I am asked to write a java code to read the first and last character inputted by the user. My questions are,

1. Do I convert from String to Char?
2. Does it involve any use of Arrays?


    char first = textString.charAt(0);

    char last  = textString.charAt(length() -1);


Is the above - part of the code - right?


Question by:biloonline
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
LVL 24

Accepted Solution

sciuriware earned 125 total points
ID: 12039288
1) You already did it in the 1st statement by "charAt()".
2) Your String is already acting as an array.
3) You did not define the method length() so it should be:

          char last = textString.charAt(textString.length() - 1);


Author Comment

ID: 12039303
I am getting 5 errors.

Here is my entire code:


   UW PICO(tm) 4.6               File:


public class Hw1Pr4 {
        public static void main(String[] argv) throws IOException {
                BufferedReader stdin = new BufferedReader (new InputStreamReade$
                char textString;

                System.out.println("Enter a string: ");
                textString = stdin.readLine();
                char first = textString.charAt(0);

                char last  = textString.charAt(textString.length() -1);
                System.out.println("The first character is " + first + " and the last character is " + last);



Here are the errors I am getting:

found   : java.lang.String
required: char
                textString = stdin.readLine();
                                           ^ char cannot be dereferenced
                char first = textString.charAt(0);
                                       ^ char cannot be dereferenced
                char last  = textString.charAt(textString.length() -1);
                                                         ^ char cannot be dereferenced
                char last  = textString.charAt(textString.length() -1);
4 errors


Author Comment

ID: 12039308
Got it fixed.

LVL 24

Expert Comment

ID: 12041957
I've been offline for a while.
I guess you changed
                                 char textString
                                 String textString

Any more problems?


Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
throw exception 21 78
Java string replace 11 77
java example issue 5 66
How to find out if all executorservice threads have finished 2 13
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question