I want to input a number and program makes # of input lines
Author |
Message |
Gerkin
|
Posted: Wed May 04, 2005 6:31 pm Post subject: I want to input a number and program makes # of input lines |
|
|
availabe. This may seem simple, but I have no idea how to this. I'm gonna use this as part of my project. |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
Hikaru79
|
Posted: Thu May 05, 2005 9:10 pm Post subject: (No subject) |
|
|
OK, this is a pretty small, simple thing, so I'll give it to you as a freebie, although it does sound kinda like homework...
|
|
|
|
|
![](images/spacer.gif) |
wtd
|
Posted: Thu May 05, 2005 9:50 pm Post subject: (No subject) |
|
|
I'm shocked. Why the separate declaration and initialization?
|
|
|
|
|
![](images/spacer.gif) |
Hikaru79
|
Posted: Thu May 05, 2005 9:57 pm Post subject: (No subject) |
|
|
wtd wrote: I'm shocked. Why the separate declaration and initialization?
Of 'numberOfLines'? Because I wanted to keep everything as simple as possible, not fight for every last reduced line count No offense to the original poster, but the question isn't exactly all that difficult, so I'm guessing he is a relative beginner to Java at least. Keeping declarations and intializations seperate just seemed like the more intuitive thing to do.
BTW, thank you for indenting and prettying up my code A lot easier on the eyes now! |
|
|
|
|
![](images/spacer.gif) |
wtd
|
Posted: Thu May 05, 2005 10:00 pm Post subject: (No subject) |
|
|
Hikaru79 wrote: Keeping declarations and intializations seperate just seemed like the more intuitive thing to do.
Not in Java, or any other language that supports that kind of syntax. |
|
|
|
|
![](images/spacer.gif) |
Gerkin
|
Posted: Sun May 08, 2005 5:03 pm Post subject: (No subject) |
|
|
What does try and catch do? Sorry for my inadequate knowledge of Java, I blame it on my com sci teacher. |
|
|
|
|
![](images/spacer.gif) |
JackTruong
|
Posted: Sun May 08, 2005 7:50 pm Post subject: (No subject) |
|
|
Try/Catch statements allow a program to continue running when something catastrophic could happen.
In this case, the try part, runs through the code inside the braces {} and if it finds any errors (IOException, bad input; NumberFormatException, bad number), it goes to code inside the catch braces and executes. |
|
|
|
|
![](images/spacer.gif) |
wtd
|
Posted: Sun May 08, 2005 9:28 pm Post subject: (No subject) |
|
|
Gerkin wrote: What does try and catch do? Sorry for my inadequate knowledge of Java, I blame it on my com sci teacher.
Well, there are a couple of basic approaches to handling errors, which is of course important, because errors are inevitable.
We can have functions return error codes indicating whether they failed or succeeded. But this has a few problems. First, it means we can't have a function return anything else, and secondly, it means a lot of extra code. Consider something like the following psuedocode.
code: | int result = open_file("foo.txt");
if (result == 0)
{
// read from file and close it
}
else
{
// print an error message
} |
The other approach is exceptions. Exceptions allow us to break out of the current level of the program with an identifiable error. In Java, we have a "try" block which "tries" some code. "catch" blocks are then provided to handle specific types of errors.
In the above example we handle either a fairly general IOException, and a NumberFormatException. The latter occurs when a String cannot be converted to an int, in this case.
The beauty of exceptions is that we don't have to directly mix error-handling code with the rest of our code. |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
|
|