I'm trying to write a small program in java, that'll calculate surface are and volume of a sphere, based on the radius of the sphere. These radii comes from .txt file with a single column of numbers.
I've tried searching a bit for this: Reading numbers in java
The code example looks a bit complicated for me as I'm not yet comfortable and experienced in reading java code. I've also tried reading this one:
Opening and reading numbers from a text file
I get confused of the 'try' keyword, among other things, what is it there for?
Where the second example says File("file.txt");
Do I put in the path to my text file?
If anyone can point me to a tutorial that'll take a beginner through these things, I'd very much like to know.
Here is my code so far:
import java.io.*;
// This class reads a text file (.txt) containing a single column of numbers
public class ReadFile {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String fileName = "/home/jacob/Java Exercises/Radii.txt";
Scanner sc = new Scanner(fileName);
}
}
Best regards,
Jacob Collstrup
Here's a small, simple snippet:
Scanner in = null;
try {
in = new Scanner(new File("C:\\Users\\Me\\Desktop\\rrr.txt"));
while(in.hasNextLine()) {
int radius = Integer.parseInt(in.nextLine());
System.out.println(radius);
// . . .
}
} catch(IOException ex) {
System.out.println("Error reading file!");
} finally {
if(in != null) {
in.close();
}
}
A try-catch
block is something used in Java to handle exceptions . You can read all about them, and why they are useful here: http://docs.oracle.com/javase/tutorial/essential/exceptions/
Of course, if you are using Java 7 or above, the previous code can be simplified using something called try-with-resources
. This is a another type of try
block, except this will automatically close any "autocloseable" streams for you, removing that ugly finally
section of the code:
try (Scanner in = new Scanner(new File("C:\\Users\\Me\\Desktop\\rrr.txt"))) {
while(in.hasNextLine()) {
int radius = Integer.parseInt(in.nextLine());
System.out.println(radius);
// . . .
}
} catch(IOException ex) {
System.out.println("Error reading file!");
}
You can read more about try-with-resources
here: http://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html
The rrr.txt
file should just have a single number on each line, something like this
10 20 30
- try/catch
and finally are the ways for handling the Exceptions
or Errors
(as both extends Throwable Class
) which arises while performing some work.
Eg: File I/O, Network Operation etc.
Scanner in = null;
try {
in = new Scanner(new File("C:\\Users\\Me\\Desktop\\rrr.txt"));
while(in.hasNextLine()) {
int radius = Integer.parseInt(in.nextLine()); // If this is
// not an integer
// NumberFormatException is thrown.
System.out.println(radius);
}
} catch(IOException ex) {
System.out.println("Error reading file!");
} catch(NumberFormatException ex){
System.out.println("Its not an integer");
}
finally {
if(in != null) {
in.close();
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.