Hello,
I'm currently stuck on this program and wondering if I can find assistance

This programs should prompt users to
enter number(s) (integer(s)) <enter> multiple scores on the same line
see a grade(s)<enter>
then see a sum of the grades entered (abcdf).

It also must use BufferedReader.

I'm not using any IDEs, just Notepad and command prompt.
This is what I have so far.

---------------------------------------------------------------------------
import java.io.*;
public class asg4 {
public static void main(String [] args) {
int integer;
int sumA = 0;
int sumB = 0;
int sumC = 0;
int sumD = 0;
int sumF = 0;


//Prompt user using system.out.println and then create your buffered reader here
System.out.println( "Please enter your score(s)");
BufferedReader br;
br = new BufferedReader( new InputStreamReader( System.in ) );
//String line = br.readline();
input = Integer.parseInt(br.readline());
//pass the buffered reader into a Integer.parseInt method here and call the readline()



if(input==-99) System.exit(1);


while(input != -99){


if(input>=90){
sumA++;
System.out.println(new Integer(input) + "      A");
}
else if(input >= 70){
sumB++;
System.out.println(new Integer(input) + "      B");
}
else if(input >= 50){
sumC++;
System.out.println(new Integer(input) + "      C");
}
else if(input >= 35){
sumD++;
System.out.println(new Integer(input) + "      D");
}
else{
sumF++;
System.out.println(new Integer(input) + "      F");
}


System.out.println("Please enter your score22");


//put a prompt here here (dont have to re-create a buffered reader, use the one above)
input = Integer.parseInt(br.readline());
//and a integer.parseINT method here  and call the readline() method off of it



}


System.out.println("The total number of A's is " + new Integer(sumA ));
System.out.println("The total number of B's is " + new Integer(sumB ));
System.out.println("The total number of C's is " + new Integer(sumC ));
System.out.println("The total number of D's is " + new Integer(sumD ));
System.out.println("The total number of F's is " + new Integer(sumF ));
}
}

Recommended Answers

All 15 Replies

What is the problem? Does it not compile? Compile but crash when your run it? Run successfully but give bad results? If so, what are the results?

It does not complie nor does it produce the desired results. I get like 15 errors: ex. cannot find symol. I wish i could copy and paste the CMD prompt.

You don't define the variable "input" anywhere. If you put this line at the top with your other variable declarations:

int input;

the vast majority of your errors disappear though you still have a couple left after that. The method "readLine" from BufferedReader is

readLine

not

readline

The 'L' in "readLine" is a capital L, not lower-case.

I have this coded.

import java.io.*;
public class asg4 {
public static void main(String [] args) {


int input;
int integer;
int sumA = 0;
int sumB = 0;
int sumC = 0;
int sumD = 0;
int sumF = 0;



//Prompt user using system.out.println and then create your buffered reader here
System.out.println( "Please enter your score or -99 to quit");
BufferedReader br;
br = new BufferedReader( new InputStreamReader( System.in ) );
//String line = br.readLine();
input = Integer.parseInt(br.readLine());
//pass the buffered reader into a Integer.parseInt method here and call the readline()



if(input==-99) System.exit(1);


while(input != -99){


if(input>=90){
sumA++;
System.out.println(new Integer(input) + "      A");
}
else if(input >= 70){
sumB++;
System.out.println(new Integer(input) + "      B");
}
else if(input >= 50){
sumC++;
System.out.println(new Integer(input) + "      C");
}
else if(input >= 35){
sumD++;
System.out.println(new Integer(input) + "      D");
}
else{
sumF++;
System.out.println(new Integer(input) + "      F");
}


System.out.println("Please enter your score22");


//put a prompt (IM THINKING THIS IS NOT A PRINTLINE, MAYBE A TRY CATCH?) here (dont have to re-create a buffered reader, use the one above)
input = Integer.parseInt(br.readLine());
//and a integer.parseINT method here  and call the readline() method off of it


}


System.out.println("The total number of A's is " + new Integer(sumA ));
System.out.println("The total number of B's is " + new Integer(sumB ));
System.out.println("The total number of C's is " + new Integer(sumC ));
System.out.println("The total number of D's is " + new Integer(sumD ));
System.out.println("The total number of F's is " + new Integer(sumF ));
}
}

and I get these 2 errors now.

asg4.java:19: unreported exception java.io.IOException; must be caught or declared to be thrown
input = Integer.parseInt(br.readLine());
^
asg4.java:51: unreported exception java.io.IOException; must be caught or declared to be thrown
input = Integer.parseInt(br.readLine());
^

I've been running different versions of this program but I can't erase these errors and get a successful run.

I have this coded.

and I get these 2 errors now.

asg4.java:19: unreported exception java.io.IOException; must be caught or declared to be thrown
input = Integer.parseInt(br.readLine());
^
asg4.java:51: unreported exception java.io.IOException; must be caught or declared to be thrown
input = Integer.parseInt(br.readLine());
^
I've been running different versions of this program but I can't erase these errors and get a successful run.

Yeah, readLine throws an IOException. See this link:
http://java.sun.com/j2se/1.4.2/docs/api/java/io/BufferedReader.html#readLine()

Whenever a piece of code gives you an error with the word "exception" in it or the word "caught" or "declared", check the function specification on Sun's Java websites for that method and chances are it'll say something like:
"throws IOException" in its text. IOException is not the only type of exception, but it's the relevant one in this case. Check out this link:
http://java.sun.com/docs/books/tutorial/essential/exceptions/

Say a line of code is giving you that error. In your case,

input = Integer.parseInt(br.readLine());

Try surrounding that line of code with brackets and sticking the word "try" in front, like this:

try
{
     input = Integer.parseInt(br.readLine());
}

In your case, the relevant Exception is IOException, so add this:

catch(IOException ex)  // "ex" can be any variable name.  I chose
{                      // "ex" as short for "exception".
    // code here to handle exception.  Can be blank.
}

after the code above. Basically, when the code runs, it will attempt to do the code in the "try" part. If successful, the "catch" code is never executed. You can also put code there to execute in case something goes wrong (i.e. end the program early), but you don't have to.

Should the TRY CATCH
be utilized with the "input = Integer.parseInt(br.readLine());" at the top or the one at the bottom of the source code?

What var. should I use in the (IOException ??)

Should the TRY CATCH
be utilized with the "input = Integer.parseInt(br.readLine());" at the top or the one at the bottom of the source code?

What var. should I use in the (IOException ??)

Both of them. In my example, I used the variable name "ex". As I mentioned, call it whatever you like, but generally people call it "ex" because it's descriptive. So something like this each time you use the readLine function:

try
{
     input = Integer.parseInt(br.readLine());
}
catch(IOException ex)
{
    // code here to handle exception.  Can be blank.
}

Note: Java might now complain that you haven't initialized interest. Sometimes it does that when you add the brackets like I did here, so just initialize it to anything at the top of the code. It'll get written over by the readLine statements but Java is hyper-vigilant about initialization and so you have to do it to make Java happy even though other languages like C++ wouldn't complain.

I'm getting that error you said
How do I initialize it to anything at the top of the code?
I thought I did?

I'm getting that error you said
How do I initialize it to anything at the top of the code?
I thought I did?

You have defined/declared input, but you have not initialized it. Initializing a variable is giving a variable a value. Declaring it is giving it a name and telling Java what kind of variable it is. In your code below:

int input;
int integer;
int sumA = 0;
int sumB = 0;
int sumC = 0;
int sumD = 0;
int sumF = 0;

you have declared AND intialized sumA, sumB, sumC, sumD, and sumF. You have assigned them all initial values of 0. You have declared variables input and integer, but you have NOT initialized them. You have not given them values so it is impossible to know what is stored in them, probably complete jibberish. Java is mindful of the Garbage In, Garbage Out principal so it is hyper-vigilant (overly vigilant in my opinion) about requiring you to initalize if there is any possible doubt on its part that not initializing might potentially cause trouble. So to initialize input and integer, simply change from this:

int input;
int integer;

to this,

int input = 0;
int integer = 0;

as you did with the other five variables.

Ok, thank you.
It worked.

How do I catch an exception?

I have this

try
{
input = Integer.parseInt(br.readLine());
}
catch(IOException ex)
{
System.out.println( "enter digits");
// code here to handle exception.  Can be blank.
}

and when the user enters a letter value or something else I want a prompt to come up and say "Enter Digits"

Ok, thank you.
It worked.

How do I catch an exception?

I have this

try
{
input = Integer.parseInt(br.readLine());
}
catch(IOException ex)
{
System.out.println( "enter digits");
// code here to handle exception. Can be blank.
}

and when the user enters a letter value or something else I want a prompt to come up and say "Enter Digits"

Well you would not put the output there because if the user enters a letter, that would not result in an IOException. The readLine command would have been successful. It returns a string. The user types in "A", the readLine returns "A". It is successful, there has been no IO error, so no IOException will be caught.

If the user enters a letter, what will fail is the parseInt function. That throws a NumberFormatException, not an IOException. So add another catch block of code after the IOException block, like so:

try
{
     input = Integer.parseInt(br.readLine());
}
catch(IOException ex)
{
   System.out.println ("readLine function failed");
}
catch(NumberFormatException ex)
{
   System.out.println ("parseInt function failed");
   // code for handling letter input here
}

The computer, when there is an exception, figures out what type of exception it was, then goes to the corresponding "catch" function to "handle" that exception.

See parseInt link:
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Integer.html#parseInt(java.lang.String)

It's working great.
Thanks

When I enter non numerical digits I get the error message parseInt function failed

How would it be possible to get the error mesage readLine function failed?

I entered every input i can imagine but i always get parseInt function failed

import java.io.*;
public class asg4 {
public static void main(String [] args) {


int input = 0;
int integer = 0;
int sumA = 0;
int sumB = 0;
int sumC = 0;
int sumD = 0;
int sumF = 0;



//Prompt user to enter data and give instructions to exit
System.out.println( "Dhell box user please enter your score and/or -99 to quit");
BufferedReader br;
br = new BufferedReader( new InputStreamReader( System.in ) );


try
{
input = Integer.parseInt(br.readLine());// Integer.parseInt method here which calls the readline() method off of it


}
catch(IOException ex )
{
System.out.println ("readLine function failed");
}
catch(NumberFormatException ex){


System.out.println ("parseInt function failed");


}



if(input==-99) System.exit(1);// when the user enters "-99" the program will exit.


while(input != -99){


if(input>=90){
sumA++;
System.out.println(new Integer(input) + "      A");
}
else if(input >= 70){
sumB++;
System.out.println(new Integer(input) + "      B");
}
else if(input >= 50){
sumC++;
System.out.println(new Integer(input) + "      C");
}
else if(input >= 35){
sumD++;
System.out.println(new Integer(input) + "      D");
}
else{
sumF++;
System.out.println(new Integer(input) + "      F");
}


System.out.println("Please Enter Digits");


try
{
input = Integer.parseInt(br.readLine());// Integer.parseInt method here which calls the readline() method off of it
}
catch(IOException ex)
{


System.out.println ("readLine function failed");
}
catch(NumberFormatException ex)
{


System.out.println ("parseInt function failed");


}
}



System.out.println("The total number of A's is " + new Integer(sumA ));
System.out.println("The total number of B's is " + new Integer(sumB ));
System.out.println("The total number of C's is " + new Integer(sumC ));
System.out.println("The total number of D's is " + new Integer(sumD ));
System.out.println("The total number of F's is " + new Integer(sumF ));
}
}

You can use code tags to delineate your code:

[code=JAVA] // paste your code here

[/code]
It leaves the spacing/indentation in, highlights key words, and assigns line numbers for readability. The spacing's already been stripped out of yours so it won't help there, but for future reference you'll want to use the code tags.

