1

i am writing a program that needs to have 5 methods other than main each method does something specific, main calls all other methods outputs warning to users about incorrect input, second method just pulls main input of how many variables user wants to process, third method checks to make sure the user's input is non-negative and also checks all user input later to make sure it is also non-negative, fourth method tests the numbers the user inputs and lastly the last method prints everything.. so far im not doing so great. i have the program ask for user input which works fine, the second method which is supposed to check and make sure the input is correct i cannot seem to get to work it either loops saying the input is right or wrong. as the code is now it loops saying the input is incorrect.

import java.util.Scanner;

public class tgore_perfect 
{
    private static int count;   
    public static void main ( String args [])
    {
        count = getNum ();
        boolean check = validateNum();
        while (check == false)
        {
            System.out.print ("Non-positive numbers are not allowed.\n");
            count = getNum();
        }
        if (check == true)
        {
            System.out.print("kk");
        }
    }

    public static int getNum () //gets amount of numbers to process
    {
    Scanner input = new Scanner ( System.in );
    int counter;

    System.out.println ("How many numbers would you like to test? ");
    counter = input.nextInt();
    return counter;
    }

    private static boolean validateNum() //checks user input
    { 
        if (count <= 0)
        {
            return false;
        }
        else
        {
            return true;
        }
    }
}

Doing this program through one main method is easy doing it through this many is just confusing to me. . .

Anders R. Bystrup
  • 15,729
  • 10
  • 59
  • 55
  • We can only see 2 methods here apart from main. – Rohit Jain Nov 15 '12 at 11:12
  • 2
    One problem is that you only call `validateNum` **before** you while loop. So if the first entry is not valid, check will be false forever... – assylias Nov 15 '12 at 11:13
  • There is no call to validateNum() inside your while loop. Since this is the case there is no way for the value check to be set to anything other than false (the initial value it gets from validateNum(), hence the loop is infinite. – zzzirk Nov 15 '12 at 11:34

2 Answers2

1

Your problem is that you aren't checking the value a second time. Try this:

//System.out.print(check);
while (validateNum() == false)
{
    System.out.print ("Non-positive numbers are not allowed.\n");
    count = getNum();
}
PearsonArtPhoto
  • 38,970
  • 17
  • 111
  • 142
0
boolean check = validateNum();
//System.out.print(check);
while (check == false)
{
    System.out.print ("Non-positive numbers are not allowed.\n");
    count = getNum();
    // reset check here 
}

You are not resetting check within loop, so if control enters within loop, it'll be for infinite loop.

One of the solutions could be:

while ( ! validateNum())
{
    System.out.print ("Non-positive numbers are not allowed.\n");
    count = getNum();
}
Azodious
  • 13,752
  • 1
  • 36
  • 71