2015-10-21 21 views
1

我試圖讀取製表符分隔的txt文件並將數據放入字符串數組的兩列中。試圖將分隔的txt文件掃描到字符串數組中

package mailsender; 

import java.io.*; 
import java.util.Scanner; 

public class MailSenderList { 
    static String address=null; 
    static String name=null; 
    static String[][] mailer; 

    // @SuppressWarnings("empty-statement") 
    public static void main(String[] args) throws IOException { 

     try { 
      Scanner s = new Scanner(new BufferedReader(new FileReader("/home/fotis/Documents/MailRecipients.txt"),'\t')); //This is the path and the name of my file 

      for(int i=0;i>=30;i++){ 
       for(int j=0;j>=2;j++){ 
        if (s.hasNext());{ 
        mailer[i][j]=s.next(); //here i am trying to put 1st+2 word into first column and 2nd+2 into second column. 
       } 

       } 
      } 
      for(int ii=0;ii>=30;ii++){ 
       System.out.println("Line : "); 
       for(int ji=0;ji>=2;ji++){ 
        System.out.print(" " + mailer[ii][ji]); 
//trying to print and check the array 
       } 
      } 

     } 
     catch (java.io.FileNotFoundException e) { 
     System.out.println("Error opening file, ending program"); 
     //System.exit(1);} 


    } 
} 
class mail{ 
mail(){ 

} 

} 
} 

文件成功生成,但沒有結果System.out.In調試器,似乎它從來沒有從第一for循環傳遞。

+0

該代碼將永遠不會輸入for循環。你說我= 0,而我大於或等於30,這不是,所以它會退出循環。 – Ryan

回答

0

確實。因爲是錯的,但我頭暈。儘管如此,令人困惑的「<」並不是真正的問題。代碼與while一起工作。 這是全班同學。

import java.io.File; 
import java.util.Scanner; 

public class ReadFile { 


    public static void main(String[] args) { 

     try { 

      File file = new File("/home/fotis/Documents/Mailers.txt"); //this a the path there 
      try (Scanner input = new Scanner(file).useDelimiter("\\t")) { 
       String line[] = new String[150000]; 
       int i=0; 


       while (input.hasNextLine()) { 
        line[i] = input.next(); 
        System.out.println(line[i]); 
        i++; 
       } 
      } 

     } catch (Exception ex) { 
      ex.printStackTrace(); 
     } 
+0

嗯,是的,這個效果更好。但你最初的問題是你混合了大於和小於。 – Ryan

1

您可能在<和>之間犯了一個錯誤。嘗試在兩個循環中將i> = 30切換到i < = 30。與j循環相同。