Results 1 to 6 of 6
  1. #1
    6thDAY is offline Member
    Join Date
    Aug 2010
    Posts
    8
    Rep Power
    0

    Default Reading from file?

    hey guys, Im having trouble reading different data from one line

    Here is part of my txt file:

    Java 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:

    Java 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!

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by 6thDAY View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    6thDAY is offline Member
    Join Date
    Aug 2010
    Posts
    8
    Rep Power
    0

    Default

    can u give me an example of what you mean by that?

    thanks

  4. #4
    mi14chal is offline Member
    Join Date
    Dec 2010
    Posts
    15
    Rep Power
    0

    Default

    Java 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" */

  5. #5
    6thDAY is offline Member
    Join Date
    Aug 2010
    Posts
    8
    Rep Power
    0

    Default

    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:

    Java 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:
    Java 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!

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    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.

Similar Threads

  1. Reading file .txt
    By will_java in forum Advanced Java
    Replies: 5
    Last Post: 11-20-2010, 05:16 AM
  2. reading a file and writing to a file....help!!!!
    By java_prgr in forum New To Java
    Replies: 3
    Last Post: 07-26-2010, 07:53 PM
  3. Replies: 9
    Last Post: 10-20-2009, 11:52 AM
  4. Replies: 3
    Last Post: 05-10-2009, 12:31 AM
  5. reading csv file help
    By fritz1474 in forum New To Java
    Replies: 5
    Last Post: 09-04-2008, 09:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •