Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial

Eric Witowski
Courses Plus Student 92 PointsSCANNER, 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());
}
}
3 Answers

Steve Hunter
57,712 PointsHere'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);
}
}

Eric Witowski
Courses Plus Student 92 PointsPlease 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

Steve Hunter
57,712 PointsOK. 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?

Eric Witowski
Courses Plus Student 92 PointsYes 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
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsHi Eric,
What is it you want to do? Do you want the user to be able to populate an instance of
StudentData
. You can useScanner
for that, yes, or if you're careful you can useconsole.readLine()
. In an IDEScanner
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.