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
  • 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how do i compare an object based on two fields 6 54
Java Timer (static) 9 43
jboss wildfly 10.1 10 92
ejb on wildfly 5 20
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

777 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