Solved

Java Questions

Posted on 2004-09-12
4
232 Views
Last Modified: 2010-03-31
Howdy,

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?



code:--------------------------------------------------------------------------------    

    char first = textString.charAt(0);

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

--------------------------------------------------------------------------------


Is the above - part of the code - right?

Thanks

0
Comment
Question by:biloonline
  • 2
  • 2
4 Comments
 
LVL 24

Accepted Solution

by:
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);

;JOOP!
0
 

Author Comment

by:biloonline
ID: 12039303
I am getting 5 errors.

Here is my entire code:

<code>

   UW PICO(tm) 4.6               File: Hw1Pr4.java

import java.io.*;

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);

                System.exit(0);
        }
}

</code>

Here are the errors I am getting:

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

Thanks
0
 

Author Comment

by:biloonline
ID: 12039308
Got it fixed.

Thanks
0
 
LVL 24

Expert Comment

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

Any more problems?

;JOOP!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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…

828 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