how to get input from scanner to my private viarable string in another class?

this is my first assignment, how to get input from scanner to my private viarable string in another class?

  1. Create Author class as the description mentioned below.(i already done it)
  2. Create a test class called TestAuthor to test all the public methods in class Author. Below is the example of the output.

the questions

this is my author code

package assignment1;

public class Author {
    
    //attribute of the class
    private String name;
    private String email;
    private char gender;
    
    
    //constructor
    public Author(String name, String email, char gender) {
        
        this.name = name;
        this.email = email;
        this.gender = gender;
    }
    
    //methods
    public String getName() {
        
        return name;
    }
    
    public String getEmail() {
        
        return email;
    }
    
    public void setEmail(String newEmail) {
        
        this.email = newEmail;
        
    }
    
    public char getGender() {
        
        return gender;
        
    }
    //method to get data from author object 
    public String toString() {
        
        return "Author[name="+name+", email="+email+", gender="+gender+"]";
                
    }   

}

this is my testauthor and i get this error ‘The method getName() in the type Author is not applicable for the arguments (String)’

package assignment1;

import java.util.Scanner;  // Import the Scanner class


public class TestAuthor {

    public static void main (String[] args) {
        
        
        //test constructor 
        Author newAuthor = new Author();
        
         Scanner input = new Scanner(System.in);
         System.out.println("Please enter name : ");
         String name = input.nextLine();
            newAuthor.getName(name);
            System.out.println("Name : "+newAuthor.getName());
        
    }
    
    
    
    
    
}

TIA

Answer

thank you for the help, i got the answer this is my testAuthor file

package assignment1;

import java.util.Scanner;  // Import the Scanner class


public class TestAuthor {

    public static void main (String[] args) {
        
        //set variables()
        String name;
        String email;
        char gender;
        
        
        //Scanner input object
        Scanner input = new Scanner(System.in);
        
        
        //get user input
        System.out.println("Please enter Name : ");
        name= input.nextLine();
        
        System.out.println("Please enter Email : ");
        email= input.nextLine();
        
        System.out.println("Please enter Gender : ");
        gender= input.nextLine().charAt(0);
        
        
        //
        Author newAuthor = new Author(""+name,""+email,'f');
        name = newAuthor.getName();
        email = newAuthor.getEmail();
        gender = newAuthor.getGender();
        

        //output toString
        System.out.println(newAuthor.toString());
        
        
        //set new email
        System.out.println("Please change Email : ");
        String newEmail= input.nextLine();
        newAuthor.setEmail(newEmail);
        
        
        //output
        System.out.println("Name : "+newAuthor.getName());
        System.out.println("Email : "+newAuthor.getEmail());
        System.out.println("Gender : "+newAuthor.getGender());
        
        
    }
    
    
    
    
    
}

Leave a Reply

Your email address will not be published. Required fields are marked *