This is an assignment I have and i'm running into problems on this part of it.

I need to assign the value I get from scanner input = to a variable in another class.

After I do that, I need to be able to call a method that will display the value along with other ones.

Like this,
Name: "value"
Age: "value"
ect...

import java.util.*;

public class main {
    public static void main(String[] args) {
        }

    public void setEmployee(){
        Employee emp = new Employee();
        Scanner scan = new Scanner(System.in);

        try {
            System.out.println("Enter Employee name. \n");
    //        String emp.setName(nameIn) = scan.next();   <-- trying to set the nameIn = to the scanner value
            System.out.println("Enter Employee age. \n");
            int ageIn = scan.nextInt();
            System.out.println("Enter Employee sex. \n");
            String sexIn = scan.next();
            System.out.println("Enter Employee salary. \n");
            double salaryIn = scan.nextDouble();
            System.out.println("Enter Employee job title. \n");
            String jobIn = scan.next();
        }

        catch(InputMismatchException ime)
        {
            System.out.println("Input mismatch, please enter the correct datatype.");
        }
    }

    public void getEmployee() {
        Employee emp = new Employee();
        System.out.println("Name: " + emp.getName());
        System.out.println("Age: " + emp.getAge());
        System.out.println("Sex: " + emp.getSex());
        System.out.println("Salary: " + emp.getSalary());
        System.out.println("Job : " + emp.getJob_title());
    }
}
public class Employee {

    private String name;
    String nameIn;
    private int age;
    int ageIn;
    private char sex;
    char sexIn;
    private double salary;
    double salaryIn;
    private String job_title;
    String jobIn;

    // constructor
 //   Employee(String nameIn, int ageIn, char sexIn, double salaryIn, String jobIn)
 //   {
 //       name = nameIn;
 //       age = ageIn;
 //       sex = sexIn;
 //       salary = salaryIn;
 //       job_title = jobIn;
 //   }

    // Accessors and Mutators
    public void setName(String nameIn)
    {
        name = nameIn;
    }
    public String getName()
    {
        return name;
    }

    public void setAge(int ageIn)
    {
        age = ageIn;
    }
    public int getAge()
    {
        return age;
    }

    public void setSex(char sexIn)
    {
        sex = sexIn;
    }
    public char getSex()
    {
        return sex;
    }

    public void setSalary(double salaryIn)
    {
        salary = salaryIn;
    }
    public double getSalary()
    {
        return salary;
    }

    public void setJob_title(String jobIn)
    {
        job_title = jobIn;
    }
    public String getJob_title()
    {
        return job_title;
    }
}

Anyone know how to do this? example or fix would be awesome. This is just a part of the assignment I'm having trouble with, I have the rest pretty much done.

in order to run, you 'll have to put code in your main method.
but your getEmployee method in that main class won't do you any good

Employee newEmp = new Employee();
/* this creates a new instance of the class Employee
when you now use the getters on that, it 'll only return null values, since you didn't put a name, ... in it
*/

below I'll put a piece of code that I think is what you're looking for

import java.util.*;

public class EmployeeTest {

    public static void main(String[] args) {
      	Employee emp = readEmployee();
      	System.out.println("============ END OF INPUT ============");
      	System.out.println("");
      	System.out.println("============ BEGIN OUTPUT ============");
      	
      	writeEmployee(emp);
    }
    
    public static Employee readEmployee(){
		Employee emp = new Employee();
		Scanner scan = new Scanner(System.in);
		try {
			// VERY IMPORTANT!!! you must use the setters of your Employee
			// class to store the input in it, otherwise, you're not using your Object 
			// properly and your application will not be able to retrieve the input
			// later on.
			// also, remember: next will only take the info until the first space, so if your
			// job title is 'Software Engineer', don't be surprised if it only prints 'Software'
			System.out.println("Enter Employee name. \n");
				emp.setName(scan.next());// <-- trying to set the nameIn = to the scanner value
			System.out.println("Enter Employee age. \n");
				emp.setAge(scan.nextInt());
			System.out.println("Enter Employee sex. \n");
			// since there is no nextChar =>
				String aid = scan.next();
				char[] charArr = aid.toCharArray();
				emp.setSex(charArr[0]);
			System.out.println("Enter Employee salary. \n");
				emp.setSalary(scan.nextDouble());
			System.out.println("Enter Employee job title. \n");
				emp.setJob_title(scan.next());
		}
		catch(InputMismatchException ime)
		{
			System.out.println("Input mismatch, please enter the correct datatype.");
		}
    	
    	return emp;
    }
    
    public static void writeEmployee(Employee emp){
    	
		System.out.println("Name: " + emp.getName());
		System.out.println("Age: " + emp.getAge());
		System.out.println("Sex: " + emp.getSex());
		System.out.println("Salary: " + emp.getSalary());
		System.out.println("Job : " + emp.getJob_title());
    }
}

Thanks a ton stultuske!

I have one more question, how can I assign multiple words to a value with scanner? To avoid losing info and to stop it from assigning the lost info to the next scanner.
Thanks!

Thanks a ton stultuske!

I have one more question, how can I assign multiple words to a value with scanner? To avoid losing info and to stop it from assigning the lost info to the next scanner.
Thanks!

Also, if I wanted to use the getName method for a subsystem to a consultant class, how would I do that? Right now I just have the method repeated in the consultant class, and it is used exactly like the one in the employee class - but I dont think that is the correct way to use it as a "subsystem" of employee.

Thanks!

Also, if I wanted to use the getName method for a subsystem to a consultant class, how would I do that? Right now I just have the method repeated in the consultant class, and it is used exactly like the one in the employee class - but I dont think that is the correct way to use it as a "subsystem" of employee.

Thanks!

to answer your fist question:
use a

while(scannerInput.hasNext()){
  System.out.print(scannerInput.next() + " ");
}

the hasNext() will check whether or not your input has more tokens left.

your second question..
maybe it's the late hour, or what ever, but I honestly have no idea what you're talking about with that subsystem.. :s (could be just me, though, 'm pretty tired for the moment)

to answer your fist question:
use a

while(scannerInput.hasNext()){
  System.out.print(scannerInput.next() + " ");
}

the hasNext() will check whether or not your input has more tokens left.

your second question..
maybe it's the late hour, or what ever, but I honestly have no idea what you're talking about with that subsystem.. :s (could be just me, though, 'm pretty tired for the moment)

Its probably me. I'm trying to set it up so you can enter info for a consultant class, which is like a sub group of an employee. The consultant uses some of the exact same methods as employee, such as name and salary.

So instead of remaking those methods, can I somehow use them through the employee class and assign the values to the consultant?

thanks :)

So I think I figured it out.. I just used inheritance between employee and consultant.

But back to the scanner question.

while(scannerInput.hasNext()) {
System.out.print(scannerInput.next() + " ");
}

How do I get this to work properly? I've been trying several different ways but none give the desired result.

I have a series of inputs I have to give, some will have more than one word inputs.
Thanks again :)

You can also use scannerInput.nextLine() to grab an entire line of text. I don't understand what exactly you're trying to do though. If you give us some short sample inputs, and tell us what you want to happen after those inputs are read in, that would be helpful...

You can also use scannerInput.nextLine() to grab an entire line of text. I don't understand what exactly you're trying to do though. If you give us some short sample inputs, and tell us what you want to happen after those inputs are read in, that would be helpful...

nextLine is what I needed :)

Its been a while since I've had to use the scanner.. so I forgot about that hehe, thank you.

I was just trying to get the line for some of the inputs like job title where it might be more than one word.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.