0

i dont know where to put my getValidString validation when i ask the user if he wants to judge another

public class JudgingApp
{
    public static void main(String args[])
    {
        Scanner sc= new Scanner(System.in);

        DecimalFormat df = new DecimalFormat("0.##");//format decimals to 2 decimal places

        //These variables hold the max and min numbers.
        double maxNumber = Double.MIN_VALUE; // Maximum number
        double minNumber = Double.MAX_VALUE; // Minimum number



        //allow user to execute the code multiple times
        String choice = "y";

        while(choice.equalsIgnoreCase("y"))
        //while(getValidString())
        {
        //variables
        double realTotalNumber,number,totalNumber = 0;

            //for loop to ask user to enter scores
            for(int n = 1; n < 9; n++)
            {
                //get input from user
                number = getNumberWithinRange(sc,"Enter score #" + n + ": ", 1.00, 10.00);

                //records max and min numbers
                if (number > maxNumber)
                {
                    maxNumber = number;
                }//end if
                if (number < minNumber)
                {
                    minNumber = number;
                }//end if

                //adds and records numbers inputted from user
                totalNumber += number;
            }//end forloop


        //total of user inputs not including the min and max numbers entered
        realTotalNumber = totalNumber -(minNumber+maxNumber);

        //prints out the following
        System.out.println("\nThe minimum score is " + df.format(minNumber));
        System.out.println("The maximum score is " + df.format(maxNumber));
        System.out.println("The total score is " + df.format(realTotalNumber));

        //prompts user to continue
        System.out.print("\nJudge another? (y/n): ");
        choice = sc.next();
        System.out.println();
        }//end while

    }//end MainMethod()



//method to validate the scores to be the correct data type and to be within the correct range
public static double getNumberWithinRange(Scanner sc, String prompt, double min, double max)
    {
        double d = 0;
        boolean isValid = false;

        while(isValid == false)
        {
            d = getDouble(sc, prompt);
            if(d< min)
                System.out.println("Error! Number must be "+ min +"0"+" or greater.");
            else if(d> max)
                System.out.println("Error! Number must be "+ max +"0"+" or less.");
            else
                isValid = true;//exit the loop because the value is within the specified range

        }// end while loop

        //return the valid data back to the caling mthod
        return d;
    }//end getNumberWithinRange()



//the getDouble() method gets input from the user, and validates, then returns to method getNumberWithinRange()
    public static double getDouble(Scanner sc, String prompt)
    {
        double d = 0;
        boolean isValid = false;

        while(isValid == false)
        {
            System.out.print(prompt);

            if(sc.hasNextDouble())
            {
                d = sc.nextDouble
                ();
                isValid = true;//get out of the loop
            }//end if
            else
            {
                System.out.println("Error! invalid value. Try again. ");
            }//end else

            //discard anything left on the line
            sc.nextLine();

        }//end while loop
        //return the valid data back to the calling method..getNumberWithinRange()
        return d;
    }//end getDouble()



    public static String getValidString(Scanner sc, String prompt)
    {
    String s = "";
    boolean isValid = false;
    while(isValid ==false)
    {
    s = getString(sc,prompt);
    if(s.equalsIgnoreCase("y") || s.equalsIgnoreCase("n"))
    {
    isValid = true;
    }//end if
    else
    {
    System.out.println("Error! Please enter y or n");
    }//end else
    }//end while
    return s;
    }//end getValidString

    public static String getString(Scanner sc, String prompt)
    {
    String s = "";
    boolean isValid = false;
    while (isValid == false);
    {
    System.out.print(prompt);
    s = sc.nextLine();
    if (s.equals(""))

    System.out.println("Error! Entry Required.");

    else

    isValid = true;

    }//end while
    return s;
}//end getString


}//end class

Edited by ~s.o.s~: Formatting

2
Contributors
1
Reply
7
Views
8 Years
Discussion Span
Last Post by kekkaishi
0

i dont know where to put my getValidString validation when i ask the user if he wants to judge another

