2017-08-20 13 views
-2

私は何をしようとしているのですか?実行しようとしているのは明らかです。私がやろうとしていることは明らかですが、実行するのがはっきりしています。私がやろうとしていることは明らかですが、しかし実行するためにはっきりと苦労している私は何をしようとしているが、実行するために明確に苦労しているので、私は何をしようとしているが、実行するために明確に奮闘している。だから私は何をしようとしているが、あなたは一つの「行」を読んだ後行うが、明らかに実行するのに苦労することはあなたがあなたのファイルを閉じているこれをチェックする必要はありません!スキップ

import java.util.Scanner; 
import java.io.*; 
public class hello 
{ 
    public static void main(String[] args) throws IOException 
    { 
     Scanner Keyboard = new Scanner(System.in); 
     System.out.print(); 
     String response = Keyboard.nextLine(); 
     File inFile = new File(response); 
     Scanner route = new Scanner(inFile); 
     while() 
     { 
     System.out.print("); 
     String word = Keyboard.next(); 
     String Street = route.next(); 
     String stopNum = route.next(); 
+0

このコードで現在発生しているエラーまたは問題は何ですか? – Izruo

+0

ファイルを読み取って一致するID番号を見つける方法がわかりません@Izruo – Majeet

+0

ループ内の 'Scanner' **を閉じないでください** ... – AKSW

答えて

1

fのテキストに一行 ですし(実際には、私はあなたが読んでいる何行かわかりません - nextLineに電話しないでください)。また、行を解析していません。また、私は明示的な終了(そしてあなたの変数の多くはクラス名のように見える)の上にtry-with-resourcesを好むでしょう。最後に、lineが条件に一致するかどうかを確認する必要があります。それは、

Scanner keyboard = new Scanner(System.in); 
System.out.print("Enter filename >> "); 
String response = keyboard.nextLine(); 
File inFile = new File(response); 
System.out.print("Enter tram tracker ID >> "); 
String word = keyboard.nextLine(); // <-- read a line. Bad idea to leave trailing 
            //  new lines. 
try (Scanner route = new Scanner(inFile)) { 
    while (route.hasNextLine()) { 
     String[] line = route.nextLine().split("\\^"); 
     String street = line[0]; 
     String stopNum = line[1]; 
     String trkID = line[2]; 
     String road = line[3]; 
     String suburb = line[4]; 
     if (!trkID.equals(word)) { 
      continue; 
     } 
     System.out.printf("street: %s, stop: %s, id: %s, road: %s, suburb: %s%n", 
       street, stopNum, trkID, road, suburb); 
    } 
} 
0

のようにすることができます。あなたのコードはファイル内のすべてを印刷します。そして、検索する方法を作成

ArrayList<String> lines = new ArrayList<>(); 
while (route.hasNextLine()) 
{ 
    lines.add(route.nextLine()); 
} 

:あなたが最初のメインの方法でこのようなArrayListのにファイルのすべての行をバッファリングすることができます

:指定したIDの行を印刷するように

mainメソッドで

public static int find(ArrayList information, int ID) 
{ 
    String idString = "" + ID; 
    ListIterator<String> li = information.listIterator(); 
    String currentLine = ""; 
    int index = 0; 
    while(li.hasNext()) 
    { 
     currentLine = li.next(); 

     int count = 0; 
     int index1 = 0; 
     int index2 = 0; 

     /*Trying to locate the string between the 2nd and 3rd^*/ 
     for(int i = 0; i < currentLine.length(); i++) 
     { 
      if(currentLine.substring(i, i+1).equals("^")) 
      { 
       count++; 
       if(count == 2) 
        index1 = i; 
       else if(count == 3) 
       { 
        index2 = i; 
        break; 
       } 
      } 
     } 

     if(currentLine.substring(index1+1, index2).equals (idString)) 
      return(index); 

     index++; 
     } 
    //If no such ID found, return -1; 
    return -1; 
} 

System.out.println("enter an ID") 
int ID = Integer.parseInt(Keyboard.next()); 
int lineNumber = find(lines, ID); 
if(lineNumber == -1) 
    System.out.println("no information found"); 
else 
    System.out.println(lines.get(lineNumber)); 
固有IDと一致
関連する問題