aspose file tools*
The moose likes Beginning Java and the fly likes err get and set methods and scanner class / JOptionPane Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "err get and set methods and scanner class / JOptionPane" Watch "err get and set methods and scanner class / JOptionPane" New topic
Author

err get and set methods and scanner class / JOptionPane

Abhradeep Banerjee
Ranch Hand

Joined: Dec 09, 2009
Posts: 71
Hi,

I just want to know how i am able to access the private attribute eg Private String name.

I would then want the user to key in his name using scanner class or JOptionPane.

Do i use a get and set method or?
Ryan Webb
Greenhorn

Joined: Dec 09, 2009
Posts: 28
Hi,

private access is only accessible to the class that contains the private member/attributes.
So this means you cannot directly access em.

however you may access it via public accessors. (Getters)


if you need to get the name use .getName() function.

Ryan Webb
philippines

frustrated to learn c/c++/objective-c
Abhradeep Banerjee
Ranch Hand

Joined: Dec 09, 2009
Posts: 71
thanks for your reply ryan

yes i did that part but im not sure what to put for the get method i pasted my code here


public class Customer{

private String name;
private String icNo;

public void setName(String n){
n=name;

}



public void setName(String i){
i=icNo;

}

}


import java.util.Scanner;
public class topmethod{

public static void main(String args[]){

Customer d = new Customer();
Scanner input = new Scanner(System.in);

System.out.println("Please Enter your name");
d.setName = input.nextLine();

//System.out.println("Please Enter your IC");
//d.icNo = input.nextLine();




}

}

thanks
Abhradeep
Soumil Shah
Ranch Hand

Joined: Jul 13, 2009
Posts: 54
1. no need to write two separate setter methods, you can use the one method with two arguments.
2. The way you are calling setter method is wrong. It should be

3. in get method, normally, we return the value as
Abhradeep Banerjee
Ranch Hand

Joined: Dec 09, 2009
Posts: 71
ooo thanks man
Ryan Webb
Greenhorn

Joined: Dec 09, 2009
Posts: 28


Ryan Webb
Abhradeep Banerjee
Ranch Hand

Joined: Dec 09, 2009
Posts: 71
hi thanks for your reply...
i did try it your way before i keep getting the same error msg

i have attached a screenshot of it


[Thumbnail for Untitled.jpg]

Abhradeep Banerjee
Ranch Hand

Joined: Dec 09, 2009
Posts: 71
if use Soumil Shah way i get this error msg when i run the javaclass file no problem with compiling tho.
i have posted the screenshot of it as well.


[Thumbnail for Untitled.jpg]

Abhradeep Banerjee
Ranch Hand

Joined: Dec 09, 2009
Posts: 71
These are my code

Customer.java ---

import java.util.Scanner;


public class Customer{

private String name;


public void setName(String n){
n=name;

}

public String getName() {
return name;
}



}

Topmethod.java-----

import java.util.Scanner;
public class topmethod{

public static void main(String args[]){

Customer d = new Customer();
Scanner input = new Scanner(System.in);

System.out.println("Please Enter your name");
d.setName(input.nextLine());

String name = d.getName();
System.out.println(name);



}

}
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Please avoid screenshots; they are much harder to read than copy-and-paste.
Abhradeep Banerjee
Ranch Hand

Joined: Dec 09, 2009
Posts: 71
err looks fine if you click on the download button

anyone knows the problem???
Abhradeep Banerjee
Ranch Hand

Joined: Dec 09, 2009
Posts: 71
ok i managed to solve the problem thanks those who helped

but i got another problem how do retrive info i stored in an array.. from another class

Database.java

public class Database{


private String CarList[] = { "Altis", "Vios", "Latio", "Murano", "Jazz", "Civic", "Stream", "Odyssey", "WRX", "Impressa" };




public String findCarByModel(){




}


}


Car.java

public class Car{

private String regno;
private String make;
private String model;
private int deposit;
private int rate;

//public static int calculateCost(int x) {

//return int;


//}

public void setMake(String m){

m=make;

}

public String getMake(){

Database myDatabase = new Database();



return make;
}

}


mainmethod.java
import java.util.Scanner;
public class mainmethod{

public static void main(String args[]){

Car myCar = new Car();

Scanner input = new Scanner(System.in);
System.out.print("Enter the model to rent: ");

myCar.setMake(input.nextLine());

String make = myCar.getMake();

if(make == null){
System.out.print("Model not is available");
}


}

}
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Please use a new thread for a new question. Please go back to your code and add code tags with the edit button; it is very difficult to read otherwise.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: err get and set methods and scanner class / JOptionPane