Note that there are some explanatory texts on larger screens.

plurals
  1. POJava scanner.nextLine() is skipping
    text
    copied!<p>I am writing a TicTacToe program and for some reason it is skipping the scanner.nextLine() for player 1, but is stopping for player 2. Entire code is as follows: Method in question is called "Names"</p> <pre><code>package tictactoe; import static java.lang.System.*; import java.io.*; import java.util.Scanner; public class TicTacToe { static String p1name; static String p2name; static String p1marker; static String p2marker; public static Scanner qwe = new Scanner(in); public static void title(){ out.println("\tTicTacToe by Ryan Hosford"); out.print("\n\n\n"); out.println("1)Start"+"\t\t" + "2)How to play\t\t" + "3)Quit "); int title = qwe.nextInt(); if(title == 1) Names(); else if(title ==2) Info(); else if(title ==3) Quit(); else title(); } public static void Names(){ out.print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"); out.println("Player 1, What is your name?"); Setp1Name(qwe.nextLine()); out.println("Nice to meet you " + Getp1Name()); out.println(""); out.println("Player 2, what is your name?"); Setp2Name(qwe.next()); out.println("Nice to meet you " + Getp2Name()); qwe.next(); Markers(); } public static void Markers(){ out.print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"); out.println(Getp1Name()+", Would you like to X's or O's?"); Setp1Marker(qwe.next()); if(Getp1Marker().equals("X") || Getp1Marker().equals("x")){ Setp2Marker("O"); } else if(Getp1Marker().equals("O") || Getp1Marker().equals("o")){ Setp2Marker("X"); } else{ Markers(); } out.println(Getp1Name() + " is playing with: " +Getp1Marker()); out.println(Getp2Name() + " is playing with: " + Getp2Marker()); out.println(""); out.println("Press enter to continue..."); qwe.nextLine(); Game(); } /////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////// </code></pre> <p>/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// </p> <pre><code>public static void main(String[] args) { title(); } public static void Setp1Name(String player1name){ p1name = player1name; } public static String Getp1Name(){ return p1name; } public static void Setp2Name(String player2name){ p2name = player2name; } public static String Getp2Name(){ return p2name; } </code></pre> <p>////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////<br> ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// </p> <pre><code>public static void Setp1Marker(String p1Mark){ p1Mark.toUpperCase(); p1marker = p1Mark; } public static String Getp1Marker(){ return p1marker; } public static void Setp2Marker(String p2Mark){ p2Mark.toUpperCase(); p2marker = p2Mark; } public static String Getp2Marker(){ return p2marker; } ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// </code></pre> <p>///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// </p> <pre><code>public static void Info(){ out.print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"); out.println("TicTacToe is a game where you have to try and get 3 of your markers in a row; Be that diagonally, vertically, or horizontally."); out.println("The two markers in TicTacToe are X and O"); out.println("The two players take turns placing one of their markers, and the game is over once a player gets 3 in a row."); out.println(""); out.println("Press enter to continue..."); qwe.next(); title(); } public static void Quit(){ out.print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"); out.println("Quitting game..."); exit(0); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload