Java 如何在Userinput上下文中处理arrayindex越界异常?

Java 如何在Userinput上下文中处理arrayindex越界异常?,java,arrays,exception,Java,Arrays,Exception,这就是我犯的错误 String[] v=s.split(" ");//it is reading only first character into v[0], But the remaining Strings are getting omitted. ex: Consider "1 1 2011" is given as user input. It is taking v[0]=1, but v[1] and v[2] are being empty. 我的代码 1 1 2011

这就是我犯的错误

String[] v=s.split(" ");//it is reading only first character into v[0], But the remaining Strings are getting omitted.

ex: Consider "1 1 2011" is given as user input. It is taking v[0]=1, but 
 v[1] and v[2] are being empty.
我的代码

1 1 2011
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
    at com.techm.Magic.main(Magic.java:15)

package com.techm;

使用
String s=sc.nextLine()
代替
String s=sc.next()


我们需要使用
nextLine()
方法来读取字符串。

使用
String s=sc.nextLine()
而不是
String s=sc.next()


我们需要使用
nextLine()
方法来读取字符串。

使用
String s=sc.nextLine()
而不是
s=sc.next()


另外,请仔细阅读:以便更好地理解。

使用
字符串s=sc.nextLine()
而不是
s=sc.next()


另外,请通读:以便更好地理解。

先生,您能解释一下为什么我们需要使用nextLine()?使用
next()
将只返回分隔符前面的内容(默认为空白)。
next()
只能读取输入直到空格。它不能读两个被空格隔开的单词。另外,
next()
在读取输入后将光标放在同一行中
nextLine()
返回当前行后自动向下移动扫描仪
nextLine()
读取输入,包括单词之间的空格(也就是说,它一直读取到行末\n)。读取输入后,
nextLine()
将光标定位在下一行。请不要叫我sir:)先生,您能解释一下为什么我们需要使用nextLine()?使用
next()
只会返回分隔符之前的内容(默认为空白)。
next()
只能读取输入,直到空格为止。它不能读两个被空格隔开的单词。另外,
next()
在读取输入后将光标放在同一行中
nextLine()
返回当前行后自动向下移动扫描仪
nextLine()
读取输入,包括单词之间的空格(也就是说,它一直读取到行末\n)。一旦输入被读取,
nextLine()
将光标定位在下一行。请不要叫我先生:)
import java.util.Scanner;

public class Magic {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner sc=new Scanner(System.in);
        String s=sc.next();
        int h=0;


        String[] v=s.split(" ");
         h=(Integer.parseInt(v[0])*Integer.parseInt(v[1]));

         String k=String.valueOf(h);
         int len=k.length();

         String d=v[2];
          int y=Integer.parseInt(d);
         int a=0;

         if(len==1)
         {
             a=y%10;

            if(h==a) 
                System.out.println("true");
            else
                System.out.println("false");
         }
         else if(len==2)
         {
             //d=s.substring(s.length()-2);
             a=y%100;
             if(h==a)
                 System.out.println("true");
             else
                 System.out.println("false");
         }

         else
         {
             //d=s.substring(s.length()-3);
             a=y%1000;
             if(h==a)
                 System.out.println("true");
             else
                 System.out.println("false");
         }   
        }
    }