-
Reading from file?
hey guys, Im having trouble reading different data from one line
Here is part of my txt file:
Code:
AEM, Intake, 242.95
AGP, Intercooler, 214.95
Apex, Turbo, 5495.00
Apexi, Intake, 154.95
Apexi, Exhaust, 769.95
Apex, Suspension, 1147.95
I would like to read the first column individually, then the second data, and the third data (price), not the whole line
I know how to read the first data, ie AEM, and using the whitespace, I can read Intake also, but it also reads the "price" included, so I dont know how to break it into 3 parts.
I tried to add commas, and specific the indexes, but it doesnt work when I tried to read the price.
I also tried using this:
Code:
readStream = new Scanner(new FileInputStream("mods.txt")).useDelimiter("," , ",");
But because Im a noob, I'm not using it correctly:p
Can someone help?
THANKS!
-
Quote:
Originally Posted by
6thDAY
AEM, Intake, 242.95
Why don't you read entire lines and String.split( ... ) them on the comma?
You'll end up with an array of Strings, i.e. array[0]= "AEM"; array[1]= "Intake" and array[2]= "242.95" for the first line.
kind regards,
Jos
-
can u give me an example of what you mean by that?
thanks
-
Code:
FileReader inputFile= new FileReader ("xxx.txt");
BufferedReader inputBuffer = new BufferedReader(inputFile);
String Line = inputBuffer.readLine();
String[] record1 = Line.split(", "); /* You will have record1[0] = "AEM", record1[1] = "Intake", record1[2] = "242.95" */
-
Thanks mi14chal!
I wanted to ask u though, instead of storing them in an array, I would like to store each of them individually in a different variable based on if the user wants to search by brand name, model, or price
Here is my code:
Code:
public void searchBrand()
{
Scanner readStreamBrand = null;
String readBrand = null;
String storeBrand = null;
try
{
readStreamBrand = new Scanner(new FileInputStream("mods.txt"));
System.out.print("Enter the manufacturer: ");
brandName = keyboard.nextLine();
while (readStreamBrand.hasNextLine())
{
readBrand = readStreamBrand.nextLine();
storeBrand = readBrand.substring(0, readBrand.indexOf(","));
if (storeBrand.equalsIgnoreCase(brandName))
{
break;
}
else
{
storeBrand = null;
}
}
if (storeBrand == null) //brand name could not be found in database
{
System.out.println("TunerShop does not hold the manufacturer you are looking for.");
System.out.println("Contact them directly for more information!");
}
else
{
searchPart();
}
}
catch (FileNotFoundException e)
{
System.out.println("********************************");
System.out.println("* The file could not be found! *");
System.out.println("********************************");
System.exit(0);
}
readStreamBrand.close();
}
public void searchPart()
{
}
public void searchPrice()
{
}
As you can see I have the searchBrand(), searchPart(), and searchPrice().
In the code:
Code:
storeBrand = readBrand.substring(0, readBrand.indexOf(","));
I was able to read the brand name using the substring.
But how would I go about doing that in my searchPart() method to store the model since the model is in the middle of 2 commas?
Same question with the price?
Can you show me by example? I learn best by it. Thanks!
-
Split the lines you read on comma-space as has been suggested. Then you can obtain the appropriate target string (brand, model or price) from the array that split() returns.
-------------------------------
You may find it useful to parse the price string as a double: ie use parseDouble() on the price string. That way you can offer useful searches such as finding products whose price lies between two values.