• Post Reply Bookmark Topic Watch Topic
  • New Topic

Switch Statement  RSS feed

 
Daniel Martos
Ranch Hand
Posts: 126
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this program



I need to make this



a switch statement that works.
These are my errors:
Martos5.java:112: error: orphaned case
            case ("P"):
            ^
Martos5.java:113: error: ')' expected
               System.out.printf("\nTuition calculation for %s\n",name "a PA resident");
                                                                      ^
Martos5.java:113: error: illegal start of expression
               System.out.printf("\nTuition calculation for %s\n",name "a PA resident");
                                                                                      ^
Martos5.java:116: error: ')' expected
               System.out.printf("\nTuition calculation for %s\n",name "a US, non-PA resident");
                                                                      ^
Martos5.java:116: error: illegal start of expression
               System.out.printf("\nTuition calculation for %s\n",name "a US, non-PA resident");
                                                                                              ^
Martos5.java:119: error: ')' expected
               System.out.printf("\nTuition calculation for %s\n",name "an international student");
                                                                      ^
Martos5.java:119: error: illegal start of expression
               System.out.printf("\nTuition calculation for %s\n",name "an international student");
                                                                                                 ^
7 errors

----jGRASP wedge: exit code for process is 1.
----jGRASP: operation complete.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

There is no "switch" statement, in your switch example code. All you have is a bunch of case statements, with no switch statement.

Henry
 
Daniel Martos
Ranch Hand
Posts: 126
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
There is no "switch" statement, in your switch example code. All you have is a bunch of case statements, with no switch statement.

Henry




Yes, I forgot that, lol.  It still didn't work

Martos5.java:116: error: ')' expected
               System.out.printf("\nTuition calculation for %s\n",name "a PA resident");
                                                                      ^
Martos5.java:116: error: illegal start of expression
               System.out.printf("\nTuition calculation for %s\n",name "a PA resident");
                                                                                      ^
Martos5.java:119: error: ')' expected
               System.out.printf("\nTuition calculation for %s\n",name "a US, non-PA resident");
                                                                      ^
Martos5.java:119: error: illegal start of expression
               System.out.printf("\nTuition calculation for %s\n",name "a US, non-PA resident");
                                                                                              ^
Martos5.java:122: error: ')' expected
               System.out.printf("\nTuition calculation for %s\n",name "an international student");
                                                                      ^
Martos5.java:122: error: illegal start of expression
               System.out.printf("\nTuition calculation for %s\n",name "an international student");
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The error has nothing to do with switch or case:

Daniel Martos wrote:
Martos5.java:116: error: ')' expected
               System.out.printf("\nTuition calculation for %s\n",name "a PA resident");


The Java compiler is complaining that the method call is not syntactically correct. If you look at the second parameter, it becomes pretty clear why.

Henry
 
Daniel Martos
Ranch Hand
Posts: 126
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
The error has nothing to do with switch or case:

Daniel Martos wrote:
Martos5.java:116: error: ')' expected
               System.out.printf("\nTuition calculation for %s\n",name "a PA resident");


The Java compiler is complaining that the method call is not syntactically correct. If you look at the second parameter, it becomes pretty clear why.

Henry


int numCredits?
 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's put a little space around the parameters in this statement:

Do you see the problem with the syntax of the statement?
 
Daniel Martos
Ranch Hand
Posts: 126
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:Let's put a little space around the parameters in this statement:

Do you see the problem with the syntax of the statement?


Do I need a , after name?  due in 35 minutes......my mind is blowing!!  Thanks Knute you are very helpful
 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
printf takes a format string and then only the number of parameters that you have "%" tokens for, so the trailing String after name won't work.  Try concatenating (using "+") a whole string together without "%" tokens and using println.
 
Daniel Martos
Ranch Hand
Posts: 126
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:printf takes a format string and then only the number of parameters that you have "%" tokens for, so the trailing String after name won't work.  Try concatenating (using "+") a whole string together without "%" tokens and using println.



???

then I have issue with this:


        
 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't need the commas.

"Anything " + variable + " something else"
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why has the % disappeared before s? Only use \n if somebody has said they need an LF character. Use %n for the correct line end sequence.
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:You don't need the commas.

"Anything " + variable + " something else"
I would have added a second %s and used the commas.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Regardless, a discussion about whether there should be a plus or a comma, whether a % is missing (and why the OP removed it), whether there should be more than one % (and a comma instead), etc. ... is, in my opinion, no longer useful to the OP. It is clear that this is no longer about learning, and more about making the deadline.

Shouldn't the goal be learning? Hence, shouldn't the OP be directed to understanding method parameters or string concatenation? directed to understanding the formatting string of the printf() method? etc.?

Henry
 
Dave Tolls
Ranch Foreman
Posts: 3068
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Shouldn't the goal be learning? Hence, shouldn't the OP be directed to understanding method parameters or string concatenation? directed to understanding the formatting string of the printf() method? etc.?

Henry


That would be my opinion.
Which would lead to pointing out that printf is intended to have a String with a bunch of placeholders for the things that are variable.
There's no point having a placeholder for something that is never going to change.

So in this case the String would be:
"\nTuition calculation for %s a PA resident\n"
with the single parameter for name.

ETA: Though the newline stuff probably ought to be system based rather than a straight "\n".
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!