Search code examples
javaencapsulation

used of loop and encapsulation


I'm trying to get the program to do: If the data entered is not accepted, request the information again (note: it is fine to request ALL the information again, it is not necessary to only request specific info to be re-entered, but you can if you would like).

For the program everything seem to run fine except for when its the resolution, it ask for another input but if the input isn't correct it just accept. I need it to keep running until the correct input is enter.

import java.util.Scanner;
public class encap 
{
    private static String userID;
    private static String password;
    private static String resolution;
    private static int Ramsize;
    private static int freespace;
    private static int videocard;

    //Get and Set methods

    public static String getuserID()
    {
        Scanner input = new Scanner(System.in);
        System.out.print("Please enter userID : ");
        userID = input.next();
        return userID;
    }
    public static String getpassword()
    {
        Scanner input = new Scanner(System.in);
        System.out.print("Please enter password : ");
        password = input.next();
        return password;
    }
    public static String getresolution()
    {
        Scanner input = new Scanner(System.in);
        System.out.print("Please enter video resolution: ");
        resolution = input.next();
        if (resolution.equals("800x600") || resolution.equals("1024x768") || resolution.equals("1152x900"));
        else
        {
            while(true)
            {
                System.out.println("Information invalid, Please fill again");
                String getresolution = input.next();
                if (resolution.equals("800x600") || resolution.equals("1024x768") || resolution.equals("1152x900"));
                break;
            }
        }
        return resolution;
    }

    public static int getRamsize()
    {
        Scanner input = new Scanner(System.in);
        System.out.print("Please enter RAM size : ");
        while(true)
        {
            if(input.hasNextInt())
            {
                Ramsize = input.nextInt();
                break;
            }
            else
            {
            input.nextLine();
            System.out.println("Invalid Input! Integer required");
            System.out.print("Please enter RAM size : ");
            }
        }
        return Ramsize;
    }
    public static int getfreespace()
    {
        Scanner input = new Scanner(System.in);
        System.out.print("Please enter HD free space : ");
        while(true)
        {
            if(input.hasNextInt())
            {
                freespace = input.nextInt();
                break;
            }
            else
            {
                input.nextLine();
                System.out.println("Invalid Input! Integer required");
                System.out.print("Please enter HD free space : ");

            }   
        }
        return freespace;
    }

    public static int getvideocard()
    {
        Scanner input = new Scanner(System.in);
        System.out.print("Please enter video card RAM size: ");
        while(true)
        {
            if(input.hasNextInt())
            {
                videocard = input.nextInt();
                break;
            }
            else
            {
                input.nextLine();
                System.out.println("Invalid Input! Integer required");
                System.out.print("Please enter video card RAM size: ");
            }   
        }
        return videocard;
    }

    public static void setuserID(String newuserID)
    {
        userID = newuserID;
    }
    public static void setpassword(String newpassword)
    {
        password = newpassword;
    }
    public static void setresolution(String newresolution) 
    {
        resolution = newresolution;
    }

    public static void setRamsize (int newRamsize)
    {
        Ramsize = newRamsize;
    }

    public static void setfreespace (int newfreespace)
    {
        freespace = newfreespace;
    }

    public static void setvideocard (int newvideocard)
    {
        videocard = newvideocard;
    }
    public static void main(String[] args)
    {
    setuserID(getuserID());
    setpassword(getpassword());
    setresolution(getresolution());
    setRamsize(getRamsize());
    setfreespace(getfreespace());
    setvideocard(getvideocard());
    System.out.println("You have input the following information: " + "\nuserID: " + userID 
            + "\npassword: " + password + "\nVideo resolution: " + resolution + "\nRam Size: " 
            + Ramsize + "\nHD Free Space: " + freespace + "\nVideo Card Ram Size: " + videocard);
    }
}

Solution

  • The problem is because you never do anything within your valid case scenario and you are using .next() for a single character instead of .nextLine() which grabs the entire input entered following an end of line character (return character)

    This will ask until the input entered satisfies your if condition.

    public static String getresolution()
    {
        String resolution;
        boolean validAnswer = false;
        Scanner input = new Scanner(System.in);
        HashSet<String> validResolutions = new HashSet<>();
        validResolutions.add("800x600"); 
        validResolutions.add("1024x768"); 
        validResolutions.add("1152x900");
        //add more resolutions if you want without having to create a bigger if check 
        //validResolutions.add("1400x1120");
    
        do {
            System.out.print("Please enter video resolution: ");
            resolution = input.nextLine().replaceAll(" ", "").replaceAll("\n", "");
            validAnswer = validResolutions.contains(resolution) ? true : false;
            if(!validAnswer)
                System.out.println("Incorrect resolution please try again");
        } while (!validAnswer);
    
        return resolution;
    }