
-----------------------------------
illzidaneyou
Sun Jan 10, 2010 5:50 pm

Help with converting information from file to variable
-----------------------------------
I am trying to make it so that i can read a file and make certain lines into variable info
Say for example in the file i was reading, line 1 had the number 100 written on it
how would i make it so that an int variable will be equal to that 100?

I am also having another problem
when i use this code

String text = "Hello\n"
String text2 =  "What is your name?"
File file = new File("receipt.txt");
output = new BufferedWriter(new FileWriter(file));
output.write(text);
output.write(text2);
output.close();

it works fine it will print the text variable into a file but when i try to go to next line say by using \n
it will not, instead it will just continue to write in one big line and put a [] box whereever \n was.

Any help is appreciated

-----------------------------------
andrew.
Sun Jan 10, 2010 5:53 pm

RE:Help with converting information from file to variable
-----------------------------------
I'm just taking a guess here, but doesn't Windows have different line endings than other OSes? Like it's supposed to be something like "\r\n".

-----------------------------------
illzidaneyou
Sun Jan 10, 2010 5:55 pm

Re: Help with converting information from file to variable
-----------------------------------
hmm. Ill try that. yeah im using windows Xp.

-----------------------------------
illzidaneyou
Sun Jan 10, 2010 5:57 pm

RE:Help with converting information from file to variable
-----------------------------------
uhh it didnt work. It got rid of the box, but it still not printing onto the next line.

-----------------------------------
Euphoracle
Sun Jan 10, 2010 7:42 pm

RE:Help with converting information from file to variable
-----------------------------------
don't use \n, use the os's line terminator (this can be accessed by using writeln instead of write)

-----------------------------------
illzidaneyou
Sun Jan 10, 2010 7:50 pm

RE:Help with converting information from file to variable
-----------------------------------
oh so its kinda of like
System.out.println and 
System.out.print

K thnx

-----------------------------------
illzidaneyou
Sun Jan 10, 2010 8:04 pm

RE:Help with converting information from file to variable
-----------------------------------
it doesnt let me compile
it says somthingis wrong with the writeln command
Error message:

K:\IrfanISUinput.java:20: cannot find symbol
symbol  : method writeln(java.lang.String)
location: class java.io.Writer
	output.writeln(text);
	      ^
1 error
Finished

-----------------------------------
Euphoracle
Sun Jan 10, 2010 8:17 pm

RE:Help with converting information from file to variable
-----------------------------------
oh I saw 'File' and 'Writer' below it and thought you were using a FileWriter.  for a BufferedWriter, just do your .write without a \n, then follow it by a newLine()

this:

output.write("fish");
output.newLine();

-----------------------------------
illzidaneyou
Sun Jan 10, 2010 8:39 pm

RE:Help with converting information from file to variable
-----------------------------------
when i use that i get this error
K:\IrfanISUinput.java:22: cannot find symbol
symbol  : method newLine()
location: class java.io.Writer
	output.newLine();
	      ^
1 error
Finished

Just a note - Im using Public class in Jpad Pro 6.5

-----------------------------------
Barbarrosa
Sun Jan 10, 2010 9:22 pm

Re: Help with converting information from file to variable
-----------------------------------
You've initialized "output" as a Writer, not a BufferedWriter. The Writer class does not have newLine(), thus denying you access to this useful method. You should either change the type it's initialized with or cast it to a BufferedWriter when calling the method.


((BufferedWriter)output).newLine();


-----------------------------------
Euphoracle
Sun Jan 10, 2010 9:37 pm

RE:Help with converting information from file to variable
-----------------------------------
Or rather than casting it, you can correctly create a BufferedWriter.  Sorry you misinterpreted my previous post, it could have been better worded.

-----------------------------------
illzidaneyou
Sun Jan 10, 2010 9:54 pm