import java.io.*;
public class asg4 {
public static void main(String [] args) {

int input = 0;
int integer = 0;
int sumA = 0;
int sumB = 0;
int sumC = 0;
int sumD = 0;
int sumF = 0;


//Prompt user to enter data and give instructions to exit
System.out.println( "Dhell box user please enter your score and/or -99 to quit");
BufferedReader br;
br = new BufferedReader( new InputStreamReader( System.in ) );



try
{
input = Integer.parseInt(br.readLine());// Integer.parseInt method here which calls the readline() method off of it

}
catch(IOException ex )
{
System.out.println ("readLine function failed");
}
catch(NumberFormatException ex){

System.out.println ("parseInt function failed");

}


if(input==-99) System.exit(1);// when the user enters "-99" the program will exit.

while(input != -99){

if(input>=90){
sumA++;
System.out.println(new Integer(input) + " A");
}
else if(input >= 70){
sumB++;
System.out.println(new Integer(input) + " B");
}
else if(input >= 50){
sumC++;
System.out.println(new Integer(input) + " C");
}
else if(input >= 35){
sumD++;
System.out.println(new Integer(input) + " D");
}
else{
sumF++;
System.out.println(new Integer(input) + " F");
}

System.out.println("Please Enter Digits");

try
{
input = Integer.parseInt(br.readLine());// Integer.parseInt method here which calls the readline() method off of it
}
catch(IOException ex)
{

System.out.println ("readLine function failed");
}
catch(NumberFormatException ex)
{

System.out.println ("parseInt function failed");

}
}


System.out.println("The total number of A's is " + new Integer(sumA ));
System.out.println("The total number of B's is " + new Integer(sumB ));
System.out.println("The total number of C's is " + new Integer(sumC ));
System.out.println("The total number of D's is " + new Integer(sumD ));
System.out.println("The total number of F's is " + new Integer(sumF ));
}
}

As to your question, I'm not sure of an easy way to purposefully create an IOException. There may be one. Usually it's something you DON'T want. I'm not sure if you're doing this for a class or what, but unless it's a fairly advanced, specialized class, I can't imagine why you would want to. Basically you could get an IOException if something went wrong with the keyboard or behind the scenes in the Operating System or something weird like that. I can't think of any input a user could enter off of the top of my head that could result in an IOException. There may be some. And it's highly unlikely you would want to try to get an IOException on purpose. You want to check for bad input elsewhere, as you are doing. An IO error would be something like you type in the word "house" and somehow the computer couldn't recognize the letters coming in from the keyboard for whatever reason. All this takes place before the input even reaches your program. There's probably other ways one could get an IOException, none of them really applicable to your program I don't think. I really wouldn't worry about it unless you have to. It's a GOOD thing thing that you can't get one. If you want to be extra diligent, I'd code it to display some error message and exit the program inside that code. I doubt anything inside those IOException brackets will ever be executed.

It's working great.
Thanks


When I enter non numerical digits I get the error message "parseInt function failed"

How would it be possible to get the error mesage "readLine function failed" ?

try
	{
    		input = Integer.parseInt(br.readLine());// Integer.parseInt method here which calls the readline() method off of it
		
	}
		catch(IOException ex )
	{
   			System.out.println ("readLine function failed");
	}
		catch(NumberFormatException ex){
			
   			System.out.println ("parseInt function failed");
   
	}

Well ... you could do that like this:

try
	{input = Integer.parseInt(br.readLine());
	}catch(IOException ex )
	{System.out.println ("readLine function failed");
	}catch(NumberFormatException ex){
	System.out.println ("readLine function failed");
                }

but why would you want that? it's not the readLine() method that throws the Exception. that method is working just fine. it's like you say, when you enter non-numerical values, the parsing to an integer fails. readLine doesn't care whether or not the value you enter is numerical or not, but it's the parsing that needs to find a value it can work with.

it's about the same as giving a human being a note that says:

solve this:

word + 6 = ...
unless 'word' is connected to some numerical value which is known to that person, he will have no idea at all what to think of that, except maybe that the one who gave him that assignment isn't truly a rocket scientist :)

hahahahai kmsta kns

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.