Java 扫描程序无法从字符串中读取最后一个整数

Java 扫描程序无法从字符串中读取最后一个整数,java,string,int,java.util.scanner,Java,String,Int,Java.util.scanner,diceValue是这样的字符串:“1 2 3 4 5”,扫描器总是跳过最后一个数字。所以int val(value)总是比它应该的小。我认为您的代码没有问题。我这样运行它,它返回了150的值 if (diceValues == null || diceValues.length() == 0) return 0; int temp; int val = 0; Scanner scanner = new Scanner(diceValues); while

diceValue是这样的字符串:“1 2 3 4 5”,扫描器总是跳过最后一个数字。所以int val(value)总是比它应该的小。

我认为您的代码没有问题。我这样运行它,它返回了150的值

    if (diceValues == null || diceValues.length() == 0) return 0;
    int temp;
    int val = 0;
    Scanner scanner = new Scanner(diceValues);
    while (scanner.hasNext()) {
        temp = scanner.useDelimiter(" ").nextInt();
        if (temp == 1) val += 100;
        if (temp == 5) val += 50;
    }

您是否已正确调试此程序,因为我可以看到所有值都已从扫描仪中正确读取您如何知道它跳过了最后一个数字?我刚刚用
System.out.println(temp)
试过,它显示了所有5个数字。谢谢,我应该找一下我程序的其他部分。
import java.util.Scanner;
public class Prog1 
{
    public static void main(String[] args){
        Prog1 p =new Prog1();
        int value = p.mymethod();
        System.out.println(value);
    }
    public int mymethod()
    {
        String diceValues = "1 2 3 4 5";
        if (diceValues == null || diceValues.length() == 0) return 0;
        int temp;
        int val = 0;
        Scanner scanner = new Scanner(diceValues);
        while (scanner.hasNext()) {
        temp = scanner.useDelimiter(" ").nextInt();
            if (temp == 1) val += 100;
            if (temp == 5) val += 50;
        }
        return val;
    }