[英]Java Scanner class reading strings
我已經創建了一個掃描程序類來讀取文本文件並獲取我正在追求的值。 我們假設我有一個文本文件包含。
人員名單:長度3
1:Fnjiei:ID 7868860:年齡18歲
2:Oipuiieerb:ID 334134:年齡39歲
3:Enekaree:ID 6106274:年齡31歲
我正在嘗試獲取名稱和ID號和年齡,但每次我嘗試運行我的代碼時它都會給我一個例外。 這是我的代碼。 來自java大師的任何建議?:)它能夠讀取一行.......但不超過一行文本。
public void readFile(String fileName)throws IOException{
Scanner input = null;
input = new Scanner(new BufferedReader(new FileReader(fileName)));
try {
while (input.hasNextLine()){
int howMany = 3;
System.out.println(howMany);
String userInput = input.nextLine();
String name = "";
String idS = "";
String ageS = "";
int id;
int age;
int count=0;
for (int j = 0; j <= howMany; j++){
for (int i=0; i < userInput.length(); i++){
if(count < 2){ // for name
if(Character.isLetter(userInput.charAt(i))){
name+=userInput.charAt(i); // store the name
}else if(userInput.charAt(i)==':'){
count++;
i++;
}
}else if(count == 2){ // for id
if(Character.isDigit(userInput.charAt(i))){
idS+=userInput.charAt(i); // store the id
}
else if(userInput.charAt(i)==':'){
count++;
i++;
}
}else if(count == 3){ // for age
if(Character.isDigit(userInput.charAt(i))){
ageS+=userInput.charAt(i); // store the age
}
}
id = Integer.parseInt(idS); // convert id to integer
age = Integer.parseInt(ageS); // convert age to integer
Fighters newFighters = new Fighters(id, name, age);
fighterList.add(newFighters);
}
userInput = input.nextLine();
}
}
}finally{
if (input != null){
input.close();
}
}
}
我的應用程序,如果我的代碼只是需要改變。
編輯它給了我一個格式異常!!! 我不知道這些值之間會有多少空白空間。
這是一個僅使用Scanner
API的解決方案,重要的是findInLine
。 它可以處理輸入格式中的次要語法變體,但它非常易讀,不需要花哨的正則表達式或魔術數組索引。
String text =
"List of @#%^$ people : length 3 !@%# \n" +
"1 : Fnjiei : ID 7868860 ::: Age 18\n" +
" 2: Oipuiieerb : ID 334134 : Age 39 (old, lol!) \r\n" +
" 3 : Enekaree : ID 6106274 => Age 31\n";
Scanner sc = new Scanner(text);
sc.findInLine("length");
final int N = sc.nextInt();
for (int i = 0; i < N; i++) {
sc.nextLine();
sc.findInLine(":");
String name = sc.next();
sc.findInLine("ID");
long id = sc.nextLong();
sc.findInLine("Age");
int age = sc.nextInt();
System.out.printf("[%s] %s (%s)%n", id, name, age);
}
這打印:
[7868860] Fnjiei (18)
[334134] Oipuiieerb (39)
[6106274] Enekaree (31)
Scanner.findInLine(Pattern pattern)
Pattern.compile
重載 這似乎更短:
public void readFile(String fileName)throws IOException
{
Scanner input = null;
input = new Scanner(new BufferedReader(new FileReader(fileName)));
String userInput;
try
{
while (input.hasNextLine())
{
userInput = input.nextLine().trim();
if (userInput.length() > 0)
{
String[] userInfo = userInput.split(":");
int count = Integer.parseInt(userInfo[0].trim());
String name = userInfo[1].trim();
int id = Integer.parseInt(userInfo[2].trim().split("\\s+")[1].trim());
int age = Integer.parseInt(userInfo[3].trim().split("\\s+")[1].trim());
System.out.println("Count: " + count + " Name: " + name + " ID:" + id + " Age:" + age);
}
Fighters newFighters = new Fighters(id, name, age);
fighterList.add(newFighters);
}
}
finally
{
if (input != null)
{
input.close();
}
}
}
對於你有我們的輸入,它打印出來:
人數:1姓名:Fnjiei ID:7868860年齡:18歲
數量:2姓名:Oipuiieerb ID:334134年齡:39
數量:3姓名:Enekaree ID:6106274年齡:31歲
有關拆分方法的更多信息,請參見此處 。 我基本上首先使用:
as delimiter拆分該行,然后,我再次使用\\\\s+
拆分,它基本上拆分一個字符串並返回一個包含由空格分隔的單詞的數組。
Scanner input = null;
input = new Scanner(new BufferedReader(new FileReader("filename")));
try{
while(input.hasNextLine()){
String userInput = input.nextLine();
String[] data = userInput.split(":");
System.out.println("Name: "+data[1]+" ID:"+data[2].split("\\s+")[2]+
" Age:"+data[3].split("\\s+")[2]);
}
}finally{
if(input != null)
input.close();
}
上面的代碼段顯示了基本的想法。另外請記住,這可能不是最佳解決方案。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.