• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a sappy valentines day java program (sorry for that). any help is appreciated.
here is the code
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)

 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic