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