【问题标题】:How to read integer from InputStremReader如何从 InputStreamReader 读取整数
【发布时间】:2012-12-21 10:58:06
【问题描述】:

我是初学者...请多多包涵:)

这是一段假定读取数组中的 10 个整数的代码。 InputStreamReader 读入 char(即 java Docs 所说的)如何将 char 转换为 int 以保存在数组中?

int[] array = new int[10];
System.out.println("Please enter 10 integers. ");
for(int x=1; x <11; x++){
    InputStreamReader keyboard = new InputStreamReader(System.in);
    // Change char into int   
    array[x] = keyboard;
}

谢谢

【问题讨论】:

  • @SJuan76,除此之外,您的评论很粗鲁。 Google 不会在前 10 个结果中提供对 OP 问题的确切答案。取而代之的是,您可以提供指向如何使用 Scanner 类或其他东西的教程的链接。每个人都曾经是初学者。
  • @Vash 每个人都曾经是初学者,但有些人比其他人付出了更多的努力才不再是初学者。至少五分钟不使用谷歌搜索是最起码的,如果有人不关心它,我不在乎看起来很粗鲁。
  • @SJuan76 一开始我被你的回答吓坏了,但无论如何我很感激你的回复......你付出更多努力是绝对正确的......我会努力的......谢谢。

标签: java char int inputstreamreader


【解决方案1】:

试试这个 -

Scanner sc = new Scanner(System.in);
int[] array = new int[10];
for(int x=0; x <10; x++){
    array[x] = sc.nextInt();
}

Scanner#nextInt 将输入的下一个标记扫描为int。并抛出

InputMismatchException - if the next token does not match the Integer regular expression, or is out of range
NoSuchElementException - if input is exhausted
IllegalStateException - if this scanner is closed

【讨论】:

  • 谢谢你..我很感激。
猜你喜欢
  • 1970-01-01
  • 2011-01-26
  • 1970-01-01
  • 1970-01-01
  • 2020-01-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-04
相关资源
最近更新 更多