View Single Post
  #1 (permalink)  
Old 07-13-2007, 10:47 PM
susan susan is offline
Member
 
Join Date: Jul 2007
Posts: 44
susan is on a distinguished road
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:

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 + "\""); } } }
When i try and compile it I get this message

Code:
Prob01BAC.java:20: incompatible types Found : char Required : java.lang.String LastChar = Input.charAt(LastIndex);
the first ( has a marker under it).

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.
Reply With Quote
Sponsored Links