我正在尝试读取制表符分隔的txt文件,并将数据放入String数组的两列中。
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 :(得分:1)
你可能在&lt;之间犯了一个错误。和&gt;。尝试在两个for循环中将i&gt; = 30切换到i <= 30.与j循环相同。
答案 1 :(得分:0)
确实。因为这是错的。但我感到头晕目眩。 然而,令人困惑的是&#34;&lt;&gt;&#34;不是真正的问题。 代码与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();
}