Using a try/catch statement?
I just started learning Java several days ago for an AP Computer Science course. As I quickly found, reading variables from a user is definitely not as easy in Java as in C++. So, I am trying to use what I have been told is a try/catch statement. I know nothing about how this works, and my teacher knows very little Java, so I'm a bit left on my own.
I am wondering if anyone can give me any pointers...is this even a legitimate way to read in variables? Does anyone know a better way? Or am I completely off the path with this?. Here is my code to find the area of a triangle, with the base and height given by the user.
package area_of_triangle;
import java.io.*;
public class Main {
public Main() {
}
public static void main(String[] args) {
DataInputStream in = new DataInputStream(System.in);
double base=0, height=0, area;
System.out.print("Enter the base of the triangle: ");
try {base = in.readDouble();}catch(IOException e){}
System.out.print("Enter the height of the triangle: ");
try {height = in.readDouble();}catch(IOException e){}
area = (base*height)/2.0;
System.out.println("The area is " + area);
}
}
I use the Scanner class for input.
I'd do something like this
import java.util.Scanner;
public class Something
{
private static Scanner in = new Scanner ( System.in );
public static void main ( String [ ] args )
{
double base = 0;
double height = 0;
double area = 0;
base = getDoubleValue ( "Enter the base of the triangle: " );
height = getDoubleValue ( "Enter the height of the triangle: " );
area = ( base * height ) / 2.0;
System.out.println ( "The area is " + area );
}
private static double getDoubleValue ( String message )
{
System.out.print ( message );
// Check if the input really is a double
if ( in.hasNextDouble () )
{
return in.nextDouble ();
} else
{
// Clear the bad input line
in.nextLine ();
return getDoubleValue ( message );
}
}
}
[url http://java.sun.com/j2se/1.5.0/docs/api/java/util/Scanner.html]Scanner[/url]Note that some constructors throw exceptions, so you will have to catch those.
> I'd do something like thisWhat a tool. You dont need to give them complete code every time.
> [url
> http://java.sun.com/j2se/1.5.0/docs/api/java/util/Scan
> ner.html]Scanner[/url]
> Note that some constructors throw exceptions, so you
> will have to catch those.
I'd still read this. If you turn in my code you better know exactly what's going on in there.
Why not?
Imagine the class he's in. Now imagine what happens if he turns in what I gave him? He can't turn this in.....well AP..maybe he could turn this in. IN anycase it's up to him to do with what I posted.
I don't see the big deal. I'm not his teacher. I'm just a dude on a message board.
First of all, I think its a she; and she'll never learn how to use it correctly if people spoon-feed her.
> First of all, I think its a she; and she'll never> learn how to use it correctly if people spoon-feed> her.I'm not an education aid. I just answer questions.
Her question wasnt to give her working code, it was to give her pointers.
Well I gave her working code. Why are we playing this game? She can use it or ignore it. I'm not forcing anything on her, just providing something.
Thanks very much for the help. And, no, I'm not going to go turning it in....I don't actually have to turn anything in for this class, I am doing it as an independent study and simply taking the AP test, while doing Games Progamming in C++ at school.
I don't see what the big deal is. The OP posted what appears to be working code. She wasn't spoon fed, she was shown an alternative way of achieving what she had already posted to begin with. Do you complain about people being spoon fed when someone informs them they can use x += 1 instead of x = x + 1? It's not far fetched with what's transpiring here.
Furthermore base, height, area, message and 'in' should all be final and the Scanner should be passed as a parameter. I'd also use a loop instead of recursion just because some users are particularly stupid and the stack is finite. This is of course just personal observations and of little semantic importance, especially with such a trivial example. I believe it to be a good practice to mark any variable that's not specifically intended to be changed as final. The only reason for passing the Scanner as a parameter is because in this particular example the method could be used with any Scanner instance and there's no reason that I can see to make it dependent upon the state of the class.
> Furthermore base, height, area, message and 'in'
> should all be final
You could make them final, but there's almost zero practical benefit to doing so here.
>and the Scanner should be passed
> as a parameter.
Sure it could be, but if the program only has one and is small like mine why bother.
>I'd also use a loop instead of
> recursion just because some users are particularly
> stupid and the stack is finite.
True. This was just a really easy way of doing it.
>This is of course
> just personal observations and of little semantic
> importance, especially with such a trivial example.
> I believe it to be a good practice to mark any
> variable that's not specifically intended to be
> changed as final. The only reason for passing the
> Scanner as a parameter is because in this particular
> example the method could be used with any Scanner
> instance and there's no reason that I can see to
> make it dependent upon the state of the class.
I agree with everyhting you've said....This is just a one off example for a silly example. I didn't add any enterpriseyness to it.:-)
Make no mistake I know it's all rather absurd with a trivial example but since the OP's question was more or less geared towards "pointers" about good coding practices and style I chimed in.
Well, you're right on all counts then.Only thing I don't do is mark my stuff final. Probably b/c I'm just lazy.
So I've tried to implement the scanner, but my compiler (Netbeans) does not seem to find the scanner class in java.util and will not create a new scanner. I tried importing java.util.* which skipped over the first error, but still would not create a new scanner. Is this possibly because I do not have the class installed? Or do I need to add the folder to the libraries in my project? Or something else altogether? Any help would be appreciated. Thanks
The Scanner class is new to version 1.5.0, you probably have an older version installed.
> Well, you're right on all counts then.
>
> Only thing I don't do is mark my stuff final.
> Probably b/c I'm just lazy.
I didn't used to but somehow Tony Morris through all his ranting and raving made some good points which escape me at the moment and I started doing it. It's kept me from changing a variable I shouldn't have been or thinking one wouldn't be changed when there was an assignment enough times to be worthwhile.
You need to be using Java 5 to use the scanner. I'm guessing you're using 1.4.Upgrade and it'll work.
I am fairly sure that I have 5.0 installed. Do I need a newer version?
It should work if you have 5.0 installed. Go to the command prompt and type in java -version
> > Well, you're right on all counts then.
> >
> > Only thing I don't do is mark my stuff final.
> > Probably b/c I'm just lazy.
>
> I didn't used to but somehow Tony Morris through all
> his ranting and raving made some good points which
> escape me at the moment and I started doing it. It's
> kept me from changing a variable I shouldn't have
> been or thinking one wouldn't be changed when there
> was an assignment enough times to be worthwhile.
Sigh...well maybe I should give it a shot.
Apparently it is 1.4, I guess I just have Netbeans 5.0 installed. Where might I find a newer version?
http://java.sun.com/javase/downloads/index.jspDownload JDK 5.0 Update 8.
I downloaded and installed the update, but I am still getting the same errors. Do I need to make some sort of configurations to Netbeans to make it recognize the updates? Thanks
Yes. Despite the fact that it seems entirely illogical it's not an actual update. That is to say it doesn't update your existing JDK or JRE, it actually installs a new JDK or JRE. You'll have to go into NetBeans and point it to the new JDK.
Thanks, that got rid of the errors just fine. However, something else seems to have come up. I try to run the program, but Netbeans just sits there saying that it is building the program, but never outputs or does anything. Any ideas? Thanks
I don't use NetBeans. Try the help file or a NetBeans forum.