Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

Have a look at Sun's Java Tutorial

"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".

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

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/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top