-3
Haj_Omra pr =new Promises( "Reem", 19,"10089", "italian","9/8/8" ,  " " );

//here i have to add information

but i want user to input from object , how can i do it ? how can i use the Scanner ?

i tried to put scanner inside the class but it's doesn't work ?

Class Check{
public void Sc(String ID){
Scanner input = new Scanner(System.in);
        System.out.print("Please enter a ID: ");
}

please any idea ??

Reem
  • 11
  • 1
  • 7
  • *i want user to input from object* what do you mean? The user can only input stuff from the keyboard AFAIK, otherwise you might need to read some input from a file or a stream. – Joffrey Apr 29 '14 at 09:24
  • You might want to check this answer out: http://stackoverflow.com/a/5287561/1540818 It is not the accepted answer, but it's very useful to learn how to deal very simply with user input. – Joffrey Apr 29 '14 at 09:28
  • you should put the statement `System.out.print("Please enter a ID: ");` before taking input from the user. – Salman Apr 29 '14 at 09:50

2 Answers2

0

Hum.. just add

String s = scan.nextLine();

to get the user entry after you declared your scanner, like :

public void Sc(String ID){
    Scanner input = new Scanner(System.in);
    String s = scan.nextLine();
}
tanou
  • 1,083
  • 2
  • 13
  • 33
0

In general you should not access object's attributes directly it should be done through setter and getter methods. So you should define the class you want with the attributes needed and their setter and getter methods. for example some thing like:

    public class MyClass
{
    private string myField; //"private" means access to this is restricted

    public string getMyField()
    {
         //include validation, logic, logging or whatever you like here
        return this.myField;
    }
    public void setMyField(string value)
    {
             //include more logic
             this.myField = value;
    }
}

Quoted from: tutorial on getters and setters?

After that you need to create new object of that class and set its attribute's value through the setter methods where the value is an user input.

MyClass myClassObj=new MyClass();   // creating new instance of MyClass 
System.out.println("Please enter an ID: ");
Scanner scan= new Scanner(System.in);
String userInput=scan.nextLine();   // get user input

myClassObj.setMyField(userInput);   // setting myFiled attribute 

and just for testing purpose you can get myFiled value through getter method as follow:

System.out.println("The data enterd by user is: "+myClassObj.getMyFiled());
Community
  • 1
  • 1
Salman
  • 1,236
  • 5
  • 30
  • 59