• Post Reply Bookmark Topic Watch Topic
  • New Topic

getting illegal start of expression error while the text is with in quotation marks  RSS feed

 
Richard Scott
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a sappy valentines day java program (sorry for that). any help is appreciated.
here is the code
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37507
552
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Richard,
Welcome to CodeRanch! I've added code tags to make your post easier to read

Boiling your question down into a shorter one, we have the question of why the following doesn't work if you enter the number in quotes.


Input: "1"
Output:
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:909)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextInt(Scanner.java:2160)
at java.util.Scanner.nextInt(Scanner.java:2119)
at jb.PlayTest.main(PlayTest.java:11)


The reason is because Java is looking for only numbers. To deal with this you have three choices:
1) Read a String instead of an integer, strip out the quotes (or other special characters) and call Integer.parseInt()
2) Catch the exception and have the user re-enter the numeric choice.
3) Let the program crash (granted this isn't a good choice)

 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Richard Scott wrote: This is a sappy valentines day java program...

First: welcome to JavaRanch, Richard.

Second: I wonder why you wrote 137 lines of code before you decided to run this. Even experienced programmers know that it's a really good idea to build up programs gradually - write a few lines, compile, run, and check your results.

At the very least, it means that you'll have some idea where the problem is occurring.

Winston
 
Richard Scott
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Richard Scott wrote: This is a sappy valentines day java program...

First: welcome to JavaRanch, Richard.

Second: I wonder why you wrote 137 lines of code before you decided to run this. Even experienced programmers know that it's a really good idea to build up programs gradually - write a few lines, compile, run, and check your results.

At the very least, it means that you'll have some idea where the problem is occurring.

Winston



well to be fair it worked before i added more ascii art to it
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Richard Scott wrote:well to be fair it worked before i added more ascii art to it

OK, so when did you problems start?

TBH, I think that Jeanne's already sorted your basic issue, but you might save yourself similar problems in the future if you compile (and run) your programs A LOT. One of my esteemed colleagues here (Fred Rosenberger) suggests that you do it every 5 lines that you write. I'm not quite as Puritan, but 20 is about my limit; even after 12 years with Java (and 35 in the biz).

Winston
 
Richard Scott
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well when i take out the last two hearts it works correctly. so i am guessing that some charaters even though they are in quotes cause problems?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you would post the actual error you get, it would have saved a lot of time. Without it, we either have to a) guess, b) examine the code line by line, or c) copy it into a program on our machines and compile.

I did the later, and it tells you exactly what the problem is:


when you get a bajillion errors, focus on the FIRST one only. Fix it, then re-compile.

Inside a string, the backslash has special meaning. It means "treat the next character differently". That is how you can put a quote INSIDE a quote. but only certain things can be escaped, and a space is not one of them.

so...how do you put a backslash inside a quote? You escape it with...a backslash. Try this:


You will probably have to do this on a lot of your lines, every time you have a single '\' inside your qoutes.
 
Campbell Ritchie
Marshal
Posts: 56581
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again

Anybody who writes such a soppy program deserves all the compiler errors he gets (‍) as long the girl involved doesn't have compiler errors of her own

Fred and I have had disagreements about how many lines you should write before compiling before, he would say 5 and I said 10. Then Fred confessed that he means lines with actual code written on and I meant lines with line numbers: I would count a blank line as 1 and Fred wouldn't. So there is less disagreement about how many lines you write than it would appear at first sight.

How you write code helps you avoid mistakes. I suggest you have a look at our suggestions about indentation. Also my suggestions about editors, and find the link about writing backwards. Now all those things may appear strange, but the illegal start… error is often caused by too many {s, and consistent indentation enables you to count the { and } very easily.

I hope your teachers are not teaching you to put so much code in the main method; you should divide that into multiple methods. Look here.

And everybody else has told you, you cannot have \ or " characters inside a String literal. You have to write \\ or \". And you need an extra space on three of the lines with the rose on.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!