Joe Bork
Joe Bork

Reputation: 23

Read a specific line from file and separate the data from one line

I'm fairly new to java so any help would be appreciated.

I'm trying to write a program that asks the user which file to read from, then it asks for an ID, searches inside the file for that ID and then outputs the information from that line. The text file it will read from is formatted like this:

Apple^201^3

Banana^202^4

Orange^205^5

the 2nd column is for ID.

Also i need to separate the information some how so i can output it like this:

Item: Apple

ID: 201:

Price: $3

import java.util.*;
import java.io.*;
public class fruit
{
   public static void main(String[] args) throws IOException
{
  Scanner keyboard = new Scanner(System.in);

  System.out.println("Enter a filename >> ");
  String filename = keyboard.nextLine();

  File f = new File(filename);
  Scanner fin = new Scanner(f);

  System.out.println("Enter item ID: ");
  int fruitID = keyboard.nextInt();

  while(fin.hasNextLine())
  {
      String line = fin.nextLine();

      if(fin.hasNextInt(fruitID))
      {
          System.out.println(line);
      }
      else
      {
          System.out.println("ERROR");
      }
   }
      fin.close();
  }
}

Upvotes: 2

Views: 635

Answers (3)

Yogesh
Yogesh

Reputation: 725

Hope this will help


import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class FindGivenStringFromFile {

    public static void main(String args[]) throws FileNotFoundException {

        Scanner keyboard = new Scanner(System.in);

        System.out.println("Enter a filename >> ");
        String filename = keyboard.nextLine();

        File f = new File(filename);
        Scanner fin = new Scanner(f);

        System.out.println("Enter item ID: ");
        int fruitID = keyboard.nextInt();

        //Reading each line of file using Scanner class
        int lineNumber = 1;
        while (fin.hasNextLine()) {
            String line = fin.nextLine();
            String[] lineDataArray = line.split("\\^");
            if(lineDataArray != null && lineDataArray.length >2){
                if(Integer.parseInt(lineDataArray[1]) == fruitID){
                    System.out.println("Item: " + lineDataArray[0]);
                    System.out.println("Id: " + lineDataArray[1]);
                    System.out.println("price: $" + lineDataArray[2]);
                }

            lineNumber++;
        }       

    }   
}
}

Upvotes: 1

elirandav
elirandav

Reputation: 2063

public static void main(String[] args) throws IOException {
    Scanner keyboard = new Scanner(System.in);
    System.out.println("Enter a filename >> ");
    String filename = keyboard.nextLine();
    System.out.println("Enter item ID: ");
    String fruitID = Integer.toString(keyboard.nextInt());

    try (BufferedReader buffer = new BufferedReader(new FileReader(filename))) {
        String line;
        while ((line = buffer.readLine()) != null) {
            String[] tokens = line.split("\\^");
            if (tokens.length < 3 && !tokens[1].equals(fruitID))
                continue;
            System.out.println("Item: " + tokens[0]);
            System.out.println("ID: " + tokens[1]);
            System.out.println("Price: $" + tokens[2]);
            break;
        }
    }
    catch (Exception ex){
        ex.printStackTrace();
    }
}
  • Split by "^", check you have at least 3 tokens and then print them if the second token equal to the input ID.
  • Starting Java 7, you can use try with resources that ensures that each resource is closed at the end of the statement.
  • After you found the specific line, you can use break to avoid parsing the rest of the file.

Upvotes: 1

Scary Wombat
Scary Wombat

Reputation: 44813

As String split takes regex and ^ has regex meaning, then it will needs to be backslashed

    String input = fin.nextLine();  // e.g. Banana^202^4
    System.out.println(input.split("\\^")[2]);

Upvotes: 1

Related Questions