简体   繁体   English

使用BufferedReader读取数组?

[英]Using BufferedReader to read an array?

I'm just beginning to learn Java, and I'm unsure of how to use BufferedReader to read an array in the assignment I'm working on. 我才刚刚开始学习Java,但不确定如何使用BufferedReader读取正在处理的作业中的数组。 getSalesData is its own method. getSalesData是它自己的方法。 I understand that I need to use BufferedReader to ask the user to input a number (which are Strings here) and then store it in data [0] and [1], but I'm unsure of how to proceed and fix the errors. 我知道我需要使用BufferedReader来要求用户输入一个数字(这里是字符串),然后将其存储在数据[0]和[1]中,但是我不确定如何进行和解决错误。 Any tips would be very much appreciated! 任何提示将不胜感激!

   String [] getSalesData (){
        String [] data = new String [2];
        String [] ticketsSold = "";
        String [] ticketPrice = "";

        BufferedReader br = null;
        String buffer = new String ();

        try {
            br = new BufferedReader (new InputStreamReader(System.in));
            System.out.print ("Enter your agent ID:");
            buffer = br.readLine ();
            ticketsSold = buffer;

            br = new BufferedReader (new InputStreamReader(System.in));
            System.out.print ("Enter your agent ID:");
            buffer = br.readLine ();
            ticketPrice = buffer;


        } catch (Exception e) {
            System.out.println ("Invalid entry");
        }

        return data;

br.readLine() will return a String and you are setting ticketsSold = buffer. br.readLine()将返回一个String,并且您正在设置ticketsSold = buffer。 So let's examine a little closer: buffer is a string and ticketsSold is an array of strings. 因此,让我们仔细研究一下:buffer是一个字符串,ticketsSold是一个字符串数组。 this should produce an error for you (if you can post the error stack trace that would be very helpful). 这应该为您产生一个错误(如果您可以发布错误堆栈跟踪信息,将非常有帮助)。 I'm not sure if you actually want ticketsSold and ticketPrice to be arrays of Strings as here it looks as if they should just be strings. 我不确定您是否真的希望ticketsSold和ticketPrice是字符串数组,因为在这里看起来它们应该只是字符串。

So if you want them to really be arrays of strings, use: 因此,如果您希望它们真正是字符串数组,请使用:

ticketsSold[0] = buffer;

and

ticketPrice[0] = buffer;

or you can change the declartion of ticketPrice and ticketsSold to be strings: 或者您可以将ticketPrice和ticketsSold的声明更改为字符串:

String ticketsSold = "";
String ticketPrice = "";

hope this helps and welcome to stack overflow! 希望这会有所帮助,并欢迎堆栈溢出!

peggy's answer already explained why you get the errors and how to resolve them. peggy的答案已经解释了为什么会收到错误以及如何解决它们。 But actually you don't need ticketsSold and ticketPrice at all. 但实际上您根本不需要ticketsSoldticketPrice You said you want to put the input in data[0] and data[1] . 您说过要将输入放入data[0]data[1] Therefore, completely remove ticketsSold and ticketPrice and write 因此,完全删除ticketsSoldticketPrice并写入

data[0] = buffer;

and

data[1] = buffer;

in the appropriate locations. 在适当的位置。 Then your return value will be correct. 那么您的返回值将是正确的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM