Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从用户处获取输入字符串并将其存储到字符串数组中_Java - Fatal编程技术网

Java 从用户处获取输入字符串并将其存储到字符串数组中

Java 从用户处获取输入字符串并将其存储到字符串数组中,java,Java,有人能帮我解决这个问题吗?我想将用户输入的字符串存储到字符串数组中。错误为:Java.lang,无法将字符串转换为String.String 这是我的密码: 扫描仪输入=新扫描仪(System.in); 内化; System.out.println(“输入存储数据的数组大小…”); nSize=input.nextInt(); 字符串[]名称=新字符串[nSize];//名称数组 int[]电话=新的int[nSize]//电话阵列 对于(int i=1;i我运行了您的代码,没有遇到您在问题中描

有人能帮我解决这个问题吗?我想将用户输入的字符串存储到字符串数组中。错误为:
Java.lang,无法将字符串转换为String.String

这是我的密码:

扫描仪输入=新扫描仪(System.in);
内化;
System.out.println(“输入存储数据的数组大小…”);
nSize=input.nextInt();
字符串[]名称=新字符串[nSize];//名称数组
int[]电话=新的int[nSize]//电话阵列

对于(int i=1;i我运行了您的代码,没有遇到您在问题中描述的问题

但是,我确实发现了同一行中的一个问题,这很可能是您问题的解决方案。(不匹配错误)

您应该使用
names[i]=input.next();
而不是
names[i]=input.nextLine();
否则,它将返回一个空字符串。

Scanner input=new Scanner(System.in);
Scanner input = new Scanner(System.in);
        int nSize;
        System.out.println("Enter size of array to store data..:");
        nSize = input.nextInt();
        String[] names = new String[nSize]; // name array
        int[] phone = new int[nSize]; //phone array
        for (int i = 0; i < nSize; i++) {
            System.out.println("Enter name..:");
            names[i] = input.next();
            System.out.println("Enter phone number..:");
            phone[i] = input.nextInt()`enter code here`;
        }
内化; System.out.println(“输入存储数据的数组大小…”); nSize=input.nextInt(); 字符串[]名称=新字符串[nSize];//名称数组 int[]phone=new int[nSize];//电话阵列 对于(int i=0;i
执行代码后,我没有看到您提到的错误。此外,所需的输出有点含糊不清。 但是,我试图通过使用BufferedReader来实现我从中解释的内容

BufferedReader reader=新的BufferedReader(新的InputStreamReader(System.in));
内化;
System.out.println(“输入存储数据的数组大小…”);
试一试{
nSize=Integer.parseInt(reader.readLine());
字符串[]名称=新字符串[nSize];//名称数组
long[]phone=new long[nSize];//电话阵列
对于(int i=0;i

检查是否有帮助。

好吧,如果你不想让你的类与另一个名为
String
的类混淆,就不要给它命名。请发布你的完整类。另外,如果你在int数组中存储电话号码,它不能存储10位数字。如果你想将电话号码存储为需要长时间使用的号码,你会这样做吗要将字符串中的每个字符存储在字符串数组的各个索引中吗?感谢您的建议。我非常感激我面临的问题。当我输入名称并按enter键时,会抛出一个错误“输入不匹配错误”您好@naeem khan,尝试上述解决方案。它工作正常。在您的代码中,您将值存储在第一个索引中,但应该存储在0个索引中。观察“for”循环初始化。它不工作。当我输入全名如“naeem khan”时,它会给出错误,数字也是如此
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    int nSize;
    System.out.println("Enter size of array to store data..:");
    try {
        nSize = Integer.parseInt(reader.readLine());
        String[] names = new String[nSize]; // name array
        long[] phone = new long[nSize]; // phone array
        for (int i = 0; i < nSize; i++) {
            System.out.println("Enter name..:");
            names[i] = reader.readLine();
            System.out.println("Enter phone number.:");
            phone[i] = Long.parseLong(reader.readLine());

        }
    } catch (NumberFormatException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}