从控制台读取INT

编程入门 行业动态 更新时间:2024-10-23 22:35:29
本文介绍了从控制台读取INT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我怎么能转换字符串数组转换成Java中的 INT 数组?我从控制台读取整数字符流成字符串阵列,以

How can I convert a String array into an int array in java? I am reading a stream of integer characters into a String array from the console, with

BufferedReader br = new BufferedReader (new InputStreamReader(System.in)); for(c=0;c<str.length;c++) str[c] = br.readLine();

其中,海峡[] 为String类型。我想比较海峡[] 不能对字符(错误)进行内容...因此我想从控制台读取 INT 。这可能吗?

where str[] is String typed. I want to compare the str[] contents ... which can't be performed on chars (the error) And hence I want to read int from the console. Is this possible?

推荐答案

的Integer.parseInt(字符串); 是你想要的东西。

Integer.parseInt(String); is something that you want.

试试这个:

int[] array = new int[size]; try { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); for (int j = 0; j < array.length ; j++) { int k = Integer.parseInt(br.readLine()); array[j] = k; } } catch (Exception e) { e.printStackTrace(); }

不管怎么说,你为什么不使用扫描仪?如果您使用扫描仪这将会是你要容易得多。 :)

Anyways,why don't you use Scanner? It'd be much easier for you if you use Scanner. :)

int[] array = new int[size]; try { Scanner in = new Scanner(System.in); //Import java.util.Scanner for it for (int j = 0; j < array.length ; j++) { int k = in.nextInt(); array[j] = k; } } catch (Exception e) { e.printStackTrace(); }

更多推荐

从控制台读取INT

本文发布于:2023-10-27 16:00:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1533858.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:控制台   INT

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!