+ 2

Syntax error on token while printing strings

Hi, I am new at this. Really new :D I am trying to get the input for some names and then print out Player1 vs Player 2, but I'm getting a syntax error. The piece of code is: Scanner playerName = new Scanner(System.in); System.out.print ("Enter name for player 1: "); String player1 = playerName.next(); System.out.print ("Enter name for player 2: "); String player2 = playerName.next(); //test line - to be deleted later on System.out.print(+ player1 "vs." + player2); On the last line I am getting the following error: "Syntax error on token ""vs."", delete this token" I have tried without the pluses as well and it still does not work The initial last line was a different one, but I simplified it to see if it generates the same error. It was: System.out.println("Currently playing: " + player1 " against " + player2 ". Good luck!"); Any help on this would be very appreciated. Thanks!

29th Jan 2018, 8:00 PM
Lăbonţu Marius-Andrei
Lăbonţu Marius-Andrei - avatar
2 odpowiedzi
+ 3
This is the correct syntax: System.out.print(player1 + "vs." + player2); Explanation: You must put + between Strings and variables. This is because the print method only accepts a single object into its parameters. What we are doing is putting everything together to make one big String. Then passing the single String as arguments. This is called String Concatenation.
29th Jan 2018, 8:08 PM
Rrestoring faith
Rrestoring faith - avatar
+ 2
It seems I figured it out. The pluses should have been placed where the text and the strings would "collide" It seems that the following statement resolves the error and displays what I needed System.out.println("Currently playing: " + player1 + " against " + player2 + ". Good luck!");
29th Jan 2018, 8:06 PM
Lăbonţu Marius-Andrei
Lăbonţu Marius-Andrei - avatar