Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Java Questions

Posted on 2004-09-12
Medium Priority
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 500 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

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

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

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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…
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.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

688 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