My resolution isn\'t printing our correct, it ask again for a new input but that
ID: 3783621 • Letter: M
Question
My resolution isn't printing our correct, it ask again for a new input but thats about it. it doesnt check to see if its the correct input. here is my code:
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: " + " userID: " + userID
+ " password: " + password + " Video resolution: " + resolution + " Ram Size: "
+ Ramsize + " HD Free Space: " + freespace + " Video Card Ram Size: " + videocard);
}
}
Explanation / Answer
HI Friend, Please find my correct code that follow the encapsulation rules.
Please let me know in case of any issue.
import java.util.Scanner;
public class encap
{
private String userID;
private String password;
private String resolution;
private int Ramsize;
private int freespace;
private int videocard;
//Get and Set methods
public String getUserID()
{
return userID;
}
public String getPassword()
{
return password;
}
public String getResolution()
{
return resolution;
}
public int getRamsize()
{
return Ramsize;
}
public int getFreespace()
{
return freespace;
}
public int getVideocard()
{
return videocard;
}
public void setUserID(String newuserID)
{
userID = newuserID;
}
public void setPassword(String newpassword)
{
password = newpassword;
}
public void setResolution(String newresolution)
{
resolution = newresolution;
}
public void setRamsize (int newRamsize)
{
Ramsize = newRamsize;
}
public void setFreespace (int newfreespace)
{
freespace = newfreespace;
}
public void setVideocard (int newvideocard)
{
videocard = newvideocard;
}
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
encap obj1 = new encap();
System.out.print("Please enter userID : ");
String userID = input.next();
obj1.setUserID(userID);
System.out.print("Please enter password : ");
String password = input.next();
obj1.setPassword(password);
System.out.print("Please enter video resolution: ");
while(true)
{
String resolution = input.next();
if (resolution.equals("800x600") || resolution.equals("1024x768") || resolution.equals("1152x900")){
obj1.setResolution(resolution);
break;
}
System.out.println("Information invalid, Please fill again");
}
while (true) {
System.out.print("Please enter RAM size : ");
String ramsize = input.next();
int intInputValue = 0;
try {
intInputValue = Integer.parseInt(ramsize);
obj1.setRamsize(intInputValue);
break;
} catch (NumberFormatException ne) {
System.out.println("Invalid Input! Integer required");
}
}
while (true) {
System.out.print("Please enter HD free space : ");
String freespace = input.next();
int intInputValue = 0;
try {
intInputValue = Integer.parseInt(freespace);
obj1.setFreespace(intInputValue);
break;
} catch (NumberFormatException ne) {
System.out.println("Invalid Input! Integer required");
}
}
while (true) {
System.out.print("Please enter video card RAM size: ");
String videocard = input.next();
int intInputValue = 0;
try {
intInputValue = Integer.parseInt(videocard);
obj1.setVideocard(intInputValue);
break;
} catch (NumberFormatException ne) {
System.out.println("Invalid Input! Integer required");
}
}
System.out.println("You have input the following information: " + " userID: " + obj1.getUserID()
+ " password: " + obj1.getPassword() + " Video resolution: " + obj1.getResolution() + " Ram Size: "
+ obj1.getRamsize() + " HD Free Space: " + obj1.getFreespace() +
" Video Card Ram Size: " + obj1.getVideocard());
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.