2014-02-18 23 views
0

我必須從外部文件讀取數字列表並將它們插入到它們自己的數組中,以獲得正數或負數。掃描器讀取的數字很好,但是當它將它們插入到數組中時就是出錯的地方。正如你在下面看到的是我的代碼和輸出。輸出中打印的是文件中的內容,但當我要求它打印數組時,這是混亂的字母/數字/符號。任何人都可以幫我修復它嗎?可能的編碼錯誤[I @ 24e11c

public class Numbers { 
    public static void main(String[] args) throws IOException { 
     Scanner reader = new Scanner(new FileInputStream("First.dat")); 
     int Positive[] = new int[20]; 
     int Negative[] = new int[20]; 
     int X = 0; 
     int Y = 0; 
     while (reader.hasNextInt()) { 
      int Start = reader.nextInt(); 
      System.out.println(Start); 
      if (Start < 0) { 
       Negative[X] = Start; 
       X += 1; 
      } 
      if (Start > 0) { 
       Positive[Y] = Start; 
       Y += 1; 
      } 

     } 
     System.out.println(Positive + " " + Negative); 
    } 
} 

輸出:

3 
66 
54 
-8 
22 
-16 
-56 
19 
21 
34 
-34 
-22 
-55 
-3 
-55 
-76 
64 
55 
9 
39 
54 
33 
-45 
[[email protected] [[email protected] 
+2

您希望打印什麼?爲什麼? –

+0

我希望它打印出數字,我將數字插入 –

+0

如果你不知道自己在做什麼,那麼_Want_是無關緊要的。你做了什麼,你爲什麼這樣做?你爲什麼不做別的事? –

回答

6

觀看第直接轉換的陣列到String的結果。數組也是對象,但它們不會覆蓋Object's toString()方法,該方法負責「混亂的混亂」。

換句話說,此方法返回一個字符串等於的值:

getClass().getName() + '@' + Integer.toHexString(hashCode()) 

明確的數組的內容轉換成與StringArrays.toString

System.out.println(Arrays.toString(Positive) + " " + Arrays.toString(Negative)); 
+0

這工作,感謝您的幫助! –

0

你看到的是數組的對象引用。打印出您希望使用的陣列內容Arrays.toString()