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);

}

}

[1231 byte] By [twist_chicka] at [2007-10-3 5:21:56]
# 1
I use the Scanner class for input.
CaptainMorgan08a at 2007-7-14 23:28:55 > top of Java-index,Java Essentials,New To Java...
# 2

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 );

}

}

}

Norweeda at 2007-7-14 23:28:55 > top of Java-index,Java Essentials,New To Java...
# 3
[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.
CaptainMorgan08a at 2007-7-14 23:28:55 > top of Java-index,Java Essentials,New To Java...
# 4
> I'd do something like thisWhat a tool. You dont need to give them complete code every time.
CaptainMorgan08a at 2007-7-14 23:28:55 > top of Java-index,Java Essentials,New To Java...
# 5

> [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.

Norweeda at 2007-7-14 23:28:55 > top of Java-index,Java Essentials,New To Java...
# 6

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.

Norweeda at 2007-7-14 23:28:55 > top of Java-index,Java Essentials,New To Java...
# 7
First of all, I think its a she; and she'll never learn how to use it correctly if people spoon-feed her.
CaptainMorgan08a at 2007-7-14 23:28:55 > top of Java-index,Java Essentials,New To Java...
# 8
> 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.
Norweeda at 2007-7-14 23:28:55 > top of Java-index,Java Essentials,New To Java...
# 9
Her question wasnt to give her working code, it was to give her pointers.
CaptainMorgan08a at 2007-7-14 23:28:55 > top of Java-index,Java Essentials,New To Java...
# 10
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.
Norweeda at 2007-7-14 23:28:55 > top of Java-index,Java Essentials,New To Java...
# 11
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.
twist_chicka at 2007-7-14 23:28:55 > top of Java-index,Java Essentials,New To Java...
# 12

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.

kablaira at 2007-7-14 23:28:55 > top of Java-index,Java Essentials,New To Java...
# 13

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.

kablaira at 2007-7-14 23:28:55 > top of Java-index,Java Essentials,New To Java...
# 14

> 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.:-)

Norweeda at 2007-7-14 23:28:55 > top of Java-index,Java Essentials,New To Java...
# 15
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.
kablaira at 2007-7-21 11:00:46 > top of Java-index,Java Essentials,New To Java...
# 16
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.
Norweeda at 2007-7-21 11:00:46 > top of Java-index,Java Essentials,New To Java...
# 17

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

twist_chicka at 2007-7-21 11:00:46 > top of Java-index,Java Essentials,New To Java...
# 18
The Scanner class is new to version 1.5.0, you probably have an older version installed.
CaptainMorgan08a at 2007-7-21 11:00:46 > top of Java-index,Java Essentials,New To Java...
# 19

> 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.

kablaira at 2007-7-21 11:00:46 > top of Java-index,Java Essentials,New To Java...
# 20
You need to be using Java 5 to use the scanner. I'm guessing you're using 1.4.Upgrade and it'll work.
Norweeda at 2007-7-21 11:00:46 > top of Java-index,Java Essentials,New To Java...
# 21
I am fairly sure that I have 5.0 installed. Do I need a newer version?
twist_chicka at 2007-7-21 11:00:46 > top of Java-index,Java Essentials,New To Java...
# 22
It should work if you have 5.0 installed. Go to the command prompt and type in java -version
CaptainMorgan08a at 2007-7-21 11:00:46 > top of Java-index,Java Essentials,New To Java...
# 23

> > 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.

Norweeda at 2007-7-21 11:00:46 > top of Java-index,Java Essentials,New To Java...
# 24
Apparently it is 1.4, I guess I just have Netbeans 5.0 installed. Where might I find a newer version?
twist_chicka at 2007-7-21 11:00:46 > top of Java-index,Java Essentials,New To Java...
# 25
http://java.sun.com/javase/downloads/index.jspDownload JDK 5.0 Update 8.
CaptainMorgan08a at 2007-7-21 11:00:46 > top of Java-index,Java Essentials,New To Java...
# 26
Thank you
twist_chicka at 2007-7-21 11:00:46 > top of Java-index,Java Essentials,New To Java...
# 27
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
twist_chicka at 2007-7-21 11:00:46 > top of Java-index,Java Essentials,New To Java...
# 28
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.
kablaira at 2007-7-21 11:00:46 > top of Java-index,Java Essentials,New To Java...
# 29
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
twist_chicka at 2007-7-21 11:00:46 > top of Java-index,Java Essentials,New To Java...
# 30
I don't use NetBeans. Try the help file or a NetBeans forum.
kablaira at 2007-7-21 11:00:51 > top of Java-index,Java Essentials,New To Java...