Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

err get and set methods and scanner class / JOptionPane

 
Abhradeep Banerjee
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Abhradeep Banerjee
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ooo thanks man
 
Ryan Webb
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Ryan Webb
 
Abhradeep Banerjee
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Untitled.jpg
[Thumbnail for Untitled.jpg]
 
Abhradeep Banerjee
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Untitled.jpg
[Thumbnail for Untitled.jpg]
 
Abhradeep Banerjee
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50277
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please avoid screenshots; they are much harder to read than copy-and-paste.
 
Abhradeep Banerjee
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
err looks fine if you click on the download button

anyone knows the problem???
 
Abhradeep Banerjee
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50277
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic