Results 1 to 2 of 2
- 07-13-2007, 08:47 PM #1
Member
- Join Date
- Jul 2007
- Posts
- 44
- Rep Power
- 0
Help With Input.charAt(LastIndex);
I have got a question about something that I just cant figure out on my own.
With that said, here is the code:
When i try and compile it I get this messageJava Code:import java.util.*; import javax.swing.*; import java.lang.*; public class Prob01BAC { public static void main(String args[]){ String Input, LastChar; int LastIndex; double StringLength; //Gathers Input from user and stores in the 'Input' Variable for later use Input = JOptionPane.showInputDialog("Please enter a sentence"); StringLength = Input.length(); LastIndex = Input.lastIndexOf(Input); LastChar = Input.charAt(LastIndex); //Check to see if the last character of the string is a '?' if ( LastChar == "?" ){ //Checks to see if the string is even or odd by dividing by 2 and checking the remainder //0 = even, anything else is odd if ( StringLength % 2 == 0 ){ JOptionPane.showMessageDialog(null, "Yes"); } else{ JOptionPane.showMessageDialog(null, "No"); } } //Checks to see if the last character of the string is a '!' else if ( LastChar == "!" ){ JOptionPane.showMessageDialog(null, "Wow"); } //This is a sort of catch all for anything that doesnt meet the above requirements else{ JOptionPane.showMessageDialog(null, "You always say \"" + Input + "\""); } } }
the first ( has a marker under it).Java Code:Prob01BAC.java:20: incompatible types Found : char Required : java.lang.String LastChar = Input.charAt(LastIndex);
The object of the program is to take a user input string using the JOptionPane method and then responding with a certain string according to certain variables like the string length being odd or even, the string ending in a ! or a ?, or nothing at all.
I have tried googling the error, but with my limited knowledge I found nothing that appeared to be of any use in the way of resloving the problem.
- 08-07-2007, 04:22 AM #2
Member
- Join Date
- Jul 2007
- Posts
- 40
- Rep Power
- 0
The problem is that you are trying to assign a char type to a String type and this is not allowed since these types are incompatible.
You declared LastChar as a String but in your code you do this:
As the String.charAt method returns a char, the things go wrong.Java Code:LastChar = Input.charAt(LastIndex);
Some info about String and char:
- char is a primitive type and represents a single character: 'a', 'D'. Notice that you always use single quotes (') to represent a char literal.
- String is an object which represents a string of characters: "abcd". Notice that you always use double quotes (") to represent a String literal.
So the solution for your problem is to change the LastChar type to char:
Furthermore you need to change the lines:Java Code:char LastChar;
andJava Code:if ( LastChar == "?" ){
To:Java Code:if ( LastChar == "?" ){
andJava Code:if ( LastChar == '?' ){
Pay attention to the single quotes. If you say:Java Code:if ( LastChar == '?' ){
The java compiler will interpret "?" as a String with only one character and not as a char.Java Code:(LastChar == "?")
Greetings.
Similar Threads
-
How to get input from Console
By karma in forum New To JavaReplies: 8Last Post: 08-13-2010, 09:32 PM -
cant take input from user
By new_1 in forum New To JavaReplies: 6Last Post: 12-25-2007, 07:38 AM -
input placed in array
By smilejava in forum New To JavaReplies: 5Last Post: 11-12-2007, 07:29 AM -
input placed in array
By smilejava in forum New To JavaReplies: 1Last Post: 11-05-2007, 12:32 PM -
how to take input and verify input in Java programs
By bilal_ali_java in forum Advanced JavaReplies: 0Last Post: 07-21-2007, 08:46 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks