从控制台读取INT [英] reading int from console

查看:215
本文介绍了从控制台读取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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