Java Scanner input dilemma. Automatically inputs without allowing user to type

耗尽温柔 提交于 2019-12-23 23:32:16

问题


Thanks a lot for responses, I will probably stick to just adding extra input.nextLine() statements to catch any "leftovers"

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();
}

回答1:


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) {
   .....



回答2:


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);



回答3:


Try having a different Scanner object for String.




回答4:


    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);
    }



回答5:


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())



来源:https://stackoverflow.com/questions/4524279/java-scanner-input-dilemma-automatically-inputs-without-allowing-user-to-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!