Results 1 to 6 of 6
  1. #1
    MrJimmah is offline Member
    Join Date
    Nov 2010
    Posts
    2
    Rep Power
    0

    Default Making a String Array from a text file

    i would like to put some string objects that i am reading from a text file into an object. the file has 13 lines of data and each line has 5 fields. here is the text file:

    1,Cheese,SC,Small,12.95
    2,Cheese,MC,Medium,13.95
    3,Cheese,LC,Large,14.95
    4,Cheese & Sausage,SCS,Small,13.95
    5,Cheese & Sausage,MCS,Medium,14.95
    6,Cheese & Sausage,LCS,Large,15.95
    7,Cheese & Pepperoni,SCP,Small,13.95
    8,Cheese & Pepperoni,MCP,Medium,15.95
    9,Cheese & Pepperoni,LCP,Large,16.95
    10,Cheese & Vegetarian,SCV,Small,12.95
    11,Cheese & Vegetarian,MCV,Medium,13.95
    12,Cheese & Vegetarian,LCV,Large,15.95
    13,Cheese & Zebra,JC,Large,20.00

    the fields are separated by commas. the first field is the menu item number, the second is the type, the third is the code, the fourth is the size and the fifth is the price.

    i used the buferedreader class to make a bufferedreader object so i can access my file and read from it. then i used a while loop to read all the lines of data in text file. but when i print out my results the block of statements i did winded up putting all the fields onto one element in the array.

    here is my block of statements so far:

    package test;

    import java.io.*;

    public class scratch
    {

    public void experiment()
    {
    File x = new File("C:\\Program Files\\eclipse\\workplace\\scratch2\\src\\test\\me nuTextFile.txt");


    try
    {
    BufferedReader in = new BufferedReader(new FileReader(x));
    String line = in.readLine();
    while(line != null)
    {
    String[] pizzaArray = line.split(",");
    String menuItemNumber = pizzaArray[0];
    String pizzaType = pizzaArray[1];
    String pizzaCode = pizzaArray[2];
    String pizzaSize = pizzaArray[3];
    String pizzaPrice = pizzaArray[4];
    line = in.readLine();

    System.out.print(pizzaArray[4]);
    }




    in.close();
    }


    catch (Exception e) //this fixes an error just in case
    {
    System.out.print("DOH!"); //this does block does the action
    }

    }



    }

    can anyone tell me what i did wrong? did i use a wrong type of loop to try and make the array? Also how can i make a string object of each line of data so that it can be accessed by any other class in program?

    thanks in advance.

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    you used print instead of println.

  3. #3
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by MrJimmah View Post
    did i use a wrong type of loop to try and make the array? Also how can i make a string object of each line of data so that it can be accessed by any other class in program?

    each line you read is overwriting the previous readed string. if you want to keep all readed lines in memory my suggestion is: make a class Pizza with a constructor that takes all readed pizzaArray (menuItemNumber, pizzaArray etc.) and methods for adding pizzas etc. and then instantiate an ArrayList of type Pizza.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,172
    Rep Power
    20

    Default

    You're not actually storing anything.
    You are creating a new load of Strings each time round the loop and then printing pizzaArray[4] (the price I think). Those Strings are then simply ditched.

  5. #5
    MrJimmah is offline Member
    Join Date
    Nov 2010
    Posts
    2
    Rep Power
    0

    Default

    Quote Originally Posted by j2me64 View Post
    each line you read is overwriting the previous readed string. if you want to keep all readed lines in memory my suggestion is: make a class Pizza with a constructor that takes all readed pizzaArray (menuItemNumber, pizzaArray etc.) and methods for adding pizzas etc. and then instantiate an ArrayList of type Pizza.

    so how would i make another class to get the arrays my loop is making. i also need each line of data to be an object so that i can associate the pizza type with the price. so when the user inputs a pizza type then the price that is associated with it can also appear.

    can anyone please put up some sample code that i can work with. i need a rough idea to point me in the right direction.

    thanks again for all the replies so far. they have been very helpful. :D

  6. #6
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by MrJimmah View Post
    can anyone please put up some sample code that i can work with. i need a rough idea to point me in the right direction.

    hallo, i'm not sure if an arraylist points you in the right direction but my example is done with an arraylist

    Java Code:
    import java.io.*;
    import java.util.ArrayList;
    
    class Pizza {
    	String menuItemNumber;
    	String pizzaType;
    	String pizzaCode;
    	String pizzaSize;
    	String pizzaPrice;
    	
    	// the constructor
    	
    	public Pizza(String[] strArr) {
    		this.menuItemNumber = strArr[0];
    		this.pizzaType = strArr[1];
    		this.pizzaCode = strArr[2];
    		this.pizzaSize = strArr[3];
    		this.pizzaPrice = strArr[4];
    	}
    	
    	public String toString() {
    		// change the returned string to your requirements
    		return "item nr: " + menuItemNumber + ", type: " + pizzaType + ", code: " + pizzaCode + ", size: " + pizzaSize + ", price: " + pizzaPrice ; 
    	}
    }
    
    public class PizzaExample {
    
    	private ArrayList<Pizza> pizzaList = new ArrayList<Pizza>();
    	public void experiment() {
    		File x = new File(
    				"C:/temp/menutextfile.txt");
    		try {
    			BufferedReader in = new BufferedReader(new FileReader(x));
    			String line;
    			while ((line = in.readLine())!= null) {
    				String[] pizzaArray = line.split(",");
    				// the assignments is done in the constructor
    				Pizza p = new Pizza(pizzaArray);
    				// an ArrayList offers a lot of methods like adding, removing and so on 
    				pizzaList.add(p);
    			}
    		in.close();
    		}
    		catch (Exception e) // this fixes an error just in case
    		{
    			System.out.print("DOH!"); // this does block does the action
    		}
    
    	}
    	public static void main(String[] args) {
    		PizzaExample s = new PizzaExample();
    		s.experiment();
    		
    		System.out.println("List of Pizzas");
    		System.out.println("--------------");
    		
    		for (Pizza p : s.pizzaList) {
    			System.out.println(p.toString());
    		}
    	}
    }
    Last edited by j2me64; 11-17-2010 at 12:29 AM.

Similar Threads

  1. Writing a long string to a text file
    By jessie in forum New To Java
    Replies: 2
    Last Post: 11-07-2010, 09:19 AM
  2. Writing a long string to a text file
    By jessie in forum New To Java
    Replies: 2
    Last Post: 11-06-2010, 05:44 PM
  3. text file into a string?
    By Landon in forum New To Java
    Replies: 5
    Last Post: 09-12-2010, 08:57 AM
  4. get string from a text file
    By Kruptein in forum New To Java
    Replies: 11
    Last Post: 08-14-2009, 05:45 PM
  5. how to store string in text file
    By santhosh_el in forum AWT / Swing
    Replies: 2
    Last Post: 04-03-2009, 07:21 AM

Posting Permissions

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