
<strong>Thanks a lot for responses, I will probably stick to just adding extra input.nextLine() statements to catch any "leftovers"</strong>
So in this code I input 2, and once it goes to the if statement it skips the "sCreateLogin = input.nextLine();" and proceeds to the next input. Probably because there is something lingering in the Scanner yet I cannot figure out why it does it and how exactly to fix it.
If I do input.next() it stops, but it just isn't good enough because if you accidentally add a space it will also skip the next input. I know I could parse it etc., but I'm still confused with this.
Scanner input = new Scanner(System.in);
System.out.println("(1) Login");
System.out.println("(2) Create Account");
int iAccountOption = input.nextInt();
if(iAccountOption==2)
{
System.out.println("Input desired login: ");
String sCreateLogin = input.nextLine();
System.out.println("Input desired password: ");
String sCreatePassword = input.nextLine();
}
Answer1:
The problem is likely end of line tokens that are not being dealt with. To fix this, after input.nextInt(); add an extra input.nextLine() to swallow the end of line tokens:
int iAccountOption = input.nextInt();
input.nextLine();
if (iAccountOption == 2) {
.....
Answer2:
I will suggest you to use either of these two ways : 1. Using BufferedReader class 1a.Use BufferedReader Class and Wrap it with the InputStreamReader Class.
BufferedReader br = new BufferedReader(new InputStreamReader(System.in))
//string str = br.readLine(); //for string input
int i = Integer.parseInt(br.readLine()); // for Integer Input
1b.Now since the readLine method throw an IOException, so you need to catch it. so the whole code will look like this.
try{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in))
//string str = br.readLine(); //for string input
int i = Integer.parseInt(br.readLine()); // for Integer Input
}catch(IOException ioe){
ioe.PrintStackTrace();
}
2.if you are using the Java SE6 or higher then you can make use of Console class
Console cons = System.console();
String str = cons.readLine("Enter name :");
System.out.print("your name :"+str);
Answer3:
Try having a different Scanner object for String.
Answer4:
Scanner input = new Scanner(System.in);
System.out.println("(1) Login");
System.out.println("(2) Create Account");
int iAccountOption = input.nextInt();
if (iAccountOption == 2) {
input.nextLine(); // here you forget
System.out.println("Input desired login: ");
String sCreateLogin = input.nextLine();
System.out.println("Input desired password: ");
String sCreatePassword = input.nextLine();
System.out.println(sCreateLogin + " " + sCreatePassword);
}
Answer5:
It was skipping sCreateLogin because scanner.nextLine() already had a value "\r \n". So I changed all scanners to nextLine(). It worked out fine, but maybe it won't be the best idea.
package com.stackoverflow.main;
import java.util.Scanner;
public class SO4524279 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("(1) Login");
System.out.println("(2) Create Account");
int iAccountOption = new Integer(scanner.nextLine());
String sCreateLogin = "";
String sCreatePassword = "";
if (iAccountOption == 2) {
System.out.println("Input desired login: ");
sCreateLogin = scanner.nextLine();
System.out.println("Input desired password: ");
sCreatePassword = scanner.nextLine();
}
System.out.println("Login: " + sCreateLogin + "Pass: " + sCreatePassword);
}
}
Remember to use try catch on new Integer(scanner.nextLine())