• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

err get and set methods and scanner class / JOptionPane

 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ooo thanks man
 
Ryan Webb
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Ryan Webb
 
Abhradeep Banerjee
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);



}

}
 
Marshal
Posts: 79707
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79707
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Weeds: because mother nature refuses to be your personal bitch. But this tiny ad is willing:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic