相关疑难解决方法(0)

在使用next()或nextFoo()之后,Scanner正在跳过nextLine()?

我正在使用这些Scanner方法nextInt()nextLine()阅读输入.

它看起来像这样:

System.out.println("Enter numerical value");    
int option;
option = input.nextInt(); // Read numerical value from input
System.out.println("Enter 1st string"); 
String string1 = input.nextLine(); // Read 1st string (this is skipped)
System.out.println("Enter 2nd string");
String string2 = input.nextLine(); // Read 2nd string (this appears right after reading numerical value)
Run Code Online (Sandbox Code Playgroud)

问题是输入数值后,第一个input.nextLine()被跳过而第二个input.nextLine()被执行,所以我的输出如下所示:

Enter numerical value
3   // This is my input
Enter 1st string    // The program is supposed to stop here and …
Run Code Online (Sandbox Code Playgroud)

java io java.util.scanner

627
推荐指数
17
解决办法
45万
查看次数

Scanner#nextFloat的奇怪行为

在Eclipse中运行以下操作最初导致Scanner无法识别控制台中的回车,从而有效阻止了进一步的输入:

price = sc.nextFloat();
Run Code Online (Sandbox Code Playgroud)

在代码之前添加此行会导致Scanner接受0,23(法语表示法)作为浮点数:

Locale.setDefault(Locale.US);
Run Code Online (Sandbox Code Playgroud)

这很可能是由于Windows XP Pro(法语/比利时语)中的区域设置.当代码再次运行时,仍然接受0,23并输入0.23会导致它抛出一个java.util.InputMismatchException.

有关为什么会发生这种情况的任何解释?还有一个解决方法或我应该使用Float#parseFloat

编辑:这演示了Scanner如何使用不同的语言环境(在开头取消注释其中一行).

import java.util.Locale;
import java.util.Scanner;


public class NexFloatTest {

    public static void main(String[] args) {

        //Locale.setDefault(Locale.US);
        //Locale.setDefault(Locale.FRANCE);

        // Gives fr_BE on this system
        System.out.println(Locale.getDefault());

        float price;

        String uSDecimal = "0.23";
        String frenchDecimal = "0,23";

        Scanner sc = new Scanner(uSDecimal);

        try{
            price = sc.nextFloat();
            System.out.println(price);
        } catch (java.util.InputMismatchException e){
            e.printStackTrace();
        }

        try{
            sc = new Scanner(frenchDecimal);
            price = sc.nextFloat();
            System.out.println(price);
        } catch (java.util.InputMismatchException e){
            e.printStackTrace();
        }

        System.out.println("Switching …
Run Code Online (Sandbox Code Playgroud)

java java.util.scanner

13
推荐指数
1
解决办法
1万
查看次数

nextLine()的问题;

可能重复:
在nextInt之后使用nextLine时出现扫描程序问题

我正在尝试创建一个程序,它允许用户使用扫描仪将值输入到数组中.

但是,当程序要求学生的近亲时,它不会让用户输入任何内容并立即结束该程序.

以下是我所做的代码:

if(index!=-1)
    {
        Function.print("Enter full name: ");
        stdName = input.nextLine();

        Function.print("Enter student no.: ");
        stdNo = input.nextLine();

        Function.print("Enter age: ");
        stdAge = input.nextInt();

        Function.print("Enter next of kin: ");
        stdKin = input.nextLine();

        Student newStd = new Student(stdName, stdNo, stdAge, stdKin);
        stdDetails[index] = newStd;
    }
Run Code Online (Sandbox Code Playgroud)

我尝试过使用next(); 但它只会取用户输入的第一个字而不是我想要的.反正有没有解决这个问题?

java java.util.scanner

11
推荐指数
2
解决办法
4万
查看次数

无法打印第0个数组元素

先生,我正在尝试打印阵列的第0个元素,但我不能.这是我的代码

import java.util.Scanner;
public class prog3
{
    public static void main (String[] args){
        Scanner input = new Scanner(System.in);
        int size = input.nextInt();
        String arr[] = new String[size];

        for (int i=0; i<size; i++){
            arr[i]= input.nextLine();
        }

        System.out.print(arr[0]);
    }
}
Run Code Online (Sandbox Code Playgroud)

当我试图打印arr[0]其返回空白但当我打印arr[1]它时返回第0个元素值.你能帮我找到我的错误吗?

java arrays

5
推荐指数
2
解决办法
140
查看次数

String的NumberFormatException似乎是一个数字

我对一个JAVA程序有一点问题.我试图做一个InsertionSort算法,但似乎是转换String程序通过stdin获取的问题.似乎程序只使用少量数字,但它不适用于这些数字:https: //dl.dropboxusercontent.com/u/57540732/numbers.txt

这是我的算法:

public class Sort {

    private static ArrayList<String> insertionSort(ArrayList<String> arr) {
        for (int i = 1; i < arr.size(); i++) {
            int valueToSort = Integer.parseInt(arr.get(i).trim());
            int j = i;
            while (j > 0 && Integer.parseInt(arr.get(j - 1).trim()) > valueToSort) {
                arr.set(j, arr.get(j-1));
                j--;
            }
            arr.set(j, Integer.toString(valueToSort));
        }
        return arr;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        ArrayList<String> al;
        String inputNumbers = sc.nextLine();
        String[] xs = inputNumbers.split(" ");
        al = new ArrayList<String>(Arrays.asList(xs)); …
Run Code Online (Sandbox Code Playgroud)

java sorting string insertion-sort numberformatexception

5
推荐指数
1
解决办法
472
查看次数

Java输入字符串

我在返回用户输入字符串时遇到困难.如果我有一个代码:

System.out.println("please enter a digit: ");
number1 = in.nextInt();
System.out.println("enter another digit: ");
number2 = in.nextInt();
System.out.println("enter a string: ");
string = in.nextLine();
//calculations

System.out.println(number1);
System.out.println(number2);
System.out.println(string);
Run Code Online (Sandbox Code Playgroud)

它打印出数字但不打印字符串.我觉得这个解决方案非常简单,但我现在正在做脑屁.任何帮助,将不胜感激!

java

4
推荐指数
1
解决办法
591
查看次数

Java Scanner不等待输入

我这里有两个代码块.一个扫描仪正确地等待用户输入,另一个扫描仪正好通过它并且调用nextInt()返回一个NoSuchElementException.这是有效的块:

public void startGame() {
   out.println("Player1: 1 for dumb player, 2 for smart player, 3 for human player.");
   Scanner scan = new Scanner(System.in);
   p = scan.nextInt();

   if (p == 1)
        p1 = new DumbPlayer("ONE");
   if (p == 2)
        p1 = new SmartPlayer("ONE");
   else 
       p1 = new HumanPlayer("ONE");

   out.println("Player2: 1 for dumb player, 2 for smart player, 3 for human player.");
   p = scan.nextInt();

   if (p == 1)
        p2 = new DumbPlayer("TWO");
   if (p == 2)
        p2 …
Run Code Online (Sandbox Code Playgroud)

java user-input java.util.scanner

4
推荐指数
2
解决办法
1万
查看次数

Java计算器不执行if语句

我对编程比较陌生,最近开始学习Java以便进入Android编程.我以为我会创建一个非常简单的计算器来练习,但似乎我的if语句不起作用.

import java.util.Scanner;

public class Calculator {

    public static void main(String[] args) {
        //Create new scanner object
        Scanner numInput = new Scanner( System.in );

        //Enter first number
        System.out.println("Please enter the first number: ");
        int num1 = numInput.nextInt();

        //Enter the second number
        System.out.println("Please enter the second number: ");
        int num2 = numInput.nextInt();

        //Choose the operation to perform (+,-,*,/)
        System.out.println("What operation would you like to do?");
        System.out.println("Type \"+\" to add.");
        System.out.println("Type \"-\" to subtract.");
        System.out.println("Type \"*\" to multiply.");
        System.out.println("Type \"/\" to divide."); …
Run Code Online (Sandbox Code Playgroud)

java if-statement calculator

4
推荐指数
1
解决办法
6613
查看次数

为什么在Java中跳过整数输入后的字符串输入?

我试图在Java中输入某些字符串和整数变量的值.但是如果我在整数之后输入字符串,则在控制台中只跳过字符串输入并移动到下一个输入.

这是代码

String name1;
int id1,age1;

Scanner in = new Scanner(System.in);

//I can input name if input is before all integers

System.out.println("Enter id");
id1 = in.nextInt();


System.out.println("Enter name");       //Problem here, name input gets skipped
name1 = in.nextLine();

System.out.println("Enter age");
age1 = in.nextInt();
Run Code Online (Sandbox Code Playgroud)

java

4
推荐指数
1
解决办法
2万
查看次数

简单的Java Scanner代码无法正常工作

这是我写的一些基本代码的骨架,用于制作一个简单的游戏:

    Scanner in = new Scanner(System.in);

    String name;
    String playing;
    int age;

    do {

        System.out.println("Enter your name");
        name = in.nextLine();

        System.out.println("Enter your age");
        age = in.nextInt();

        System.out.println("Play again?");
        playing = in.nextLine();

    } while (true);
Run Code Online (Sandbox Code Playgroud)

代码无法按预期工作,例如,以下是代码的预期功能:

Enter your name
John
Enter your age
20
Play again?
Yes
Enter your name
Bill
...
Run Code Online (Sandbox Code Playgroud)

但是,再次阅读Play有一个问题,这是实际输出:

Enter your name
John
Enter your age
20
Play again?
Enter your name
Run Code Online (Sandbox Code Playgroud)

如您所见,"再次播放?"之前会再次显示"输入您的姓名".能够接受输入.当调试播放变量设置为""时,没有我可以看到的输入,我无法弄清楚正在消耗什么.任何帮助将不胜感激,谢谢!

java loops java.util.scanner

3
推荐指数
1
解决办法
3266
查看次数