Results 1 to 20 of 21
- 12-08-2010, 06:11 AM #1
Senior Member
- Join Date
- Dec 2010
- Location
- Indiana
- Posts
- 202
- Rep Power
- 3
I do not understand this error with charAt()
ERROR MSGJava 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 + "."; } }
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.
- 12-08-2010, 06:27 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Does that compile?
If it does not and you can't understand the message, post the code and the compiler message.
- 12-08-2010, 06:29 AM #3
Senior Member
- Join Date
- Dec 2010
- Location
- Indiana
- Posts
- 202
- Rep Power
- 3
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.
- 12-08-2010, 06:36 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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 (.
- 12-08-2010, 06:45 AM #5
Senior Member
- Join Date
- Dec 2010
- Location
- Indiana
- Posts
- 202
- Rep Power
- 3
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
- 12-08-2010, 07:16 AM #6
Member
- Join Date
- May 2010
- Posts
- 90
- Rep Power
- 0
lastChar = userSentence.charAt(userLength-1);
- 12-08-2010, 07:22 AM #7
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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.
- 12-08-2010, 09:10 AM #8
Member
- Join Date
- Dec 2010
- Posts
- 2
- Rep Power
- 0
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.
- 12-08-2010, 02:33 PM #9
Senior Member
- Join Date
- Dec 2010
- Location
- Indiana
- Posts
- 202
- Rep Power
- 3
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
This show up with an error... isnt charAt a string function? it returns "a" how is this not a string?String nameLetter;
String name = "abcd";
nameLetter = name.charAt(0)
What should I be using instead?
And pbrockway2
I meant does NOT equal. Is there a better way?if (!(lastChar.equals(".")))
- 12-08-2010, 02:49 PM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 10,480
- Rep Power
- 16
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 != '.'
- 12-08-2010, 02:59 PM #11
Senior Member
- Join Date
- Dec 2010
- Location
- Indiana
- Posts
- 202
- Rep Power
- 3
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.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 != '.'
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-08-2010, 03:16 PM #12
Moderator
- Join Date
- Apr 2009
- Posts
- 10,480
- Rep Power
- 16
- 12-08-2010, 05:35 PM #13
Member
- Join Date
- Dec 2010
- Posts
- 11
- Rep Power
- 0
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.
- 12-08-2010, 07:11 PM #14
Senior Member
- Join Date
- Dec 2010
- Location
- Indiana
- Posts
- 202
- Rep Power
- 3
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.
- 12-08-2010, 10:25 PM #15
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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.
- 12-21-2010, 02:16 PM #16
Member
- Join Date
- Dec 2010
- Posts
- 11
- Rep Power
- 0
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 charsI'm a teenage Thai Java programmer who studying in secondary school.
Practicing English in process ...
- 12-21-2010, 07:15 PM #17
Member
- Join Date
- Dec 2010
- Posts
- 19
- Rep Power
- 0
- 12-21-2010, 07:45 PM #18
Senior Member
- Join Date
- Dec 2010
- Location
- Indiana
- Posts
- 202
- Rep Power
- 3
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.
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.
- 12-22-2010, 08:37 AM #19
Moderator
- Join Date
- Apr 2009
- Posts
- 10,480
- Rep Power
- 16
- 12-22-2010, 02:09 PM #20
Member
- Join Date
- Dec 2010
- Posts
- 19
- Rep Power
- 0
Similar Threads
-
HELP ...DONT UNDERSTAND Error
By ask4soteria in forum New To JavaReplies: 6Last Post: 11-26-2010, 09:43 AM -
Space for charAt()?
By Tussmann in forum New To JavaReplies: 5Last Post: 11-02-2010, 06:57 PM -
I don't understand the error ... (help!)
By XmisterIS in forum New To JavaReplies: 3Last Post: 09-02-2010, 09:33 AM -
Help with charAt()
By HackerOfDoom in forum New To JavaReplies: 7Last Post: 03-21-2010, 05:27 PM -
i don understand this error
By Deon in forum New To JavaReplies: 4Last Post: 01-12-2008, 10:03 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks