简体   繁体   中英

How do I store the user input that is indented by a space into the String array and convert the number into int array?

I'm not that new to programming, but I had a problem when storing the user input to the string array and store it into an int array. Does anybody know how to fix my code? Or is there any other way?

I want to store this input into a separate int array and a string array.

User input:

"T 3"

"V 4"

"Q 19"

Expected result:

num[0] = 3

num[1] = 4

num[2] = 19

store[0] = "T"

store[1] = "V"

store[2] = "Q"

This code creates an index out of bounds:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1

    import java.util.*;

    public class Main
    {
        public static void main(String[] args) 
        {
            // Variable Declarations & Initializations
            Scanner input = new Scanner(System.in);     
            int k = input.nextInt();
            int[] num = new int[k];
            String[] store = new String[k];
            for(int i = 0; i < k; i++)
            {
                String[] paint = input.next().split(" ");
                store[i] = paint[0];
                num[i] = Integer.parseInt(paint[1]);
            }//end loop
         }//end main
      ]//end class
    

The problem is that you're calling input.next() which returns the next token, delimited by spaces. Therefore, the token itself can't contain any spaces. So the .split(" ") method call will always return a one-element array, so there is only a paint[0] and there is no paint[1] . It's not clear what you're trying to achieve in your code; if you expand the question, you may get some more answers.

Update: now that you've shown us your input and expected results, I think what you need to do is:

store[i] = input.next();
num[i] = input.nextInt();

input.next() will only work if you took String data type but here you had taken int as the data type so it won't work here.

Try this out

import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int k = sc.nextInt();
        int[] num = new int[k];
        String[] store = new String[k];
        for(int i=0; i<k; i++){
            String s = sc.nextLine();
            String str = sc.next();
            int number = Integer.parseInt(sc.next());
            num[i]=number;
            store[i]=str;
        }
        for(int i=0; i<k; i++){
            System.out.println(store[i] +" "+num[i]);
        }
    }
}

Check this. Minor changes done in your code.

import java.util.*;

public class Main
{
    public static void main(String[] args) 
    {
        // Variable Declarations & Initializations
        Scanner input = new Scanner(System.in);     
        int k = Integer.parseInt(input.next());
        int[] num = new int[k];
        String[] store = new String[k];
        input.nextLine();
        for(int i = 0; i < k; i++)
        {
            String paint[] = input.nextLine().split(" ");
            store[i] = paint[0];
            num[i] = Integer.parseInt(paint[1]);
        }//end loop
     }//end main
  }//end class

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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