Click here to Skip to main content
15,886,689 members

Comments by kinderu (Top 4 by date)

kinderu 9-Aug-17 12:46pm View    
Thank you for your time Jochen Arndt!
kinderu 9-Aug-17 9:42am View    
import java.util.Scanner;

class FirstClass
{
private String firstName;
private String secondName;
public FirstClass(String firstName, String secondName)
{
this.firstName = firstName;
this.secondName = secondName;
}

public String getFirstName()
{
return firstName;
}

public void setFirstName()
{
Scanner in = new Scanner(System.in);
firstName = in.nextLine();
}

public String getSecondName()
{
return secondName;
}

public void setSecondName()
{
Scanner in = new Scanner(System.in);
secondName = in.nextLine();
}
};

public class MyClass
{
public static void main(String[] args)
{
FirstClass obj = new FirstClass("Dragu", "Stelian");
System.out.println("Name is: " +obj.getFirstName() + " " +obj.getSecondName());

System.out.print("Enter First Name: ");
obj.setFirstName();

System.out.print("Enter Second Name: ");
obj.setSecondName();

System.out.println("Name is: " +obj.getFirstName() + " " +obj.getSecondName());
} 
}


Still I have a question: Why is so bad to use char arrays ?
kinderu 9-Aug-17 9:35am View    
Deleted
package myclass;
import java.util.Scanner;

class FirstClass
{
private String firstName;
private String secondName;
public FirstClass(String firstName, String secondName)
{
this.firstName = firstName;
this.secondName = secondName;
}

public String getFirstName()
{
return firstName;
}

public void setFirstName()
{
Scanner in = new Scanner(System.in);
firstName = in.nextLine();
}

public String getSecondName()
{
return secondName;
}

public void setSecondName()
{
Scanner in = new Scanner(System.in);
secondName = in.nextLine();
}
};

public class MyClass
{
public static void main(String[] args)
{
FirstClass obj = new FirstClass("Dragu", "Stelian");
System.out.println("Name is: " +obj.getFirstName() + " " +obj.getSecondName());

System.out.print("Enter First Name: ");
obj.setFirstName();

System.out.print("Enter Second Name: ");
obj.setSecondName();

System.out.println("Name is: " +obj.getFirstName() + " " +obj.getSecondName());
}
}

Look, I made it.
Still I have a question: Why is so bad to use char arrays ?
kinderu 9-Aug-17 9:13am View    
Both codes are made by me(for the sake of practice).
Yes I did compiled both version, C++ code run ok, Java code no.
As you saw in the previous post, I said it would have been easier to use String data.

Ok, I changed the type of members.

package myclass;
import java.util.Scanner;

class FirstClass
{
    private String firstName;
    private String secondName;
    public FirstClass(String firstName, String secondName)
    {
        this.firstName = firstName;
        this.secondName = secondName;
    }

    public String getFirstName()
    {
        return firstName;
    }
    
    public void setFirstName(String firstName)
    {
       this.firstName = firstName; 
    }

    public String getSecondName()
    {
        return secondName;
    }
    
    public void setSecondName(String secondName)
    {
       this.secondName = secondName;
    }
};

public class MyClass
{
    public static void main(String[] args)
    {
        FirstClass obj = new FirstClass("Dragu", "Stelian");
        Scanner input = new Scanner(System.in);
        System.out.print("Enter First Name: ");
        obj.setFirstName = input.nextLine();
        
        System.out.print("Enter Second Name: ");
        obj.secondName = input.nextLine();
        
        System.out.println("Name is: " +obj.firstName + " " +obj.secondName);
    } 
}



So I get an error in the lines Obj.firstName = input.nextLine();
Obj.secondName = input.nextLine();
which is normal because firstName and secondName members are private.


I just want to input firstName and secondName from keyboard like in C++ code where I am using stream operator overloading.