-1

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();
    }
}
Jack Flamp
  • 1,223
  • 1
  • 16
  • 32
Nathan
  • 21
  • 2
  • 2
    Your code is significantly harder to read than it would be if you'd formatted it conventionally - and ideally used conventional names. Additionally, see http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java. But note how currently you're not even creating an instance of `People`... – Jon Skeet Mar 03 '17 at 13:37
  • To clarify I'm trying to assign the given_name to name (as I got an error when trying to call name there) and so forth for all the of the input. – Nathan Mar 03 '17 at 13:38
  • 3
    @Nathan Without an instance of People there is no name to assign to. You first have to instanciate a People object before you can assign a name to that object. – OH GOD SPIDERS Mar 03 '17 at 13:41
  • 2
    Never compare Strings with == – Gyro Gearless Mar 03 '17 at 14:04

3 Answers3

1

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();
    }
}
Pavlo Plynko
  • 586
  • 9
  • 27
0

You can asign the variable by creating a setter for this certain variable.

So for example your property

String hair_colour;

shall have the following method

public void setHairColour(string hair_colourIn) {
   hair_colour = hair_colourIn;
}

You will call this property as any other method in its class

People people = new People(); 
people.setHairColour(-your input string -)

To get to a value from a different class, you need a getter In the given example property hair_colour this shall be as followed:

public string getHairColour() {
   return hair_colour;
}

So you eventually have:

People people = new People(); 
string hairColour = people.getHairColour();
MwBakker
  • 498
  • 5
  • 18
-1

Couldn't you overload a constructor for your People class ?
It would look like such :

class People {
String name;
int age;
String hair_colour;
String personality;

public People(String name, int age, String hair_colour, String personality){
    this.name = name;
    this.age = age;
    this.hair_colour = hair_colour;
    this.personality = 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.equals("Y") || reply.equals("y")){
        System.out.println("Profile confirmed. Thank you.");
    }
    else{
        System.out.println("Returning to profile creation");
    }
}
}

Then in your main you would call it as such :

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();
    People myPeople = new People(given_name, given_age, given_hair_colour, given_personality);
    myPeople.confirm();

}
Irindul
  • 398
  • 1
  • 6
  • 21