I have a class and a public class and want to assign a variable from the class with user inputted data in the public class. Is it possible to do this in this way or would I have to move the variables.
This is my code
import java.util.Scanner;
class People {
String name;
int age;
String hair_colour;
String personality;
void confirm(){
System.out.println(name +" is " + age + " years old and has "
+ hair_colour + " hair and has a " + personality +" personality");
System.out.println("Is this correct? Y/N");
Scanner correct = new Scanner(System.in);
String reply = correct.nextLine();
if(reply=="Y" || reply=="y"){
System.out.println("Profile confirmed. Thank you.");
}
else {
System.out.println("Returning to profile creation");
}
}
}
public class PeopleProfile {
public static void main(String[]args){
Scanner input_details = new Scanner(System.in);
System.out.println("Please enter a name: ");
String given_name = input_details.nextLine();
System.out.println("Please enter a age: ");
int given_age = input_details.nextInt();
System.out.println("Please enter the persons hair colour: ");
String given_hair_colour = input_details.nextLine();
System.out.println("Please enter the persons personality: ");
String given_personality = input_details.nextLine();
}
}
Try this:
import java.util.Scanner;
class People {
String name;
int age;
String hairColour;
String personality;
void confirm() {
System.out.println(name + " is " + age + " years old and has " + hairColour + " hair and has a " + personality + " personality");
Scanner correct = new Scanner(System.in);
System.out.println("Is this correct? Y/N");
String reply = correct.nextLine();
if ("Y".equals(reply) || "y".equals(reply)) {
System.out.println("Profile confirmed. Thank you.");
} else {
System.out.println("Returning to profile creation");
}
}
}
public class PeopleProfile {
public static void main(String[] args) {
People boy = new People();
Scanner inputDetails = new Scanner(System.in);
System.out.println("Please enter a name: ");
boy.name = inputDetails.nextLine();
System.out.println("Please enter a age: ");
boy.age = Integer.parseInt(inputDetails.nextLine());
System.out.println("Please enter the persons hair colour: ");
boy.hairColour = inputDetails.nextLine();
System.out.println("Please enter the persons personality: ");
boy.personality = inputDetails.nextLine();
boy.confirm();
}
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments