第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

獲取 null 作為數(shù)組的輸出

獲取 null 作為數(shù)組的輸出

幕布斯7119047 2021-10-28 09:22:24
import java.util.*;public class a{   public static void main(String[] args) throws FileNotFoundException {    Scanner sc = new Scanner(new File ("master file.txt"));    String[] ids = new String[100];    System.out.println(ids);while(sc.hasNext()) {    int i = 0;    ids[i] = sc.next();    i++;}我試圖將數(shù)據(jù)從文件放入數(shù)組。我總是得到一個空值作為輸出。我想不通為什么。這一直非常令人緊張。
查看完整描述

2 回答

?
慕森王

TA貢獻(xiàn)1777條經(jīng)驗(yàn) 獲得超3個贊

  1. 在用元素填充數(shù)組之前,您正在打印數(shù)組。

  2. 您的計(jì)數(shù)器在循環(huán)的每次迭代中都i重置為。雖然使用具有固定數(shù)量元素的數(shù)組來讀取未知長度的文本不是一個好主意,但還是使用一些動態(tài)數(shù)組,例如.0whileArrayList

  3. 確保您提供了正確的.txt文件路徑。

所以你的代碼可能是這樣的:

   Scanner sc = new Scanner(new File ("C:/correct/path/to/file/master_file.txt")); 


    List<String> listOfStrings = new ArrayList<String>(); 


        while(sc.hasNextLine()) {


           listOfStrings.add(sc.nextLine());


        }


    System.out.println(listOfStrings);


查看完整回答
反對 回復(fù) 2021-10-28
?
嚕嚕噠

TA貢獻(xiàn)1784條經(jīng)驗(yàn) 獲得超7個贊

輸出為空,因?yàn)樵趪L試打印數(shù)組之前您從未分配過任何數(shù)組。我還將 i 移到循環(huán)之外,因此每次都不會重新初始化。此外,由于 ids 是一個數(shù)組,您需要使用 Arrays.toString(ids) 來打印它,或者您只需獲取對象 id。


public static void main(String[] args) throws FileNotFoundException {

    String[] ids = new String[100];  //array to store lines

    int i = 0;  // line index

    try (Scanner sc = new Scanner(new File ("master file.txt"))) { // try resource

        while(sc.hasNextLine()) { // check for next line

            ids[i] = sc.nextLine(); // store line to array index

            i++;  // increment index

        }

    }

    System.out.println(Arrays.toString(ids));  //print output.

}


查看完整回答
反對 回復(fù) 2021-10-28
  • 2 回答
  • 0 關(guān)注
  • 206 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號