Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2006

Tip: Looking for answers? Try searching our database.

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

Thread view: 
judith - 16 Oct 2006 18:27 GMT
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);
    }
   
}
Oliver Wong - 16 Oct 2006 18:57 GMT
> // Loop until there is no error
> do
[quoted text clipped - 17 lines]
> }
> 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 - 16 Oct 2006 19:17 GMT
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

> > // Loop until there is no error
> > do
[quoted text clipped - 36 lines]
>
>     - Oliver
Oliver Wong - 16 Oct 2006 19:48 GMT
[post-reordered]

>>     This problem might be more manageable if you refactored it into
>> several
[quoted text clipped - 17 lines]
> 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
judith - 16 Oct 2006 21:03 GMT
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
[quoted text clipped - 42 lines]
> >
> >     - Oliver
schouery - 16 Oct 2006 22:28 GMT
Hi Judith, I removed the i++ inside the catch and everything works...

> 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
[quoted text clipped - 109 lines]
>    
> }


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.