如何使用逐行拆分文本?

时间:2021-11-15 15:00:29

I am trying to split data by lines, using the method split. This is the code i am trying to use but i can't figure out what to use for the parameters of .split(). This is how the data will be in the txt.

我试图使用方法拆分按行分割数据。这是我试图使用的代码,但我无法弄清楚用什么来表示.split()的参数。这就是数据在txt中的表现方式。

19
23
58
49


Scanner sc = new Scanner(new File("random.txt")); 
string line = sc.nextLine(); 
String[] numbers = line.split(" "); 
int nums = new int[numbers.length]; 
for(int i=0;i<numbers.length;i++) 
nums[i] = Integer.parseInt(numbers[i]);

The end goal is so that the data can be put into an array, not to just print it.

最终目标是将数据放入数组中,而不是仅将其打印出来。

4 个解决方案

#1


2  

You dont need to use split if you have only one number in each line.

如果每行只有一个数字,则不需要使用拆分。

just use the following

只需使用以下内容

Scanner sc = new Scanner(new File("C:/projects/random.txt"));
        while( sc.hasNext()){
            System.out.println(sc.nextInt());
        }

#2


1  

Try to use it like this:

尝试使用它像这样:

Scanner sc = new Scanner(new File("random.txt"));

扫描仪sc =新扫描仪(新文件(“random.txt”));

int[] nums;

int i=0;

while(sc.hasNext()){

String line = sc.nextLine(); 

nums[i] = Integer.parseInt(line);

i++;

}

Hope this helps.

希望这可以帮助。

#3


0  

Just as simple as that, you don't need split since you're reading line by line as @Luiggi Mendoza said

就像那样简单,你不需要拆分,因为你正在逐行阅读@Luiggi门多萨说

Scanner sc = new Scanner(new File("C:/projects/random.txt"));
while( sc.hasNext()){
    nums[i] = Integer.parseInt(numbers[i]); //They're stored in here
    i++;
}
int max = i; //To keep length of numbers that have been read
for(i = 0; i < max; i++){
    System.out.println(nums[i]); //We print them from the array
}

#4


0  

Try this

while(true){
        int tmp=line.indexOf(" ");
        if(tmp==-1)
            break;
        System.out.println(ar[i]);
        ar[i++]=Integer.parseInt(line.substring(0,tmp));
        line =line.substring(tmp+1);    
    }

#1


2  

You dont need to use split if you have only one number in each line.

如果每行只有一个数字,则不需要使用拆分。

just use the following

只需使用以下内容

Scanner sc = new Scanner(new File("C:/projects/random.txt"));
        while( sc.hasNext()){
            System.out.println(sc.nextInt());
        }

#2


1  

Try to use it like this:

尝试使用它像这样:

Scanner sc = new Scanner(new File("random.txt"));

扫描仪sc =新扫描仪(新文件(“random.txt”));

int[] nums;

int i=0;

while(sc.hasNext()){

String line = sc.nextLine(); 

nums[i] = Integer.parseInt(line);

i++;

}

Hope this helps.

希望这可以帮助。

#3


0  

Just as simple as that, you don't need split since you're reading line by line as @Luiggi Mendoza said

就像那样简单,你不需要拆分,因为你正在逐行阅读@Luiggi门多萨说

Scanner sc = new Scanner(new File("C:/projects/random.txt"));
while( sc.hasNext()){
    nums[i] = Integer.parseInt(numbers[i]); //They're stored in here
    i++;
}
int max = i; //To keep length of numbers that have been read
for(i = 0; i < max; i++){
    System.out.println(nums[i]); //We print them from the array
}

#4


0  

Try this

while(true){
        int tmp=line.indexOf(" ");
        if(tmp==-1)
            break;
        System.out.println(ar[i]);
        ar[i++]=Integer.parseInt(line.substring(0,tmp));
        line =line.substring(tmp+1);    
    }