Java: calling a class variable and assigning it a value in the main

Nathan

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();
    }
}
Pavlo Plynko

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Calling a variable in main class

From Dev

Assigning a variable within a class with value of a method

From Dev

Assigning variable of Class<> Type implementing an interface in Java

From Java

Java 8: assigning a value from a stream to a variable?

From Java

Java: Assigning a variable its current value?

From Dev

Calling Main Method in Java Class From JSP

From Java

assigning class variable as default value to class method argument

From Dev

Python: Calling a class based on variable's value

From Dev

Question concerning assigning a new value to the member variable of a class element in the list

From Dev

Angular - The problem with directly assigning a value from the service to a variable in the class

From Dev

Insert value into class variable from main function

From Java

Java calling a void method in a different class to a main class?

From Dev

Calling a function in main class from different class in java

From Dev

Assigning a value to variable(nameOfVariable)

From Dev

Assigning value to global variable

From Dev

Assigning a random value to a variable

From Dev

Assigning ArrayList value into a Variable

From Dev

Calling a function of a class in the main

From Dev

Calling solution class in main

From Dev

Java - How to change a method variable in the calling class?

From Dev

Java - VariableDeclaratorID expected after this token, when assigning value to an instance variable

From Java

Assigning a default value to a final variable in case of an exception in Java

From Dev

Calling a subclass attribute from the main class using Visual Code Java

From Dev

assigning a variable to HTML selector , class

From Dev

Python: Assigning class method to a variable

From Dev

Assigning a String " value in java

From Dev

Assigning a value to an object in Java

From Dev

Calling navigator.javaEnabled() after assigning to a variable

From Dev

Calling an element from an array and assigning it to a variable?

Related Related

  1. 1

    Calling a variable in main class

  2. 2

    Assigning a variable within a class with value of a method

  3. 3

    Assigning variable of Class<> Type implementing an interface in Java

  4. 4

    Java 8: assigning a value from a stream to a variable?

  5. 5

    Java: Assigning a variable its current value?

  6. 6

    Calling Main Method in Java Class From JSP

  7. 7

    assigning class variable as default value to class method argument

  8. 8

    Python: Calling a class based on variable's value

  9. 9

    Question concerning assigning a new value to the member variable of a class element in the list

  10. 10

    Angular - The problem with directly assigning a value from the service to a variable in the class

  11. 11

    Insert value into class variable from main function

  12. 12

    Java calling a void method in a different class to a main class?

  13. 13

    Calling a function in main class from different class in java

  14. 14

    Assigning a value to variable(nameOfVariable)

  15. 15

    Assigning value to global variable

  16. 16

    Assigning a random value to a variable

  17. 17

    Assigning ArrayList value into a Variable

  18. 18

    Calling a function of a class in the main

  19. 19

    Calling solution class in main

  20. 20

    Java - How to change a method variable in the calling class?

  21. 21

    Java - VariableDeclaratorID expected after this token, when assigning value to an instance variable

  22. 22

    Assigning a default value to a final variable in case of an exception in Java

  23. 23

    Calling a subclass attribute from the main class using Visual Code Java

  24. 24

    assigning a variable to HTML selector , class

  25. 25

    Python: Assigning class method to a variable

  26. 26

    Assigning a String " value in java

  27. 27

    Assigning a value to an object in Java

  28. 28

    Calling navigator.javaEnabled() after assigning to a variable

  29. 29

    Calling an element from an array and assigning it to a variable?

HotTag

Archive