mia_tech
02-26-2009, 06:56 PM
guys I'm suppose to read from a text file, and separate the text from the numbers and put them into an arrayList....here's the text file:
Cupcake 1.50
Coffee .75
Doughnut .95
Tea 1.25
I'm not asking for the code.... just some ideas as to how I could loop through the file and separate the text from the numbers... here's what I've got so far
Main
import java.io.*;
import java.util.*;
public class ReadInMenu {
public static void main(String [] args)
{
ArrayList <Item> allTheItems = new ArrayList<Item>();
try
{
FileReader firstReader = new FileReader("menu.txt");
BufferedReader br = new BufferedReader(firstReader);
String line = "";
int counter = 0;
while((line = br.readLine()) != null)
{
counter++;
System.out.println("We have read in " + counter + " items!");
//parse menu and make Items
}
catch (Exception e)
{ System.out.println(e); }
Item Class
public class Items {
private String name = "";
private double price = 0.0;
public Items(String name, double price)
{
this.name = name;
this.price = price;
}
public String getName()
{
return name;
}
public double getPrice()
{
return price;
}
public void setName(String nam)
{
name = nam;
}
public void setPrice(double p)
{
price = p;
}
}
Cupcake 1.50
Coffee .75
Doughnut .95
Tea 1.25
I'm not asking for the code.... just some ideas as to how I could loop through the file and separate the text from the numbers... here's what I've got so far
Main
import java.io.*;
import java.util.*;
public class ReadInMenu {
public static void main(String [] args)
{
ArrayList <Item> allTheItems = new ArrayList<Item>();
try
{
FileReader firstReader = new FileReader("menu.txt");
BufferedReader br = new BufferedReader(firstReader);
String line = "";
int counter = 0;
while((line = br.readLine()) != null)
{
counter++;
System.out.println("We have read in " + counter + " items!");
//parse menu and make Items
}
catch (Exception e)
{ System.out.println(e); }
Item Class
public class Items {
private String name = "";
private double price = 0.0;
public Items(String name, double price)
{
this.name = name;
this.price = price;
}
public String getName()
{
return name;
}
public double getPrice()
{
return price;
}
public void setName(String nam)
{
name = nam;
}
public void setPrice(double p)
{
price = p;
}
}