+ 2

How to get switch to check If input is the same as a string?

Scanner scorer = new Scanner(System.in); while (gamePt[p1] != 40 && gamePt[p2] != 40) { System.out.print("Cine a punctat?"); String score = scorer.nextLine(); switch (score) { case "p1": { System.out.println("A inscris p1"); ++p1; System.out.println("Scorul este " + gamePt[p1] + " la " + gamePt[p2]); } case "p2": { //Some code

1st Feb 2018, 9:09 PM
Lăbonţu Marius-Andrei
Lăbonţu Marius-Andrei - avatar
5 Respuestas
+ 2
Thanks for the answers @Gordie: the cases work fine. Eclipse treats them as good. It's the switch that is causing trouble
1st Feb 2018, 9:34 PM
Lăbonţu Marius-Andrei
Lăbonţu Marius-Andrei - avatar
+ 2
change ur last case to "default:"
1st Feb 2018, 10:47 PM
Jeremy
Jeremy - avatar
+ 1
I have a few days since I started with java, and I don't know what the playground is in eclipse. I will attach the full code. It is for a tennis match. the input should be how scored, in this case with a simple input like p1 or p2. the output of the method should be a number of points added to an int and taken from an array of 0 15 30 40 https://code.sololearn.com/WOzOdCOKQ2aS/?ref=app
1st Feb 2018, 10:43 PM
Lăbonţu Marius-Andrei
Lăbonţu Marius-Andrei - avatar
+ 1
Thanks, I will try the solution later today and let you know how it went
2nd Feb 2018, 9:22 AM
Lăbonţu Marius-Andrei
Lăbonţu Marius-Andrei - avatar
+ 1
Thanks all for the answers. I removed the brackets and changed the last case into default and it worked
5th Feb 2018, 2:57 PM
Lăbonţu Marius-Andrei
Lăbonţu Marius-Andrei - avatar