-2

So what I'm trying to do but clearly struggling to execute isSo what I'm trying to do but clearly struggling to execute isSo what I'm trying to do but clearly struggling to execute isSo what I'm trying to do but clearly struggling to execute isSo what I'm trying to do but clearly struggling to execute isSo what I'm trying to do but clearly struggling to execute isSo what I'm trying to do but clearly struggling to execute isSo what I'm trying to do but clearly struggling to execute is a single line in the text f

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();
Majeet
  • 9
  • 5

2 Answers2

1

You are closing your file after you read one "line" (actually, I'm not sure how many lines you're reading - you don't call nextLine). You also aren't parsing the line. Also, I'd prefer a try-with-resources over an explicit close (and many of your variables look like class names). Finally, you need to check if the line matches your criteria. That might be done like,

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);
    }
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0

Your code print everything in the file.

To print a line with an given ID:

You can first buffer all lines of the file into a ArrayList like this in the main method:

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

Then create a method to find a line with a specific ID:

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; 
}

In the main method:

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));
Jason Liu
  • 109
  • 1
  • 4