简体   繁体   English

将字符串分配给String数组

[英]Assigning a String to a String array

I am trying to assign each string a user inputs to a String array. 我试图将用户输入的每个字符串分配给String数组。 The entire thing is in a for loop and is evaluated by the index of the array. 整个过程都在for循环中,并由数组的索引进行评估。 My code is: 我的代码是:

String skillAssign[] = new String[100];

    for (int i=0; isDone == false; i++)
    {
        System.out.println(skillAssign[i]);
        System.out.println(i);
        skillAssign[i] = keyboard.nextLine();
        if ((!(skillAssign[i].equalsIgnoreCase("stats"))) && (!(skillAssign[i].equalsIgnoreCase("done"))))
        {
            assignmentValue = keyboard.nextInt();
            if (((skillAssign[i].equalsIgnoreCase("health"))) && (skillPoints - assignmentValue >=0))
            {
                System.out.println("Added " + assignmentValue + " points to Health!");
                skillPoints = (skillPoints - assignmentValue);
                newMaxHealth = (assignmentValue + newMaxHealth);
            }
        //code that evaluates the string located inside the skillAssign[i] for other stats
    }

The first string evaluates properly, but when I go to input the second string, I get java.util.InputMisMatchException. 第一个字符串计算正确,但是当我输入第二个字符串时,我得到了java.util.InputMisMatchException。 How can I get it so it assigns a string to each index of the array inputted by the user, then evaluate it? 我如何获得它,以便它为用户输入的数组的每个索引分配一个字符串,然后对其进行评估? (I think I got the evaluation part though) (我想我虽然得到了评估部分)

I tried to limit the post to relevant code, so things like isDone are omitted, but isDone is changed to true when done is typed and keyboard is constructed with Scanner keyboard = new Scanner all other variables are set to 0 except for skillPoints 我试图将帖子限制为相关代码,因此省略了isDone之类的内容,但键入done后将isDone更改为true,并使用Scanner keyboard = new Scanner构造了Scanner keyboard = new Scanner所有其他变量均设置为0(skillPoints除外)

I have tested the abovementioned code, and this is what happens: 我已经测试了上面提到的代码,这是发生了什么:

  • We enter the loop. 我们进入循环。
  • You are requested to input the first string (through keyboard.nextLine() ). 要求您输入第一个字符串(通过keyboard.nextLine() )。 I inputted ' health '. 我输入了“ 健康 ”。
  • You are requested to input an integer (through keyboard.nextInt() ). 要求您输入一个整数(通过keyboard.nextInt() )。 I inputted ' 40 '. 我输入了“ 40 ”。
  • We re-enter the loop. 我们重新进入循环。
  • You are requested to input an integer (through keyboard.nextInt() ). 要求您输入一个整数(通过keyboard.nextInt() )。
  • ... ...

It seems that I'm not asked to input the second string, but instantly the integer. 似乎没有要求我输入第二个字符串,而是立即输入整数。

I do not know why it is, but it looks like nextInt() causes the next nextLine() to be skipped. 我不知道为什么,但是看起来nextInt()导致下一个nextLine()被跳过。

Maybe you can replace 也许你可以更换

assignmentValue = keyboard.nextInt();

with

try {
    assignmentValue = Integer.parseInt(keyboard.nextLine());
}
catch (NumberFormatException exc) {
    throw new InputMismatchException(exc.getMessage());
}

EDIT 编辑
I saw a post on StackOverflow which briefly mentions why nextLine() after nextInt() is skipped. 在StackOverflow上看到一则帖子 ,简要提到了为何nextInt()之后的nextLine() nextInt()被跳过。

i believe this is closer to your intention. 我相信这更接近您的意图。 this way the reference to your keyboard input that nextLine grabs isn't lost on each iteration, but remains preserved in a new String instance. 这样,nextLine捕获的对键盘输入的引用不会在每次迭代中丢失,而是保留在新的String实例中。 correct? 正确?

System.out.println(i);
String getMe = keyboard.nextLine();
skillAssign[i] = new String(getMe);
System.out.println(skillAssign[i]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM