Basic Input/Output
I am having trouble with a seemingly simple program. Basically, I am trying to read in a file that I created with a previous program. I tried to generate the output such that it would be:
int double
string
int double
string
The content of these items is:
quantity cost
description
quantity cost
descrption
The file I am reading in looks ok visually, but it refuses to acknowledge my efforts to format the content as int, double and string. Therefore, I have just bypassed that little inconvenience for the moment and I am reading it all as string.
I will need to do some calculations, but for now I am just trying to get it to spit out the data as I have fed it in. The problem seems to be that I don't know how many words will be in the descpription and it keeps trying to break it with the space between the words.
I have included my code and both the inFile and outFile.
I will be very grateful for any assistance that can be provided.
++++++++++++++++++++++++++++++++++++++++++++++++++
String qty1, qty2, cost1, cost2, description1, description2;
// Create and associate the Scanner object with the input source //
Scanner inFile = new
Scanner(new FileReader("order.in"));
// Create a PrintWriter object //
PrintWriter outFile = new
PrintWriter("inventory.out");
// Display dialog box giving the user explanation //
JOptionPane.showMessageDialog(null,
"This program reads an input file that includes items received\n" +
"from a wholesaler and creates an output file for a gift shop.\n" +
"The output file includes the quantity and retail price per item\n" +
"in one line, and the description in the next line.","",
JOptionPane.INFORMATION_MESSAGE);
// Retrieve the information on the 1st item //
qty1 = inFile.next();
cost1 = inFile.next();
description1 = inFile.next();
// Retrieve the information on the 2nd item //
qty2 = inFile.next();
cost2 = inFile.next();
description2 = inFile.next();
//qty1
//ret1 = (cost1/qty1) * 2.4
// Output file //
outFile.println(qty1 + cost1);
outFile.println(description1);
outFile.println(qty2 + cost2);
outFile.println(description2);
// Display dialog box for inventory processed //
JOptionPane.showMessageDialog(null, "Inventory Processed", "",
JOptionPane.INFORMATION_MESSAGE);
// Close the files //
inFile.close();
outFile.close();
+++++++++++++++++++++++++++++++++++++++++
My order.in file looks like this:
4 12.84
Wine Stoppers
2 35.64
Silver Cheese Trays
My inventory.out file is looking like this:
412.84
Wine
Stoppers2
35.64
<< and a line 5 that just shouldn't exist >>
[2906 byte] By [
wdatskova] at [2007-10-3 5:09:55]

The PrintWriter description:
Print formatted representations of objects to a text-output stream.
The key here is a text-output stream hence your output file will be text.
If you want to output object state data to a file you will have to implement the Serializeable interface in your class.
Here is a small example that does this:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
/**
* <Class Description>
* @version 1.0.0
*/
public class SerializationDemo {
/**
* Creates a new instance of SerializationDemo
*
*
*/
public SerializationDemo() {
}
public static void main(String[] args) {
try {
MyClass obj1 = new MyClass("Jason", -7, 2.7e10);
MyClass obj2 = new MyClass("Hello", 89, 1.4e-10);
System.out.println("Object1: " + obj1);
System.out.println("Object3: " + obj2);
String pth = System.getProperty("user.dir");
// System.out.println("User.dir: " + pth); //Debug statement
FileOutputStream fos =
new FileOutputStream(pth + "/serial.dat");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(obj1);
obj1 = new MyClass();
oos.writeObject(obj1);
System.out.println("Object being writen: " + obj1);
oos.writeObject(obj2);
oos.flush();
oos.close();
oos = null;
fos.close();
fos = null;
obj1 = null;
obj2 = null;
} catch (Exception e) {
System.err.println("Exception during Serialization..");
e.printStackTrace(System.err);
System.exit(0);
}
// Deserialization
try {
MyClass obj1, obj2;
obj1 = obj2 = null;
FileInputStream fis =
new FileInputStream(System.getProperty("user.dir") + "/serial.dat");
ObjectInputStream ois = new ObjectInputStream(fis);
obj1 = (MyClass)ois.readObject();
obj2 = (MyClass)ois.readObject();
System.out.println("Object1: " + obj1);
System.out.println("Object2: " + obj2);
obj2 = (MyClass)ois.readObject();
System.out.println("Object3: " + obj2);
ois.close();
ois = null;
fis.close();
fis = null;
obj1 = null;
obj2 = null;
} catch (Exception e) {
System.err.println("Problem reading objects");
e.printStackTrace(System.err);
}
}
}
class MyClass implements Serializable {
public String s;
public int i;
public double d;
public MyClass() {
this("Default", 0, 0.0);
}
public String toString(){
return "String: " + s + "; Int: " + i + "; Double: " + d;
}
public MyClass(String s, int i, double d) {
this.s = s;
this.i = i;
this.d = d;
}
}
Have Fun,
JJ
Java_Jay: Thank you for responding so quickly. I wish that I could say that you didn't go over my head with that, but you are WAY over my head. Could you scale this back to remedial. I have only barely started and have not had much help thus far.
Let me understand your problem first. You want to be able to write to a file so that if you write an integer or some other number, it is written as binary data to the file. Correct? Also if you write textural data, such as a name or description to the same file, you want it represented as text. Correct?
JJ
That would be nice because it would probably make calculations on the other side easier. However, I think at this point I'm just trying to get it to spit out the individual pieces and then I'll convert them so that I can do the calculations. It's probably not the best way, but my "big picture" in the world of Java is really quite small.
I will need to do some calculations, but for now I am
just trying to get it to spit out the data as I have fed it in.
The problem seems to be that I don't know how many
words will be in the descpription and it keeps trying to
break it with the space between the words.
I just re read your original post. To solve the problem of "not knowing how many words will be in the description"
Since you input file is text data, one line representing both qty and price, and the next line representing the description you could do something like this:
String inputLine = "";
while (there is more data to read in) {
inputLine = inputfile.readLine;
// Use the Character wrapper class to test the first index of the inputLine string
if (the first character of the line is a number) {
parse the line into the integer variables
} else {
the description = the inpuutLine;
}
}
Does that help?
JJ
Message was edited by:
Java_Jay
Here is a junk example to test the first character.
public class Junk {
public static void main(String args[]) {
String inLine1 = "1 234.5";
String inLine2 = "This is a description";
// Test the first string
if (Character.isDigit(inLine1.charAt(0))) {
System.out.println("True Digit");
} else {
System.out.println("Description: " + inLine1);
}
// Test the second string
if (Character.isDigit(inLine2.charAt(0))) {
System.out.println("True is digit.");
} else {
System.out.println("Decsription: " + inLine2);
}
}
}
I just used two strings for demonstration, you will have to adapt the concept to fit in your context.
JJ
Message was edited by:
Java_Jay
Thanks for your help. I'll take some time to digest the information that you have given me and hopefully be able to get through this.
One problem is right here:
> Retrieve the information on the 1st item //
>qty1 = inFile.next();
> cost1 = inFile.next();
>description1 = inFile.next();
>
>// Retrieve the information on the 2nd item //
> qty2 = inFile.next();
>cost2 = inFile.next();
> description2 = inFile.next();
Description = inFile.next();
This will return the next token seperated by white space,
you could use ... inFile.nextLine();that will return
the last skipped line. Play around with that idea and the
solution may come to you.
I don't think that is a proper solution for the real world. But it serves the
purpose of the exerscise; to get familiar with some of the basics.
Have fun!