public class JudgingApp
{
    public static void main(String args[])
    {
        Scanner sc= new Scanner(System.in);

        DecimalFormat df = new DecimalFormat("0.##");//format decimals to 2 decimal places

        //These variables hold the max and min numbers.
        double maxNumber = Double.MIN_VALUE; // Maximum number
        double minNumber = Double.MAX_VALUE; // Minimum number



        //allow user to execute the code multiple times
        String choice = "y";

        while(choice.equalsIgnoreCase("y"))
        //while(getValidString())
        {
        //variables
        double realTotalNumber,number,totalNumber = 0;

            //for loop to ask user to enter scores
            for(int n = 1; n < 9; n++)
            {
                //get input from user
                number = getNumberWithinRange(sc,"Enter score #" + n + ": ", 1.00, 10.00);

                //records max and min numbers
                if (number > maxNumber)
                {
                    maxNumber = number;
                }//end if
                if (number < minNumber)
                {
                    minNumber = number;
                }//end if

                //adds and records numbers inputted from user
                totalNumber += number;
            }//end forloop


        //total of user inputs not including the min and max numbers entered
        realTotalNumber = totalNumber -(minNumber+maxNumber);

        //prints out the following
        System.out.println("\nThe minimum score is " + df.format(minNumber));
        System.out.println("The maximum score is " + df.format(maxNumber));
        System.out.println("The total score is " + df.format(realTotalNumber));

        //prompts user to continue
        System.out.print("\nJudge another? (y/n): ");
        choice = sc.next();
        System.out.println();
        }//end while

    }//end MainMethod()



//method to validate the scores to be the correct data type and to be within the correct range
public static double getNumberWithinRange(Scanner sc, String prompt, double min, double max)
    {
        double d = 0;
        boolean isValid = false;

        while(isValid == false)
        {
            d = getDouble(sc, prompt);
            if(d< min)
                System.out.println("Error! Number must be "+ min +"0"+" or greater.");
            else if(d> max)
                System.out.println("Error! Number must be "+ max +"0"+" or less.");
            else
                isValid = true;//exit the loop because the value is within the specified range

        }// end while loop

        //return the valid data back to the caling mthod
        return d;
    }//end getNumberWithinRange()



//the getDouble() method gets input from the user, and validates, then returns to method getNumberWithinRange()
    public static double getDouble(Scanner sc, String prompt)
    {
        double d = 0;
        boolean isValid = false;

        while(isValid == false)
        {
            System.out.print(prompt);

            if(sc.hasNextDouble())
            {
                d = sc.nextDouble
                ();
                isValid = true;//get out of the loop
            }//end if
            else
            {
                System.out.println("Error! invalid value. Try again. ");
            }//end else

            //discard anything left on the line
            sc.nextLine();

        }//end while loop
        //return the valid data back to the calling method..getNumberWithinRange()
        return d;
    }//end getDouble()



    public static String getValidString(Scanner sc, String prompt)
    {
    String s = "";
    boolean isValid = false;
    while(isValid ==false)
    {
    s = getString(sc,prompt);
    if(s.equalsIgnoreCase("y") || s.equalsIgnoreCase("n"))
    {
    isValid = true;
    }//end if
    else
    {
    System.out.println("Error! Please enter y or n");
    }//end else
    }//end while
    return s;
    }//end getValidString

    public static String getString(Scanner sc, String prompt)
    {
    String s = "";
    boolean isValid = false;
    while (isValid == false);
    {
    System.out.print(prompt);
    s = sc.nextLine();
    if (s.equals(""))

    System.out.println("Error! Entry Required.");

    else

    isValid = true;

    }//end while
    return s;
}//end getString


}//end class

end quote.

hey, I am not an expert but i think i can help you out a little.
i get that u r trying to do is to identify if the entered value is a string. so may be this is where u need to call getValidString().

    //prompts user to continue
        System.out.print("\nJudge another? (y/n): ");
 // CALL TO getValidString(sc, "");

// U'd NEED TO rEMOVE THE NEXT LINE (choice = sc.next(); and the system.out.print...  

        choice = sc.next();
        System.out.println();
        }//end while

    }//end MainMethod()

Also, you might wanna change ur algorithm a bit since u r executing the program only if String choice = "y"; and whenever the getValidString is true, ur program starts all over again and even though u entered 'n' for 'Judge another', the program will ask to enter the scores again. the problem is choice string.

Also, if you are displaying the minimum n maximum value for each round, u might wanna change ur algorithm to display the correct values.
I hope this helps. nd if u r havin difficulties, lemme know, i have adjusted ur program n mine is working like a rainbow :cool:
and man, try using the code-tag. its in the top horizontal bar where u get bol italic n stuff. we sometimes call it the toolbar :D

Edited by mike_2000_17: Fixed formatting

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.