Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Swing / AWT / SWT and the fly likes Reading from text file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Reading from text file" Watch "Reading from text file" New topic
Author

Reading from text file

Heather Marie
Greenhorn

Joined: Sep 25, 2005
Posts: 8
Hey there,
I hope this is the right place to post this question. I'm new here - so forgive me if I'm posting in the wrong place.

Can someone give me some code for reading from a text file and assigning each line to a variable?

The text file looks like this:

200000
7.5
30

The text file is located at C:/JavaStuff
I'm using swing and trying to do some calculations on the numbers above and displaying the output in the GUI.

Thanks in advance - you guys have been awesome.

Heather
Mark Van Tuyl
Ranch Hand

Joined: Mar 22, 2002
Posts: 60


<a href="http://www.catb.org/~esr/faqs/smart-questions.html" target="_blank" rel="nofollow">How To Ask Smart Questions</a>
Heather Marie
Greenhorn

Joined: Sep 25, 2005
Posts: 8
Thanks - I think I'm moving in the right direction, but now I'm getting errors when I try to assign the array elements to specific variables. I need to do this so that I can call another reusable class that actually does the math I need. I need that class to recognize the variables.

Please look at my code below. Also, all the code that comes under this is giving errors (it worked before) so I think maybe part of my problem is the brackets - but I can't see it. Please help me.



________________________________________________________________________
public void rfile(){
try{
BufferedReader in = new BufferedReader(new FileReader("c:\\javastuff\\mortgage.txt"));
String line =" ";
int counter = 0;
double[] data = new double[3];
for(int x = 0; x < data.length; x++)
{
data[x] = Double.parseDouble(in.readLine());
if (data[x]=="data0")
{
double loanamt = data[x];
}
else if (data[x]=="data1")
{
double yrsnum = data[x];
}
else if (data[x]=="data2")
{
double rate = data[x];}
}

}
in.close(); //close the reader
}catch (Exception e) {
System.out.println("File Error!");
e.printStackTrace();
}

__________________________________________________________________________

And here are my error messages:

GuiMortgage.java [165:1] 'try' without 'catch' or 'finally'
try{
^
GuiMortgage.java [188:1] illegal start of type
}catch (Exception e) {
^
GuiMortgage.java [192:1] <identifier> expected
themath4array();}
^
GuiMortgage.java [193:1] 'class' or 'interface' expected
double loanamt = data1;
^
GuiMortgage.java [194:1] 'class' or 'interface' expected
double yrsnum = data2;
^
GuiMortgage.java [196:1] 'class' or 'interface' expected
public void themath4array(){}
^
GuiMortgage.java [207:1] 'class' or 'interface' expected
}
^
GuiMortgage.java [210:1] 'class' or 'interface' expected
^
GuiMortgage.java [26:1] GuiMortgage should be declared abstract; it does not define itemStateChanged(java.awt.event.ItemEvent) in GuiMortgage
public class GuiMortgage extends JFrame implements ActionListener, ItemListener {
^
GuiMortgage.java [173:1] operator == cannot be applied to double,java.lang.String
if (data[x]=="data0")
^
GuiMortgage.java [177:1] operator == cannot be applied to double,java.lang.String
else if (data[x]=="data1")
^
GuiMortgage.java [181:1] operator == cannot be applied to double,java.lang.String
else if (data[x]=="data2")
^
GuiMortgage.java [187:1] cannot resolve symbol
symbol : variable in
location: class GuiMortgage
in.close(); //close the reader
^
13 errors
Errors compiling GuiMortgage.
Heather Marie
Greenhorn

Joined: Sep 25, 2005
Posts: 8
Update! I found a missing bracket. Here's my code now:

_________________________________________________________________

public void rfile(){
try{
BufferedReader in = new BufferedReader(new FileReader("c:\\javastuff\\mortgage.txt"));
String line =" ";
int counter = 0;
double[] data = new double[3];
for(int x = 0; x < data.length; x++)
{
data[x] = Double.parseDouble(in.readLine());
if (data[x]=="data0")
{
double loanamt = data[x];
}
else if (data[x]=="data1")
{
double yrsnum = data[x];
}
else if (data[x]=="data2")
{
double rate = data[x];}
}
}
}
in.close(); //close the reader
}catch (Exception e) {
System.out.println("File Error!");
e.printStackTrace();
}

____________________________________________________________________

and here are the error messages I'm still getting - the bracket only removed three of the 13 messages:

GuiMortgage.java [165:1] 'try' without 'catch' or 'finally'
try{
^
GuiMortgage.java [187:1] <identifier> expected
in.close(); //close the reader
^
GuiMortgage.java [188:1] 'class' or 'interface' expected
}catch (Exception e) {
^
GuiMortgage.java [192:1] 'class' or 'interface' expected
themath4array();}
^
GuiMortgage.java [210:1] 'class' or 'interface' expected
^
GuiMortgage.java [187:1] package in does not exist
in.close(); //close the reader
^
GuiMortgage.java [26:1] GuiMortgage should be declared abstract; it does not define itemStateChanged(java.awt.event.ItemEvent) in GuiMortgage
public class GuiMortgage extends JFrame implements ActionListener, ItemListener {
^
GuiMortgage.java [173:1] operator == cannot be applied to double,java.lang.String
if (data[x]=="data0")
^
GuiMortgage.java [177:1] operator == cannot be applied to double,java.lang.String
else if (data[x]=="data1")
^
GuiMortgage.java [181:1] operator == cannot be applied to double,java.lang.String
else if (data[x]=="data2")
^
10 errors
Errors compiling GuiMortgage.
Heather Marie
Greenhorn

Joined: Sep 25, 2005
Posts: 8
Got it!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Reading from text file
 
Similar Threads
IOException when trying to load from binary file
korean characters handling
Java
How to read the lyrics(text) from .kar files?
Buffered File Reader