Results 1 to 2 of 2
  1. #1
    susan is offline Member
    Join Date
    Jul 2007
    Posts
    44
    Rep Power
    0

    Default 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:

    Java 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

    Java 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.

  2. #2
    cachi is offline Member
    Join Date
    Jul 2007
    Posts
    40
    Rep Power
    0

    Default

    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:

    Java Code:
    LastChar = Input.charAt(LastIndex);
    As the String.charAt method returns a char, the things go wrong.

    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:
    Java Code:
     char LastChar;
    Furthermore you need to change the lines:
    Java Code:
    if ( LastChar == "?" ){
    and
    Java Code:
    if ( LastChar == "?" ){
    To:
    Java Code:
    if ( LastChar == '?' ){
    and
    Java Code:
    if ( LastChar == '?' ){
    Pay attention to the single quotes. If you say:

    Java Code:
    (LastChar == "?")
    The java compiler will interpret "?" as a String with only one character and not as a char.
    Greetings.

Similar Threads

  1. How to get input from Console
    By karma in forum New To Java
    Replies: 8
    Last Post: 08-13-2010, 10:32 PM
  2. cant take input from user
    By new_1 in forum New To Java
    Replies: 6
    Last Post: 12-25-2007, 08:38 AM
  3. input placed in array
    By smilejava in forum New To Java
    Replies: 5
    Last Post: 11-12-2007, 08:29 AM
  4. input placed in array
    By smilejava in forum New To Java
    Replies: 1
    Last Post: 11-05-2007, 01:32 PM
  5. how to take input and verify input in Java programs
    By bilal_ali_java in forum Advanced Java
    Replies: 0
    Last Post: 07-21-2007, 09:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •