I"m a new Java student and i've got my program working except for one problem help

J

judith

My programs working now except for one problem when the program asks
you to enter a number and you enter anything other than a number it
outputs the error (Error , please enter the number again) but if it's
on Enter number 1 and i enter a letter like t when it goes back to
Enter a number it counts backward instead of asking to Enter number 1
again and i don't know what to do. I tried adding an i++ statement and
that didn't work. can anyone please help Judith

C:\>java program3JS
How many numbers do you want to enter?
3
Enter number 1
3
Enter number 2
t
Error, please enter the number again.
Enter number 1
t
Error, please enter the number again.
Enter number 0
3
Enter number 1
3
Enter number 2
3
Enter number 3
3
The average is 5.0

C:\>










// Author: Judith Spurlock
// Course: ITSE 2417
// Program No: 3
// Due Date: 10/20/2006
//
// Program Name: program3JS.java


import java.util.Scanner;
import java.util.InputMismatchException;

public class program3JS
{
public static void main (String[] args)
{

// Variable declarations

boolean error = true;
double average;
int sum = 0;
int n = 0;
int i = 0;
int num = 0;


Scanner keyboard = new Scanner(System.in);

// Loop until there is no error
do
{
try
{ error = false;
System.out.println("How many numbers do you want to enter?");
n = keyboard.nextInt();
if (n <= 0 )
throw new Exception ("Number must be greater than 0.");

}
catch (Exception e)
{


String message = e.getMessage();
System.out.println(message);
error = true;

}
}
while (error);

// Loop through each number and calculate the average

for ( i = 0; i < n; i ++)
{
// Repeat input as long as there is an error
do
{
try
{
error = false;
System.out.println("Enter number " + (i+1));
num = keyboard.nextInt();
sum += num;



}
catch(InputMismatchException e)
{

keyboard.nextLine();
System.out.println("Error, please enter the number again.");
error = true;
i ++;
}

}
while (error);
}



average = sum/n;

System.out.println ("The average is " + average);
}

}
 
O

Oliver Wong

judith said:
// Loop until there is no error
do
{
try
{ error = false;
System.out.println("How many numbers do you want to enter?");
n = keyboard.nextInt();
if (n <= 0 )
throw new Exception ("Number must be greater than 0.");

}
catch (Exception e)
{


String message = e.getMessage();
System.out.println(message);
error = true;

}
}
while (error);

You're using exceptions for control flow, which is generally considered
bad form. See http://java.sun.com/docs/books/tutorial/essential/exceptions/

This problem might be more manageable if you refactored it into several
smaller methods. If each method is very short, and does one thing, then it's
easier to see if the method contains errors, as opposed to one big method
which does lots of different things.

public static void main(String[] args) {
int numberOfNumbers = getNumberOfNumbers();
int sum = 0;
for (int i = 0; i < numberOfNumbers; i++) {
sum = sum + getIthNumber(i);
}
displaySumAverageAndOtherStatistics(sum, numberOfNumbers);
}

- Oliver
 
J

judith

The instructer wrote it this way. He wrote everything except the catch
statements so i don't know what to do because i can't change what he
wrote i just need the prompt to go back to the same Enter number 1 or
Enter number 2 if i make an error can some one help Oliver thanks for
your suggestion
Oliver said:
judith said:
// Loop until there is no error
do
{
try
{ error = false;
System.out.println("How many numbers do you want to enter?");
n = keyboard.nextInt();
if (n <= 0 )
throw new Exception ("Number must be greater than 0.");

}
catch (Exception e)
{


String message = e.getMessage();
System.out.println(message);
error = true;

}
}
while (error);

You're using exceptions for control flow, which is generally considered
bad form. See http://java.sun.com/docs/books/tutorial/essential/exceptions/

This problem might be more manageable if you refactored it into several
smaller methods. If each method is very short, and does one thing, then it's
easier to see if the method contains errors, as opposed to one big method
which does lots of different things.

public static void main(String[] args) {
int numberOfNumbers = getNumberOfNumbers();
int sum = 0;
for (int i = 0; i < numberOfNumbers; i++) {
sum = sum + getIthNumber(i);
}
displaySumAverageAndOtherStatistics(sum, numberOfNumbers);
}

- Oliver
 
O

Oliver Wong

[post-reordered]

judith said:
Oliver said:
This problem might be more manageable if you refactored it into
several
smaller methods. If each method is very short, and does one thing, then
it's
easier to see if the method contains errors, as opposed to one big method
which does lots of different things.

public static void main(String[] args) {
int numberOfNumbers = getNumberOfNumbers();
int sum = 0;
for (int i = 0; i < numberOfNumbers; i++) {
sum = sum + getIthNumber(i);
}
displaySumAverageAndOtherStatistics(sum, numberOfNumbers);
}
The instructer wrote it this way. He wrote everything except the catch
statements so i don't know what to do because i can't change what he
wrote i just need the prompt to go back to the same Enter number 1 or
Enter number 2 if i make an error can some one help Oliver thanks for
your suggestion

Okay, well what you could do, though it might be a bit advanced, is to
write the methods as described above anyway, and when everything is working,
inline the methods: http://www.refactoring.com/catalog/inlineMethod.html

Failing that, I can't think of any strategy other than just reading
through the code, and try to figure out why it isn't doing what you think
it's doing (possibly using a debugger, if you know how). Do you think your
program should work? If so, can you walk through the program, giving the
state of the program (i.e. what values all of the variables are) at each
point, and then run tests to determine if the program state really is what
you expect it to be? If you're not confident your program should work, which
parts are you the least confident about? Look there, and try to explain what
the program is doing to yourself. Then check whether what it's doing will
actually solve the problem you've been asked to solve. If you're not sure
what one specific part is doing, you can ask here.

- Oliver
 
J

judith

Now it's adding the numbers back up when i put in a i--; i can't figure
out what to do it's in the catch statement (InputMismatchException
e)catch(InputMismatchException e)
{

keyboard.nextLine();
System.out.println("Error, please enter the number again.");
error = true;
i--;

}
It's supposed to go back to Enter number 1 if there's an error like
entering a letter like i entered t



C:\>java program3JS
How many numbers do you want to enter?
3
Enter number 1
t
Error, please enter the number again.
Enter number 2
3
Enter number 3
3
The average is 2.0
The instructer wrote it this way. He wrote everything except the catch
statements so i don't know what to do because i can't change what he
wrote i just need the prompt to go back to the same Enter number 1 or
Enter number 2 if i make an error can some one help Oliver thanks for
your suggestion
Oliver said:
judith said:
// Loop until there is no error
do
{
try
{ error = false;
System.out.println("How many numbers do you want to enter?");
n = keyboard.nextInt();
if (n <= 0 )
throw new Exception ("Number must be greater than 0.");

}
catch (Exception e)
{


String message = e.getMessage();
System.out.println(message);
error = true;

}
}
while (error);

You're using exceptions for control flow, which is generally considered
bad form. See http://java.sun.com/docs/books/tutorial/essential/exceptions/

This problem might be more manageable if you refactored it into several
smaller methods. If each method is very short, and does one thing, then it's
easier to see if the method contains errors, as opposed to one big method
which does lots of different things.

public static void main(String[] args) {
int numberOfNumbers = getNumberOfNumbers();
int sum = 0;
for (int i = 0; i < numberOfNumbers; i++) {
sum = sum + getIthNumber(i);
}
displaySumAverageAndOtherStatistics(sum, numberOfNumbers);
}

- Oliver
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,533
Members
45,006
Latest member
LauraSkx64

Latest Threads

Top