Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Help with converting information from file to variable
Index -> Programming, Java -> Java Help
Goto page Previous  1, 2
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Euphoracle




PostPosted: Sun Jan 10, 2010 10:58 pm   Post subject: 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.

Java:

int num  = 6;
string[] product = new string[num];
double[] prices = /* fill this in */;

for (int i = 0;/*fill this in*/)
{
product[i] = /*fill this in
... */

}
Sponsor
Sponsor
Sponsor
sponsor
illzidaneyou




PostPosted: Sun Jan 10, 2010 11:27 pm   Post subject: 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




PostPosted: Mon Jan 11, 2010 3:05 am   Post subject: 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:
Java:

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:
Java:

i.intValue();


You can use similar methods with char (Character), float (Float), double (Double), etc.
illzidaneyou




PostPosted: Mon Jan 11, 2010 4:19 pm   Post subject: 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




PostPosted: Mon Jan 11, 2010 5:01 pm   Post subject: 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




PostPosted: Mon Jan 11, 2010 10:00 pm   Post subject: 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




PostPosted: Mon Jan 11, 2010 10:03 pm   Post subject: 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




PostPosted: Mon Jan 11, 2010 10:07 pm   Post subject: 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);}
Sponsor
Sponsor
Sponsor
sponsor
Euphoracle




PostPosted: Tue Jan 12, 2010 12:30 am   Post subject: 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 = ... ;

<same for prices>

And then a large copypasta train of
item1 = input.readLine()
price1 = ...( input.readLine() )...
...
item5 = ...

etc.

Cheers.
illzidaneyou




PostPosted: Tue Jan 12, 2010 4:46 pm   Post subject: 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




PostPosted: Tue Jan 12, 2010 5:51 pm   Post subject: 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 Razz
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);}
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 2 of 2  [ 26 Posts ]
Goto page Previous  1, 2
Jump to:   


Style:  
Search: