亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

嘗試使用 Array 而不是 ArrayList 從具有用戶輸入的文本文件中讀取特定行

嘗試使用 Array 而不是 ArrayList 從具有用戶輸入的文本文件中讀取特定行

慕田峪9158850 2023-08-16 18:23:10
我已經弄清楚如何將文本文件中的內容讀取、存儲和顯示到數組中,但我的問題是當用戶按下數字時僅輸出特定行..例如,如果用戶輸入 3,它應該顯示文本文件第三行中的所有內容..這是我迄今為止所擁有的內容的片段。文本文件將包含 john、doe、[email protected]、10101、blue、342.21 等內容。謝謝!public static void readLine(Record[] objects) throws FileNotFoundException {    Scanner in = new Scanner(System.in);    File fileobject = new File(filename);    Scanner input = new Scanner(fileobject);        String[] array;        System.out.println("Enter the record you would like to see: ");        int userChoice = in.nextInt();        for (int i = 0; i < userChoice; i++) {                objects[i] = new Record();                array = input.nextLine().replaceAll(" ", "").split(",");                objects[i].firstname = array[0];                String name = array[0];                objects[i].lastname = array[1];                String lastname = array[1];                objects[i].email = array[2];                String email = array[2];                objects[i].idnumber = Double.parseDouble(array[3]);                double idnumber = Double.parseDouble(array[3]);                objects[i].color = array[4];                String color = array[4];                objects[i].balance = Double.parseDouble(array[5]);                double balance = Double.parseDouble(array[5]);                passThis(name, lastname, email, idnumber, color, balance, userChoice);        }          }    public static void passThis(String firstname, String lastname, String email, double idnumber, String color, double balance, int userChoice) {           System.out.println(firstname + " " + " " + lastname + " " + email + " " + (int)idnumber + " " + color + " " + balance);    }
查看完整描述

2 回答

?
揚帆大魚

TA貢獻1799條經驗 獲得超9個贊

建議:不要使用Scanner來讀取文件。它實際上很慢,但很可能適合您的用例。您可能想嘗試以下方法:


Scanner in = new Scanner(System.in);

System.out.println("Enter the record you would like to see: ");

int userChoice = in.nextInt();


// Try With Resources is used here to auto-close the reader.    

try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {

    String line;

    int counter = 0;

    // String[] myArray = {};

    while ((line = reader.readLine().trim()) != null) {

        if (line.equals("")) {

            continue;

        }

        counter++;

        if (counter == userChoice) {

            // myArray = line.split("\\s{0,},\\s{0,}");

            // System.out.println(Arrays.toString(myArray).replaceAll("[,\\[\\]]", ""));

            // If you want to use an Array then un-comment the 

            // 3 lines above and comment the line below.

            System.out.println(line.replace(", ", " "));

            break;

        }

    }

}

catch (FileNotFoundException ex) {

    System.err.println(ex.getMessage());

}

catch (IOException ex) {

    System.err.println(ex.getMessage());

}


查看完整回答
反對 回復 2023-08-16
?
陪伴而非守候

TA貢獻1757條經驗 獲得超8個贊

 int userChoice = in.nextInt(); 
 for (int i = 0; i < userChoice; i++) {

我認為你不需要for循環。只需使用userChoise[userChoise-1].


查看完整回答
反對 回復 2023-08-16
  • 2 回答
  • 0 關注
  • 148 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號