2016-01-18 142 views
0

嗨我想做一个程序,声明一组字符串和一组整数。之后,我想根据编写的数字将两者都打印到控制台。Java扫描仪和错误

举例来说,如果我有像这样声明的,

String a[] = a1 a2 a3 a4 a5 

int b[] = 10 20 30 40 50 

我想A1和10打印出来,如果我在1到扫描仪类型。

import java.io.*; 
import java.lang.*; 
import java.util.Scanner; 
public class value { 

    private static Scanner sc; 
    public static void main(String args[]){ 

     String a[] = {"a1","a2","a3","a4","a5"}; 
     int b[] = {100, 220, 200, 230, 500}; 

     sc = new Scanner(System.in); 
     System.out.println("type in a number"); 
     String input = sc.nextLine(); 

     int i = Integer.parseInt(input); 
     int j = i - 1; 

     System.out.println(a[j] + b[j]); 
    } 
    } 

你能告诉我这有什么问题吗?我真的很新编程

+0

你在分析字符串时很脆弱。如果字符串没有任何整数类型,那么键入解析异常肯定会来,所以处理它。 –

+0

你能指定你正在遇到什么问题吗?别人会帮助你更容易。 – pnt

+0

@ pnt-你从问题中删除了}。现在它会正常工作。如果你有答案,你应该给出答案,不要编辑问题。 –

回答

1

这个答案假设你的int[]String[]数组已被声明。

首先,设置扫描仪并读取输入。

Scanner scanner = new Scanner(System.in); 
String input = scanner.nextLine(); 

在此之后,尝试从一个字符串到一个整数,这将让你抓住从阵列数据解析输入。

int j = 0; 
try { 
    j = Integer.parseInt(input); 
catch (NumberFormatException e) { 
    System.out.println("NaN"); 
    System.exit(-1); 
} 

现在你有一个号码。你现在要做的就是尝试从数组中获取数据。我们抓Exception的失败。第一种情况是,如果输入不是数字。第二种情况是数组的length小于请求的索引。

你还必须确保你的标准,一个阵列的第一指数是,不以下。

与示例代码:

import java.util.Scanner; 

public class Value { 
    public static void main(String args[]){ 
     String a[] = {"a1", "a2", "a3", "a4", "a5"}; 
     int b[] = {100, 220, 200, 230, 500}; 

     Scanner sc = new Scanner(System.in); 
     System.out.println("Type in a number."); 
     String input = sc.nextLine(); 

     int i; 
     try { 
      i = Integer.parseInt(input); 
     } catch (NumberFormatException e) { 
      System.out.println("NaN"); 
      System.exit(-1); // Replace with whatever you want if it fails. 
     } 
     try { 
      System.out.println(a[i] + " " + b[i]); 
     } catch (ArrayIndexOutOfBoundsException e) { 
      System.out.println("Index out of range: " + i); 
      System.exit(-1); // Again, change to whatever you want. 
     } 
    } 
} 
+0

除异常处理外,与作者的代码有什么不同? – pnt

+0

@pnt没什么。作者问题的关键在于他们的代码有哪些错误**。唯一的问题是错误处理是不存在的。由于** Yogesh Seralia **对此问题发表评论,因此存在解析漏洞的问题。 – finnrayment

+0

谢谢!问题解决了! –

0

你可以尝试这样的

import java.util.Scanner; 
public class Value { 
    public static void main(String args[]) { 
     String[] a = {"a1", "a2", "a3", "a4", "a5"}; 
     int[] b = {100, 220, 200, 230, 500}; 

     Scanner scanner = new Scanner(System.in); 
     while (scanner.hasNext()) { 
      if (choice < 0 || choice > Math.min(a.length, b.length)) { 
       System.out.println("out of range..."); 
      } else { 
       System.out.println(a[choice] + "\t" + b[choice]); 
      } 
     } 
    } 
} 

当你输入0,它会打印出a1 100,1型,打印出来a2 220

+1

“选择”在哪里定义? – finnrayment

+1

好像你是在if语句之前用空格 –

+0

发明''choice',Integer choice = scanner.nextInt();' – Nicholas

0

在你的代码中的问题是,System.out.println(a[j] + b[j]); out()方法。它应该是在main()和int j = i - 1;后删除}(这是一个额外的支架)

在您的代码:

。 。 。

int i = Integer.parseInt(input); 
int j = i - 1; 
    } 

    System.out.println(a[j] + b[j]); 
}} 

权代码:

....

  int i = Integer.parseInt(input); 
    int j = i - 1; 
    System.out.println(a[j] + b[j]); 
}} 

你也可以添加异常处理程序来处理像“ArrayIndexOutOfBoundException”。