PDA

View Full Version : need help reading from file...


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

}

mia_tech
02-26-2009, 11:01 PM
I came up with this main, but for some reason is not printing out anything to screen
import java.util.*;
import java.io.*;
public class Main {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
//
ArrayList<Items> menuItems = new ArrayList<Items>();
try
{
//creating file reader
FileReader file = new FileReader("menu.txt");
BufferedReader br = new BufferedReader(file);
String line = "";
int counter = 0;
while((line = br.readLine()) != null)
{
System.out.println("We have read line: "+counter++);
}
String name = "";
double price = 0.0;
for(int i = 0; i < line.length(); i++)
{
System.out.println(line.length());
if(!line.substring(i, i+1).equals(" "))
{
name+= line.substring(i, i+1);
}
else
{
price += Double.parseDouble(line.substring(i, line.length()));
break;
}
menuItems.add(new Items(name, price));
}
}
catch(Exception e)
{
System.out.println(e);
}
for(Items e: menuItems)
{
System.out.println(e.getName()+" "+e.getPrice());
}

}

}

mia_tech
02-27-2009, 12:39 AM
finally got it working....

import java.util.*;
import java.io.*;
public class Main {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
ArrayList<Items> menuItems = new ArrayList<Items>();
try
{
//creating file reader
FileReader file = new FileReader("menu.txt");
BufferedReader br = new BufferedReader(file);
String line = "";
int counter = 0;
while((line = br.readLine()) != null)
{
System.out.println("We have read line: "+counter++);
String name = "";
double price = 0.0;
for(int i = 0; i < line.length(); i++)
{
if(!line.substring(i,i+1).equals(" "))
{
name+= line.substring(i,i+1);
}
else
{
price = Double.parseDouble(line.substring(i,line.length()));
break;
}
}
menuItems.add(new Items(name, price));
}
}
catch(Exception e)
{
System.out.println(e);
}
for(Items e: menuItems)
{
System.out.println(e.getName()+" "+e.getPrice());
}