[英]Parsing text file input into an array
我有一個文本文件,其中包含名稱后跟 11 個整數。 我需要將這些數字解析為一個數組,其中最大的 11 個整數值存儲在表示該值 (0-10) 的維度中。
我還需要維護名稱的完整性。
例如:
Aaliyah 0 0 0 0 0 0 0 0 0 380 215:我正在尋找 output 和 Aaliyah [9] 一樣,並且該元素中存儲了 380 個值
Aaron 193 208 218 274 279 232 132 36 32 31 41
Abagail 0 0 0 0 0 0 0 0 0 0 958
Abbey 0 0 0 0 0 0 0 0 537 451 428
Abbie 431 552 742 924 0 0 0 0 752 644 601
Abbigail 0 0 0 0 0 0 0 0 0 953 562
Abby 0 0 0 0 0 906 782 548 233 211 209
Abdiel 0 0 0 0 0 0 0 0 0 925 721
Abdul 0 0 0 0 0 0 0 903 0 0 0
Abdullah 0 0 0 0 0 0 0 0 0 1000 863
Abe 248 328 532 764 733 0 0 0 0 0 0
這是我到目前為止的代碼:
import java.io.*;
import java.util.*;
public class Names
{
private static String name = "bb";
private static int [] yearRank;
private static boolean match;
public static void main( String[] args)
{
java.io.File file = new java.io.File("names.txt");
try
{
Scanner input = new Scanner(file);
while (input.hasNext())
{
String num = input.nextLine();
if(match = num.toLowerCase().contains(name.toLowerCase()))
{
for (int i = 0; i<11; i++)
{
System.out.println(num);
}
}
}
catch(FileNotFoundException e)
{
System.err.format("File does not exist\n");
}
}
}
num
在你的循環中不會改變 - 如果你知道空格分隔不同的字段,你可以這樣做,例如(未測試):
String num = input.nextLine();
if(match = num.toLowerCase().contains(name.toLowerCase())) {
String[] fields = num.split(" "); //split the line into an array, each item contains a field
for (String field : fields) { //loop over the array
System.out.println(field); //print each field
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.