views:

475

answers:

6

im trying to create a restaurant system that will create food items and such from a menu.

ill be learning jdbc soon and im sure that would help but for now i think the simplest way is too create my menu in notepad.

whats the best way to line up and read from a notepad file like a menu.

please try speak clearly, im not exactly sure of all terminologies.

this one looks promising but ive no idea whats goin on.

/////////////////////////////////////////////////////////////////////

im still stuck with this.

ive decided too make a seperate mthod for reading the file.

ive tried every example i can think of. could someone just show me an example of how too define a files classpath.

if i type menu.txt it just doesnt work.

+1  A: 

"Notepad" files are just text files, so you just read it in with a a Reader instance. Since Notepad supports windows Unicode, you may need to specify a charset of "UTF-16LE".

GregS
+1  A: 

The basic idea is that you create a file reader object

FileReader fr = new FileReader('file.txt');

and then go over the file line by line parsing each line and saving the stuff to some internal data storage (Array, HashMap).

The while loop in the example you have does just this. The FileReader class will take care of the line ending for you, and will return null when there's no more lines to be read. What you need to do inside the while loop is to parse each line and separate the different bits of data (course name, price etc.) from each other.

EDIT: To parse the lines you would do something like the following. What is inside the while loop depends on how you format the menu files. The following works on the assumption that the menu files contains the price and the name of the course (in that order) separated by a comma on each line.

12.95$,Penne ala Arabiata
8.15$,Fish Soup

Notice that you can't use a comma in the price if you do this. You can of course use a semicolon as the separator between the data fields instead of a comma. The number of data fields is of course also up to you.

String line = "";
// read lines from file
while ((line = fr.readLine()) != null) {
    // parse each line
    tokens = line.split(",");
    String price = tokens[0];
    String courseName = tokens[1];
    // extract all other information
}

In your final code you'll want to save the data fields into some structure instead of just extracting them from the file. Another thing to note is that the price is a String NOT a number because of the dollar sign. Should you wish to do any calculations with the prices you'll of course need the convert it to a number with parseFloat() or parseDouble().

And of course if you do use the csv (comma separated values) format, it's better to go for a csv library to do the parsing for you instead of writing the parser yourself.

http://opencsv.sourceforge.net/

Matti
uhm, not too sound like a noob but what does parse each line mean and how would i do it
OVERTONE
Parsing means extracting meaning from something. Your java compiler will parse your java classes. Your file will contain a meaning, you just need to instruct the computer how to understand it, e.g. how to get the item, quantity and price, the table number, etc.
John Ferguson
+7  A: 

Have a look at Sun's Java Tutorial

r3zn1k
+1  A: 
String filename = "myfile.txt";
BufferedReader reader = new BufferedReader(new FileReader(filename));

try{
  String line;
  //as long as there are lines in the file, print them
  while((line = reader.readLine()) != null){ 
    System.out.println(line);
  }
} catch (IOException e) {
  e.printStackTrace();
}
Fortega
+2  A: 

Easiest option is to simply use the Apache Commons IO JAR and import the org.apache.commons.io.FileUtils class. There are many possibilities when using this class, but the most obvious would be as follows;

List<String> lines = FileUtils.readLines(new File("untitled.txt"));

It's that easy.

"Don't reinvent the wheel."

Can I ask what sort of content/data you will be reading from this file as there may be other (even simpler) possibilities?

i.e.

Properties

foo="bar"

String Tokens

foo,bar,fu,baz

Let me know if you require more details with any of the processes I've mentioned.

Alasdair
wll i was thinking that in the notepad file, i would have a code too each item, a String title, and a price. cant imagine id need more information than that on a menu
OVERTONE
So something like "1,'Foo',12.34"? Each item seperated by a new line?
Alasdair
no idea what foo bar command in java means but ill find out. seperated by a new line sounds right for the way i have it laid out. or possibly how many tabs there are.
OVERTONE
"foo bar fu baz" are commonly used words for changable values and are found in lots of examples. For each `String` in the `List`, you could then split up using Apache Commons Lang JAR and importing the StringUtils class. This allows you to further extract the values contained on each line through seperation. Have a look at the API and give it a try.
Alasdair