Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
For an assignment in my class the other week, I turned in a piece of code that looked similar to the code below.
When my professor graded it, he told me the following things:
Quote:
You can't put an input.nextLine() method in a switch statement. Instead, you have to create a char variable first and store the nextLine() output into that variable. Then you can check the variable for the conditions you indicate in your case statements. You can't put strings in a switch statement.
I can't understand, however, what's wrong with it. The program functions correctly and the Java Documentation seems to say that the switch statement supports strings.
Can someone please tell me what I'm missing?

Thanks!
Java
Scanner input = new Scanner(System.in);
   switch(input.nextLine())
   {
      case "test 1":
         System.out.println("right on");
         break;
      case "test 2":
         System.out.println("try again");
         break;
      case "test":
         System.out.println("and?");
         break;
      default:
         break;
    }
   }
Posted
Updated 10-Nov-14 16:01pm
v2
Comments
PIEBALDconsult 10-Nov-14 22:19pm    
It's OK, QED.
May not be the best technique, but we don't see the bigger picture. I prefer not to make switches on strings at all, there's usually a better way.
Casey Sheridan 10-Nov-14 22:48pm    
The context is that we were validating user input, and, instead of using a series of "if's", I used a single switch.
PIEBALDconsult 10-Nov-14 23:00pm    
Sure, but then wouldn't you also like to be case-insensitive?
If there is a small number of valid options, I prefer to parse into an enumeration.
(I don't do Java.)
Casey Sheridan 10-Nov-14 23:52pm    
You make a good point.
In the actual assignment, the possible inputs were simple enough to case-sensitivity irrelevant but I would normally put them in.

Since the code I submitted for the assignment actually ran correctly, though, I'm really trying to figure out what was not acceptable about it--like did I break some crucial rule of Java or something.

(BTW, I don't do Java either. Only for this required class)
PIEBALDconsult 11-Nov-14 9:12am    
The only real issue with your technique is an inability to inspect the input value with the debugger before entering the switch.

The problem is how some compilers and interpreters generate the executable here with the Nextline() as a procedure call. If we express this to a series of IF statements then it is clearer what can go wrong.

C#
if input.newline() = "test 1":
   System.out.println("right on");
   break;
if input.newline() =  "test 2":
   System.out.println("try again");
   break;
if input.newline() = "test":
   System.out.println("and?");
   break;


At this point we can see that in the worst case the input.newline() will be executed 3 times each time obtaining a new value. input.newline() is a destructive method which returns a different segment of a stream each time it is called.

I cannot say how this is implemented in Java, but as a matter of form it is better to assign the result of newline to a temp variable and validate the input against that. But as the previous poster stated switches on strings this way is not great form as there is commonly a better way.
 
Share this answer
 
Comments
Casey Sheridan 10-Nov-14 23:44pm    
I certainly agree about there being a better way to do validation. I also agree that assigning the string to a variable would have probably been a better deal.
However, my biggest question is why what I did was wrong (not just "not the best").
Malcolm Chambers 11-Nov-14 0:21am    
Did you create tests to verify that the code works as you expect, I know that several languages are libel to evaluate the newline method each time the case statement is evaluated. So just because it produce the correct response for "test 1" does not mean that it works for the others.

You need to execute each case to verify that it produces the correct value.

But I guess it depends on the marking scheme. I would likely fail the code if I was marking it, because it is such poor practice.

Also not Java programmer.
Casey Sheridan 11-Nov-14 0:59am    
I have run every possible option and it seems to work as desired every time.
Turns out that the Professor tested the program on a machine with less than Java 7 on it. That was why it failed so dramatically.

Thanks to everyone who made suggestions!
 
Share this answer
 
Comments
Richard MacCutchan 15-Nov-14 4:05am    
How on earth did he get to be a professor, while you are only a student?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900