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

Discussion in 'Java' started by judith, Oct 16, 2006.

  1. judith

    judith Guest

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

    }
    judith, Oct 16, 2006
    #1
    1. Advertising

  2. judith

    Oliver Wong Guest

    "judith" <> wrote in message
    news:...
    >
    > // 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
    Oliver Wong, Oct 16, 2006
    #2
    1. Advertising

  3. judith

    judith Guest

    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 Wong wrote:
    > "judith" <> wrote in message
    > news:...
    > >
    > > // 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
    judith, Oct 16, 2006
    #3
  4. judith

    Oliver Wong Guest

    [post-reordered]

    "judith" <> wrote in message
    news:...
    >
    > Oliver Wong wrote:
    >>
    >> 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
    Oliver Wong, Oct 16, 2006
    #4
  5. judith

    judith Guest

    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

    judith wrote:
    > 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 Wong wrote:
    > > "judith" <> wrote in message
    > > news:...
    > > >
    > > > // 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
    judith, Oct 16, 2006
    #5
  6. judith

    schouery Guest

    Hi Judith, I removed the i++ inside the catch and everything works...

    judith wrote:
    > 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);
    > }
    >
    > }
    schouery, Oct 16, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Salerno
    Replies:
    20
    Views:
    849
    John Salerno
    Aug 11, 2006
  2. judith
    Replies:
    10
    Views:
    925
    John W. Kennedy
    Nov 21, 2006
  3. Fabio Z Tessitore

    who is simpler? try/except/else or try/except

    Fabio Z Tessitore, Aug 12, 2007, in forum: Python
    Replies:
    5
    Views:
    373
  4. Peng Yu
    Replies:
    1
    Views:
    515
    Steven D'Aprano
    Nov 18, 2009
  5. MRAB
    Replies:
    0
    Views:
    823
Loading...

Share This Page