Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java

SCANNER, AND USER INPUT LINES. FOR THIS PROGRAM

PLEASE HELP ME WITH PLACING THE SYNTAX/CODE FOR THE SCANNER SO I CAN HAVE USER INPUT.

//lets see enter 
//import java.util.Scanner;
//import foundation;

class StudentData {
   private int stuID;
   private String stuName;
   private int stuAge;
   StudentData(){
       //Default constructor
       stuID = 100;
       stuName = "New Student";
       stuAge = 18;
   }
   StudentData(int num1, String str, int num2){
       //Parameterized constructor
       stuID = num1;
       stuName = str;
       stuAge = num2;
   }
   //Getter and setter methods
   public int getStuID() {
       return stuID;
   }
   public void setStuID(int stuID) {
       this.stuID = stuID;
   }
   public String getStuName() {
       return stuName;
   }
   public void setStuName(String stuName) {
       this.stuName = stuName;
   }
   public int getStuAge() {
       return stuAge;
   }
   public void setStuAge(int stuAge) {
       this.stuAge = stuAge;
   }
///where or where does the scanner go??????
   public static void main(String args[]) {
       //This object creation would call the default constructor
       StudentData myobj = new StudentData();
       System.out.println("Student Name is: "+myobj.getStuName());
       System.out.println("Student Age is: "+myobj.getStuAge());
       System.out.println("Student ID is: "+myobj.getStuID());

       /*This object creation would call the parameterized
        * constructor StudentData(int, String, int)*/
       StudentData myobj2 = new StudentData(555, "Chaitanya", 25);
       System.out.println("Student Name is: "+myobj2.getStuName());
       System.out.println("Student Age is: "+myobj2.getStuAge());
       System.out.println("Student ID is: "+myobj2.getStuID()); 
  }
}

Hi Eric,

What is it you want to do? Do you want the user to be able to populate an instance of StudentData. You can use Scanner for that, yes, or if you're careful you can use console.readLine(). In an IDE Scanner is easier, to be frank.

How do you see this working? You'll need a menu for the user to select, say, 1 - new student, 2 - edit student, 3 - delete student.

When 1 is chosen, ask for three inputs (although the ID should be generated automatically, perhaps?) and then fire those values at the parameterised constructor. The edit option can do something similar; use the setters on an existing object. For delete, you can just point the existing student object at null and it should get collected.

The Scanner piece itself is pretty straightforward and well documented. Let me know what you want your code to do and I'll see what I can suggest.

Steve.

3 Answers

Here's something I just lashed together for you.

It isn't pretty but it gets the Scanner working and lets you create a new student, then outputs the object data.

I hope it helps.

Steve.

import java.util.Scanner;

class StudentData {
    private int stuID;
    private String stuName;
    private int stuAge;

    StudentData(){
        //Default constructor
        stuID = 100;
        stuName = "New Student";
        stuAge = 18;
    }
    StudentData(int num1, String str, int num2){
        //Parameterized constructor
        stuID = num1;
        stuName = str;
        stuAge = num2;
    }
    //Getter and setter methods
    public int getStuID() {
        return stuID;
    }
    public void setStuID(int stuID) {
        this.stuID = stuID;
    }
    public String getStuName() {
        return stuName;
    }
    public void setStuName(String stuName) {
        this.stuName = stuName;
    }
    public int getStuAge() {
        return stuAge;
    }
    public void setStuAge(int stuAge) {
        this.stuAge = stuAge;
    }
    ///where or where does the scanner go??????
    public static void main(String args[]) {
        System.out.println("Enter new student details:");
        StudentData stu = newStudent();
        System.out.println("New student name is: " + stu.getStuName());
        System.out.println("New student age is: " + stu.getStuAge());

    }

    public static StudentData newStudent(){
        Scanner sc = new Scanner(System.in);
        String name;
        String age;
        int ageAsInt;
        System.out.println("Enter student name: ");
        name = sc.nextLine();
        System.out.println("Enter student age: ");
        age = sc.nextLine();
        ageAsInt = Integer.parseInt(age);
        return new StudentData(1, name, ageAsInt);
    }
}

Please enter your selection by the way thank you for your time, you are very appreciated. Here is my idea for it to look like

1. newStudent
2. returnStudent
studentName
........
collegeName
.....
phoneNumber
.....
contactName
collegeName
phoneNumber

OK. Happy to help but we need to work on how you define your requirements! As a developer you will get annoyed about poorly defined scopes. Just like the one you wrote there! So let's work on articulating what you want to happen and go from there.

Make sense?

Yes Thank you for the help and the advice. Feels like I am going at lightspeed; not sleeping much yes thanks very very much. What you showed me was great. Eric