Originally posted by: AgaBooga
Originally posted by: notfred
Here's your example
Thank You! I really appreciate your help! I'm gonna go back and understand what it means, but that is really helpful.
I have a few questions though.
1. Why does int number = -1; have to be used and what is its purpose?
2. Can you explain basically what each of the first 8 lines of the new code at the bottom do starting with public?
3. What can cause an IOException?
1) I declare an integer and initialize it to -1, why -1? I'll get to that in a second.
2)
public static int getInput(BufferedReader dataIn, String prompt){
This line declares a new method. You already have one methos declared (main) but your teacher may not have described how they work yet. Basically it's a section of code that can be reused from somewhere else. if you see earlier in the file I have lines like "amtQuarters = getInput(dataIn, "How many quarters are there?");" where the method I wrote is used. These are called "method calls" and each time one is run, it sends some information to the method, and gets different information back from the method, all in one line of code.
Info about my specific method:
for now you can ignore "public" and "static" as they're really more advanced topics that you'll cover later. "int" specifies the return type of the method. All that means is that the method is finished, the type of data that it will have created is an int. You can create methods with any return type you like, doubles or Strings for example.
"getInput" is the name of my method, it's there so I can call it later on.
The stuff in parentheses are arguments to the method. Arguments are data that the method requires in order to run. My method has two arguments: One is a bufferedReader that I name "dataIn" the other is a String that I name "prompt"
now, since the method is of type "int" you can use it in place of an integer.
Let's define a hypothetical integer:
int number = 1;
Or, we can do the same thing with our method:
int number = getInput(someBufferedReader, someString)
Since, the method is of type integer, we don't know what integer it will end up being when it's done executing, but we know that it will be SOME integer.
You can see that is what I did earlier in the program with lines like: amtQuarters = getInput(dataIn, "How many quarters are there?");
Now, what happens inside the method when it runs:
This requires a little more discussion of arguments...
I have the arguments:
bufferedReader dataIn
and:
String prompt
Using these as arguments is similar to having these two lines at the beginning of the method:
bufferedReader dataIn;
String prompt;
However, since they're arguments, they will already have data assigned to them. The data that they have assigned to them is dependent on what was used in the method call. Here's an example.
One of my method calls from your program:
amtQuarters = getInput(dataIn, "How many quarters are there?");
now you can see that the first argument is "dataIn". That is the "dataIn" that you declared on the 7th line of the program. It gets sent to my method, where I also call it "dataIn".
The second argument is the string "How many quarters are there?"
Now, referring to earlier when I said that arguments were kind of like variable declarations at the beginning of a method, if we use this example, we have these two variable declarations:
bufferedReader dataIn = dataIn // The one after the "=" is the one from the top of the program.
string prompt = "How many quarters are there?";
if you're confused about how we can have two different variables named "dataIn", it's an issue called "variable scope". It basically says that you can re-use variable names as long as they're inside different functions. There's a lot more to the topic of variable scope if you're interested.
Ok, so that's the discussion on arguments. Now what does my code do? Let's start here:
int number = -1;
This declares an integer and sets it to -1. You'll see why I used -1 in a minute.
String input = new String();
This makes a new, empty string for us to use.
while (number < 0){
This begins a while loop. A while loop will repeat itself as long as the condition inside the parentheses is TRUE. Basically, when you start a while loop, the computer executes the code in the parentheses. If it's true, it executes all the code inside the {} that follows the parentheses. When it gets to the closing }, it goes back to the parentheses, and checks again. If the condition is still true, it does it again. As soon as it NOT TRUE the computer skips the {} after the parentheses, and continues on to the next section of code.
Now, you see that my code inside the parentheses is "number < 0". When we start the while loop, "number" is set to "-1". The computer evaluates the code inside the parentheses: "-1 < 0". That is, obviously true, so the computer starts running the code inside the {}. The reason I initialized "number" to -1 is to force the while loop to run at least one time. If I had set number initially to 0 for example, the computer would have looked inside the parentheses and evaluated it like this: "0 < 0". That is not a true statement, so the computer would have skipped the while loop, which is not what I wanted.
try{
Try is used in exception handling. It is paired with a "catch" statement later in the code. I'll explain more when I get to catch.
System.out.println(prompt);
This just prints out the String that we got earlier as an argument.
input = dataIn.readLine();
This reads from the bufferedReader that we got as an argument, and assigns it to our string "input".
number = Integer.parseInt(input);
You did this earlier, it converts a String into an Integer. One important note is that parseInt will throw a NumberFormatException if the String it gets doesn't look like an integer. We'll get to this also when I cover "catch".
if(number < 0){
System.out.println("Error: You must enter a non-negative integer");
}
This checks to see if our number is less than 0. If it is, it prints an error message.
catch(NumberFormatException e){
Ok, so "catch" watches for exceptions. It looks at any of the code in a preceding "try" block, and if that code throws out any exceptions, the "catch" block will deal with them so that the program wont crash. That's what the "try" block is for, just deciding which code the "catch" block is listening to.
In our "catch" line, we're specifically looking for an exception of type NumberFormatException. If any are generated, this catch block of code will run instead of the program crashing.
Now, when you run the line "number = Integer.parseInt(input);" it has the possibility of generating one of these exceptions. If it does, then the computer skips ahead from the "number = Integer.parseInt(input);" line, directly to the "catch" line. Once we get to the catch block, we just print a friendly error message, and the program doesn't crash.
We have one more "catch" line that looks for IOException errors. Theline we have that might generate these errors is "input = dataIn.readLine();" If your bufferedReader had been attached to a file, it's possible that you'd get an IOException if the hard drive was unplugged in the middle of reading your file, or if someone deleted the file while you were reading it. Since your bufferedReader is attached to the terminal, it's unlikely that you'd get an IOException, but bufferedReader still wants you to make sure that you catch it in the event that one does occur.
Now, after we finish this "catch" block, we have the closing "}" for our while loop. When we get to this point, the computer checks to see if "number" is less than 0 again. If it's not, because someone entered a negative number, or they entered something that caused an exception, we go back to the top of the while loop and do it again. If someone entered a valid number, it's either equal to or greater than 0, and we continue on the next line.
return number;
"return" says "Ok, this method is done, take "number" (which, you'll notice is an integer, which is the same type as our method) and send it back to the place where this function was called.
So "number", whatever the user entered, is sent back to the line that called our method in the first place and the line:
amtQuarters = getInput(dataIn, "How many quarters are there?");
Becomes:
amtQuarters = "number" that our method just returned.
I hope that makes sense. I tried to write geared toward a beginner level. Damn that was a long post.