Reading data from keyboard to store in string array

走远了吗. 提交于 2020-01-05 12:59:04

问题


String s[];
System.out.println("Enter loop value");
int t = s.nextInt();
for(int i=0;i<t;i++)
{
 str[i]=s.nextLine();
}

while it reading it gives null pointer exception


回答1:


What you need is something like this:

    Scanner s = new Scanner(System.in);

    System.out.println("Enter loop value");
    int t = s.nextInt(); // read number of element
    s.nextLine(); // consume new line
    String str[] = new String[t];
    for(int i=0;i<t;i++)
    {
     str[i]=s.nextLine();
    }

    System.out.println(Arrays.toString(str));

Hope this helps.




回答2:


I changed the typos and missing declarations in your code:

package snippet;

import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;

public class Snippet {

    public static void main(String[] args) throws IOException {

        Scanner s = new Scanner(System.in);

        String str[] = new String[10];
        System.out.println("Enter loop value (maximum 9)");
        int t = s.nextInt();
        for (int i = 0; i <= t; i++) {
            str[i] = s.nextLine();
        }

        System.out.println("your entered lines:");
        System.out.println(Arrays.toString(str));
    }

}

However, I would recommend to store the values in a List instead of an Array. I also prefer using a BufferedReader to using a Scanner.




回答3:


Try something like this:

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter loop value");
    String a[] = new String[Integer.parseInt(br.readLine())];
    for (int i = 0; i < a.length; i++) {
        a[i] = br.readLine();
    }



回答4:


//After modifying my code works well
import java.io.*;
import java.util.Scanner;
class Sample 
{
public static void main(String[] args) throws Exception
{
    bufferedReader();
            scanner();
}
    static void bufferedReader()throws Exception{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("Enter Number of Strings");
            int len = Integer.parseInt(br.readLine());
            String a[] = new String[len];
    for(int i=0; i<len; i++){
                System.out.println("Enter String"+ (i+1));
        a[i] = br.readLine();
    }
            for(String str : a)
            System.out.println(str);
    }
    static void scanner(){
        Scanner s = new Scanner(System.in);
            System.out.println("Enter Number of Strings");
            int len = Integer.parseInt(s.nextLine());
            String a[] = new String[len];
    for(int i=0; i<len; i++){
                System.out.println("Enter String"+ (i+1));
        a[i] = s.nextLine();
    }
            for(String str : a)
            System.out.println(str);
    }
}

But when using Scanner nextLine() skips the data when read if any one knows please post description with example



来源:https://stackoverflow.com/questions/22346306/reading-data-from-keyboard-to-store-in-string-array

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