RE:Help with converting information from file to variable
-----------------------------------
Thanx guys its working now. Unfortunately this only solves my second problem. The first is the one that has me bamboozled. I have no idea.
Basically our teacher doesnt want us writing prices in our program. She wants it to be in a seperate file so that it can be changed by someone who doesnt know java
But i dunno how to take my read info and make a variable = to the read information.

-----------------------------------
Euphoracle
Sun Jan 10, 2010 10:38 pm

RE:Help with converting information from file to variable
-----------------------------------
Care to elaborate?  What are you using to 'read info'?

-----------------------------------
illzidaneyou
Sun Jan 10, 2010 10:52 pm

RE:Help with converting information from file to variable
-----------------------------------
This is my code for reading my file:

FileInputStream Pizzastream = new FileInputStream("PizzaCostInformation.txt");
	DataInputStream Bread = new DataInputStream(Pizzastream);
    BufferedReader Cheese = new BufferedReader(new InputStreamReader(Bread));

This is the file its reading:

Medium Pizza
9.25
Large Pizza
10.95
Extra-Large Pizza
13.50
Regular Topping
1.50
Gourmet Topping
1.95
Dipping Sauce
0.69

-----------------------------------
illzidaneyou
Sun Jan 10, 2010 10:53 pm

RE:Help with converting information from file to variable
-----------------------------------
So like how could I make a variable that is like
double Pizza = Line2 which is 9.25?
Is there a way to do that or am i approaching this the wrong way.
Cuz all my teacher said was that all the prices had to be loaded from a file.

-----------------------------------
Euphoracle
Sun Jan 10, 2010 10:58 pm

RE:Help with converting information from file to variable
-----------------------------------
Ok so, when you read in your information using readLine(), can you not simply store them in an array?  eg.


int num  = 6;
string

-----------------------------------
illzidaneyou
Sun Jan 10, 2010 11:27 pm

RE:Help with converting information from file to variable
-----------------------------------
i have no idea what an array is...We just started java this year and this is our last project i guess. We never talked about arrays.

-----------------------------------
Barbarrosa
Mon Jan 11, 2010 3:05 am

Re: Help with converting information from file to variable
-----------------------------------
Oh dear, arrays are important.

Well, anyway...

One method you can use to read in numbers is by creating an Integer object, like so:

Integer i = new Integer(/*put number here*/);

//or this way
Integer i = Integer.parseInt(/*put number here*/);


This is called a "wrapper class". It kinda treats the number like a normal object, and has some very useful functions. The above methods should be able to take both Strings and ints (I think so, anyway). If it cannot find a number, I believe it returns zero.

You can convert Integers back to ints by using this method:

i.intValue();


You can use similar methods with char (Character), float (Float), double (Double), etc.

-----------------------------------
illzidaneyou
Mon Jan 11, 2010 4:19 pm

RE:Help with converting information from file to variable
-----------------------------------
i asked my teacher and she said the only thing that we did that was like arrays was this:

