Page 1 of 2 12 LastLast
Results 1 to 20 of 21
  1. #1
    AcousticBruce is offline Senior Member
    Join Date
    Dec 2010
    Location
    Indiana
    Posts
    202
    Rep Power
    4

    Default I do not understand this error with charAt()

    Java Code:
    int userLength;
    String lastChar;
    String userSentence = "Put a period at the end for me";
    
            
            userLength = userSentence.length();
           [COLOR="Red"] lastChar = userSentence.charAt(userLength-1);[/COLOR]
            
             if (!(lastChar.equals(".")))  {
                userSentence = userSentence + ".";
            }
            
    }
    ERROR MSG
    incompatible types
    found : char
    requied : java.lang.String


    If there is not a period then add one. Thats all I am trying to do.
    Can someone help me understand my error and possibly even help me see a better way to do this.
    Last edited by AcousticBruce; 12-08-2010 at 02:20 PM.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Does that compile?

    If it does not and you can't understand the message, post the code and the compiler message.

  3. #3
    AcousticBruce is offline Senior Member
    Join Date
    Dec 2010
    Location
    Indiana
    Posts
    202
    Rep Power
    4

    Default

    Java Code:
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.io.IOException;
    
    public class SentenceCheck {
        public static void main(String[] args) {
    
            //declare vars
            String userSentence = "";
            String userFix;
            String lastChar = "";
            int userLength;
            BufferedReader reader;
    
            //user types a sentence without punctuation and random caps.
            reader = new BufferedReader(new InputStreamReader(System.in));
            try {
                System.out.println("Type a sentence with no caps or punctuation.");
                userSentence = reader.readLine();
            }
            catch (IOException ioe) {
                System.out.println("IOE");
            }
    
            userLength = userSentence.length();
            lastChar = userSentence.charAt(userLength-1);
            if (!(lastChar.equals(".")))  {
                userSentence = userSentence + ".";
            }
            
        }
    }

    It does except for that spot.
    Last edited by AcousticBruce; 12-08-2010 at 02:33 PM.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    When I try and compile this I see:

    Java Code:
    SentenceCheck.java:26: ';' expected
            lastChar = userSentence.charAt(userLength-1));
                                                        ^

    The compiler is expecting a ; because it thinks the statement should end at that place. Check your parentheses, they should balance: one ) for every (.

  5. #5
    AcousticBruce is offline Senior Member
    Join Date
    Dec 2010
    Location
    Indiana
    Posts
    202
    Rep Power
    4

    Default

    Yes that is an error also (must have made a type before i copied)

    the error im talking about says

    incompatible types
    found : char
    requied : java.lang.String

  6. #6
    jomypgeorge is offline Member
    Join Date
    May 2010
    Posts
    90
    Rep Power
    0

    Default

    lastChar = userSentence.charAt(userLength-1);

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Quote Originally Posted by AcousticBruce View Post
    Yes that is an error also (must have made a type before i copied)

    the error im talking about says

    incompatible types
    found : char
    requied : java.lang.String


    Sorry - didn't see this post...

    Here the compiler is grumbling because it required a string (you declared lastChar as a string), but it found a char. Check the API docs for charAt(): if it returns a char you must declare and use what it returns as a char.

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

    And, of course, you can compare chars more easily with

    Java Code:
    foo=='Q'

    rather than having to use an equals() method.

  8. #8
    gayankan is offline Member
    Join Date
    Dec 2010
    Posts
    2
    Rep Power
    0

    Smile

    lastChar = userSentence.charAt(userLength-1));
    as your code like this
    You must ensure that the all of open brackets are closed.
    lastChar = userSentence.charAt(userLength-1);

    then if you print the "lastChar" then it should print the last character of the string.

  9. #9
    AcousticBruce is offline Senior Member
    Join Date
    Dec 2010
    Location
    Indiana
    Posts
    202
    Rep Power
    4

    Default

    Thanks everyone.

    So far the closed bracket issue is not what im talking about. I made that error before I copied the code. It is fixed but I still get the char / string error.


    My problem is that I thought a char was a string.
    pbrockway2 I looked at that page, I couldn't find the difference between char and string.


    So if I have a string
    String nameLetter;
    String name = "abcd";
    nameLetter = name.charAt(0)
    This show up with an error... isnt charAt a string function? it returns "a" how is this not a string?
    What should I be using instead?


    And pbrockway2
    if (!(lastChar.equals(".")))
    I meant does NOT equal. Is there a better way?

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,015
    Rep Power
    20

    Default

    charAt is a String method that returns a char.
    A char is not a String.

    ETA: If you just want to know what the last character is to check it isn't already a full stop then make lastChar a char (and not a String) and use != '.'

  11. #11
    AcousticBruce is offline Senior Member
    Join Date
    Dec 2010
    Location
    Indiana
    Posts
    202
    Rep Power
    4

    Default

    Quote Originally Posted by Tolls View Post
    charAt is a String method that returns a char.
    A char is not a String.
    I realize this now, so how can I make it show the last character of a string as a string?

    ETA: If you just want to know what the last character is to check it isn't already a full stop then make lastChar a char (and not a String) and use != '.'
    (lastChar != ".") was my issue. I tried the '.' instead and it worked.


    So I have the code working now, but learned some new things. Now I would like to read about char vs String and the difference.

    So I will do my best to search out the difference. If you have any good pages that can help also. Thanks folks.

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,015
    Rep Power
    20

  13. #13
    off99555 is offline Member
    Join Date
    Dec 2010
    Posts
    11
    Rep Power
    0

    Talking

    Quote Originally Posted by AcousticBruce View Post
    I realize this now, so how can I make it show the last character of a string as a string?



    (lastChar != ".") was my issue. I tried the '.' instead and it worked.


    So I have the code working now, but learned some new things. Now I would like to read about char vs String and the difference.

    So I will do my best to search out the difference. If you have any good pages that can help also. Thanks folks.
    The best way to explain the difference between char and String is that ...
    char is a character. It is a primitive data type, so when you compare it with another one you use the operator to make a condition. (==, !=, >, <, >=, <=)
    String is a word, sentence, message or passage. And yes it is a reference data type, so when you compare it with another one you use the .equals() method that returns boolean to make a condition.
    So char is just a part of a String!
    char data you use single quote e.g. 'c'
    String data you use double quote e.g. "Sentences"
    That's it!
    Last edited by off99555; 12-08-2010 at 05:41 PM.

  14. #14
    AcousticBruce is offline Senior Member
    Join Date
    Dec 2010
    Location
    Indiana
    Posts
    202
    Rep Power
    4

    Default

    Thanks off99555. It makes sense that it is primitive ('h' + 'h') == 208 :)



    Java Code:
    ........................
    
         //user types a sentence without punctuation and random caps.
            reader = new BufferedReader(new InputStreamReader(System.in));
            try {
                System.out.println("Type a sentence with no caps or punctuation.");
                userSentence = reader.readLine();
            }
            catch (IOException ioe) {
                System.out.println("IOE");
            }
    
            userLength = userSentence.length();
            lastChar = userSentence.charAt(userLength-1);
            if (lastChar != '.')  {
                userSentence = userSentence + ".";
            }
            System.out.println(userSentence);
        }
    }

    This works and adds a period at the end.
    1) So should I be using charAt() here?
    2) So is there a stringAt() type function that I am missing?
    3) Can someone help me find some BASIC programs that manipulate sentences?

    I am looking for some projects that do sentence manipulation. I need more then just reference to learn this stuff.
    Last edited by AcousticBruce; 12-08-2010 at 07:14 PM.

  15. #15
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    1) So should I be using charAt() here?

    I think so. Your program depends on what the last character of the string is, so it is most straightfoward with a type meant to represent a character.


    2) So is there a stringAt() type function that I am missing?

    Sort of. There is a substring() method that can be made to do this: you ask for a substring that is just one character long. The details are in the API docs I linked to before.

    -----

    As well as the section on primtives in Oracle's Tutorial that was linked to before, there is one on Strings that gives examples and discusses them.

    Beyond that the range of problems that involve manipulating strings is huge - from machine translation to page scaping to teh pig latin and palindromes. Depending on the size and nature of the problems you are looking for, just reading the posts here will supply quite a number.

  16. #16
    off99555 is offline Member
    Join Date
    Dec 2010
    Posts
    11
    Rep Power
    0

    Default

    Quote Originally Posted by AcousticBruce View Post
    Thanks off99555. It makes sense that it is primitive ('h' + 'h') == 208 :)



    Java Code:
    ........................
    
         //user types a sentence without punctuation and random caps.
            reader = new BufferedReader(new InputStreamReader(System.in));
            try {
                System.out.println("Type a sentence with no caps or punctuation.");
                userSentence = reader.readLine();
            }
            catch (IOException ioe) {
                System.out.println("IOE");
            }
    
            userLength = userSentence.length();
            lastChar = userSentence.charAt(userLength-1);
            if (lastChar != '.')  {
                userSentence = userSentence + ".";
            }
            System.out.println(userSentence);
        }
    }

    This works and adds a period at the end.
    1) So should I be using charAt() here?
    2) So is there a stringAt() type function that I am missing?
    3) Can someone help me find some BASIC programs that manipulate sentences?

    I am looking for some projects that do sentence manipulation. I need more then just reference to learn this stuff.
    I have some suggestion for you
    1. Instead of use userSentence = userSentence + '.' in your code.
    You just use userSentence += "."
    2. There is substring() method of String if you want to extract some string in your String. Example, String sub = "Hello World".substring(0, 5) This will returns "Hello" String or sub.substring(3) should returns "lo" if I'm not wrong.
    3.If you use operator +, -, * or / with the char variable it will cast itself to an integer (int type). And yes you can cast an int to a char. Such as char c = (char) 20;
    So you can make a program that list all the character in all languages (I have ever made using for-loop. It has around 60,000 characters).

    If you want to practice your logic with String. I think I have an exam that it should let you think a lot for the beginner but it doesn't harder than your intelligence.
    Make a program that receive a string value, count each character and show the result.
    Example, Input: ABCa AAaastringrrgz
    Output:
    A,a,r 3 chars
    B,C,s,t,i,n,z 1 char
    g 2 chars
    I'm a teenage Thai Java programmer who studying in secondary school.
    Practicing English in process ...

  17. #17
    SmilingKey is offline Member
    Join Date
    Dec 2010
    Posts
    19
    Rep Power
    0

    Default

    Quote Originally Posted by off99555 View Post
    2. There is substring() method of String if you want to extract some string in your String. Example, String sub = "Hello World".substring(0, 5) This will returns "Hello" String or sub.substring(3) should returns "lo" if I'm not wrong.
    Not exactly, I tried it and I get "lo World".

    Good explanation.

  18. #18
    AcousticBruce is offline Senior Member
    Join Date
    Dec 2010
    Location
    Indiana
    Posts
    202
    Rep Power
    4

    Default

    I really feels good to see my old post. I have improved immensely because of the books and this forum and I do not see my learning stopping any time soon.


    Quote Originally Posted by off99555 View Post
    Make a program that receive a string value, count each character and show the result.
    Example, Input: ABCa AAaastringrrgz
    Output:
    A,a,r 3 chars
    B,C,s,t,i,n,z 1 char
    g 2 chars

    Awesome! I really like that you are giving me test. However, I am having trouble understanding what you are asking me to make.

    So the program is:
    (input string)
    ------------
    (pull out chars used once)
    (pull out chars used twice)
    (pull out chars used three times)
    Last edited by AcousticBruce; 12-21-2010 at 07:49 PM.

  19. #19
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,015
    Rep Power
    20

    Default

    Quote Originally Posted by SmilingKey View Post
    Not exactly, I tried it and I get "lo World".

    Good explanation.
    No, off99555 is correct. He's substringing the sub, not "Hello World".
    Java Code:
    String sub = "Hello World".substring(0,5);
    System.out.println(sub.substring(3));
    produces "lo".

  20. #20
    SmilingKey is offline Member
    Join Date
    Dec 2010
    Posts
    19
    Rep Power
    0

Page 1 of 2 12 LastLast

Similar Threads

  1. HELP ...DONT UNDERSTAND Error
    By ask4soteria in forum New To Java
    Replies: 6
    Last Post: 11-26-2010, 09:43 AM
  2. Space for charAt()?
    By Tussmann in forum New To Java
    Replies: 5
    Last Post: 11-02-2010, 06:57 PM
  3. I don't understand the error ... (help!)
    By XmisterIS in forum New To Java
    Replies: 3
    Last Post: 09-02-2010, 09:33 AM
  4. Help with charAt()
    By HackerOfDoom in forum New To Java
    Replies: 7
    Last Post: 03-21-2010, 05:27 PM
  5. i don understand this error
    By Deon in forum New To Java
    Replies: 4
    Last Post: 01-12-2008, 10:03 AM

Tags for this Thread

Posting Permissions

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