public static void man (String str[]) {
String x = str[0]
or 
Int y = Integer.parseInt(str[1])

so does this work the same way?
Integer i =Integer.parseInt(/*put number here*/); 

where the number is the place value like 0 or 1

-----------------------------------
Euphoracle
Mon Jan 11, 2010 5:01 pm

RE:Help with converting information from file to variable
-----------------------------------
I really don't understand the context of the question you are answering.  Your questions are too vague for us to determine.  What do you want to do with the data?  Do you need to have it all in memory or are you doing a direct computation? ???  What?

int i = Integer.parseInt(s); consumes a string s and returns an int i which is the number represented by the string.  eg,

"64" -> 64
"3.14" -> error, a decimal isn't an integer
"ffqq" -> error, because ffqq isn;t an integer.

-----------------------------------
illzidaneyou
Mon Jan 11, 2010 10:00 pm

RE:Help with converting information from file to variable
-----------------------------------
Basically, what our teacher wants us to do is to make variables to hold prices, but rather than declaring those values in the program, she wants us to load those prices from another file. My file is PizzaCostInformation. 

This is because she is saying that if a company used our program, they wouldnt know how to change the prices in the program, but they could easily change the prices in the PizzaCostInformation file and those new prices will obviously change the prices within the program when it is read.

For my program, i only need to load 5 prices.
Hope that describes the problem well.

-----------------------------------
illzidaneyou
Mon Jan 11, 2010 10:03 pm

RE:Help with converting information from file to variable
-----------------------------------
And these variable prices will be used to determine the total cost of the pizza.
So like adding the values for a Medium Pizza, with 4 toppings and Dipping Sauce. I know how to add up these values, and write them to a file to look like a receipt.

Its just getting my variables values from a file that is making me crazy, like my teacher isnt describing well enough.

-----------------------------------
illzidaneyou
Mon Jan 11, 2010 10:07 pm

RE:Help with converting information from file to variable
-----------------------------------
This is the code that I use to read the file and display it:

FileInputStream Pizzastream = new FileInputStream("PizzaCostInformation.txt");
DataInputStream Bread = new DataInputStream(Pizzastream);
BufferedReader Cheese = new BufferedReader(new InputStreamReader(Bread));
	String Tomato;
	while ((Tomato = Cheese.readLine()) != null){
    System.out.println (Tomato);}

-----------------------------------
Euphoracle
Tue Jan 12, 2010 12:30 am

RE:Help with converting information from file to variable
-----------------------------------
EDIT::: If you are always buying all of the items, disregard what I have below.  You can just read in the even lines, add them, and then add tax or w/e, can't you?  This question still escapes me.

~~~~~~~~~

Well, then I guess you only need 5 variables.  If they are disallowing you from using arrays (dumb imho but it's their class) you are left with:

string item1 = ...
...
string item5 = ... ;

 

And then a large copypasta train of
item1 = input.readLine()
price1 = ...( input.readLine() )...
...
item5 = ...

etc.

Cheers.

-----------------------------------
illzidaneyou
Tue Jan 12, 2010 4:46 pm

RE:Help with converting information from file to variable
-----------------------------------
Do you mean like this?
	String MPiz = (input.readLine(2));
	String LPiz = (input.readLine(4)) ;
	String ELPiz = (input.readLine(6)) ;		
	String RTop = (input.readLine(8)) ;	
	String GTop = (input.readLine(10)); 			
	String DipSce = (input.readLine(12)) ;

I tried that way and got this error, one time for all 6 lines.

@MyProjects\IrfanISUInput6.java:34: cannot find symbol
symbol  : variable input
location: class IrfanISUInput6
	String MPiz = (input.readLine(2));
	               ^

-----------------------------------
illzidaneyou
Tue Jan 12, 2010 5:51 pm

RE:Help with converting information from file to variable
-----------------------------------
Nevermind guys, i found something else on the web. The only thing is that the code is deprecated, so I guess it wont work in some of the java editors.

THANKS FOR ALL YOUR HELP, never woulda made it this far. Now only one obstacle left to overcome, ROUNDING TO TWO DECIMAL PLACES :P
	FileInputStream fin;		
	try{
	fin = new FileInputStream ("PizzaCostInformation.txt");
	Medium =( new DataInputStream(fin).readLine());
	Medium2 = ( new DataInputStream(fin).readLine());
	Large = ( new DataInputStream(fin).readLine());
	Large2 = ( new DataInputStream(fin).readLine());
	XLarge = ( new DataInputStream(fin).readLine());
	XLarge2 = ( new DataInputStream(fin).readLine());
	RTopping = ( new DataInputStream(fin).readLine());
	RTopping2 = ( new DataInputStream(fin).readLine());
	GTopping = ( new DataInputStream(fin).readLine());
	GTopping2 = ( new DataInputStream(fin).readLine());
	DSauce = ( new DataInputStream(fin).readLine());
	DSauce2 = ( new DataInputStream(fin).readLine());
	fin.close();}
	catch (IOException e){
			System.err.println ("Unable to read from file");
			System.exit(-1);}
