create array which gets the user input

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 3663 Views - Last Post: 09 March 2010 - 06:50 PM Rate Topic: -----

#1 need_helpp  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 72
  • Joined: 08-September 09

create array which gets the user input

Posted 07 March 2010 - 05:43 PM

What I need to do is:

I have a Student.java which has get and set methods(because the variables are private). And I will have a StudentTest.java which should read the first name, last name ,student id and store them in the array.

I guess I need to create an array in Main file and then read it from user in the test file. And at the end it should print out whatever is in the array
Can someone help me with how to do this?

Student.java


public class Student {
	
private String firstName;
private String lastName;
private String studentNumber;


public String studentList[][][];


    
	Student(String fName,String lName,String sNumber) 
	{
		firstName=fName;  
		lastName=lName;  
		studentNumber=sNumber;
		
	} 
	
	public Student()
	{
	
	}

	public void setFirstName(String fName)
	{
	firstName=fName;  
	} 
	
	public String getFirstName()  
	{
		return firstName;
		
	}//end the method setfirstName
	
	public void setLastName(String lName)  
	{
	lastName=lName;   // Store the lastName 
		
	} // end the method setlastName
	
	public String getLastName() 
	{
		return lastName;
		
	}
	
	public void setStudentNumber(String sNumber)  
	{
	studentNumber=sNumber;   
		
	} // end the method setstudentNumber
	
	public String getStudentNumber()
	{
	return studentNumber;
		
	}
	 
	public String toString() // use of toString method to print the objects
	{
		return String.format("%-8s %-9s %-8s\n",firstName,lastName,studentNumber );
	}//end method toString
}//end class Student





Is This A Good Question/Topic? 0
  • +

Replies To: create array which gets the user input

#2 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: create array which gets the user input

Posted 07 March 2010 - 05:50 PM

The student class should not contain that Array.
also, why declare an array of String values? you have a Student class, so create an array of Student Objects in the Tester class (you can do it all in the main method of that tester class):
Student[] students = new Student[LENGHT];//LENGHT is for you to decide.

after declaring that array, you can assign new Student Objects to it as:
students[0] = new Student("Fname", "Lname", "0000");
students[1] = new Student("Fname2", "Lname2", "1111");
//etc..

to print the array use a simple for loop:
for(int i = 0 ; i < students.length; i++){
     //since you override the toString method, simply use:
     System.out.println(students[i]);
}

make sure that you instaniate all the students array's Objects, or if you'll try to refer to them you will get a NullPointerException.

**edited, missed "" on one of the Strings..

This post has been edited by japanir: 07 March 2010 - 05:56 PM

Was This Post Helpful? 1
  • +
  • -

#3 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 9037
  • View blog
  • Posts: 33,521
  • Joined: 27-December 08

Re: create array which gets the user input

Posted 07 March 2010 - 05:53 PM

For the most part, your Student class looks good. The only suggestion I have there is to remove the 3-dimmensional array public String studentList[][][]; , as you don't need it. Instead, in your main() method (or Main class), create a one-dimmensional array of Student objects. So something like:
class Main{
   private Student[] roster; //the array of Student objects

   public Main(){
       //logic of your program goes here

   }

   //the main() method is where your program will begin
   public static void main(String[] args){new Main();}
}


Was This Post Helpful? 0
  • +
  • -

#4 need_helpp  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 72
  • Joined: 08-September 09

Re: create array which gets the user input

Posted 07 March 2010 - 05:58 PM

View Postjapanir, on 07 March 2010 - 04:50 PM, said:

The student class should not contain that Array.
also, why declare an array of String values? you have a Student class, so create an array of Student Objects in the Tester class (you can do it all in the main method of that tester class):
Student[] students = new Student[LENGHT];//LENGHT is for you to decide.

after declaring that array, you can assign new Student Objects to it as:
students[0] = new Student("Fname", "Lname", "0000");
students[1] = new Student("Fname2", "Lname2", "1111");
//etc..

to print the array use a simple for loop:
for(int i = 0 ; i < students.length; i++){
     //since you override the toString method, simply use:
     System.out.println(students[i]);
}

make sure that you instaniate all the students array's Objects, or if you'll try to refer to them you will get a NullPointerException.

**edited, missed "" on one of the Strings..


Thanks but insead of me assigning the Student objects it needs to read them from user input. How can I do that?,
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 9037
  • View blog
  • Posts: 33,521
  • Joined: 27-December 08

Re: create array which gets the user input

Posted 07 March 2010 - 06:01 PM

You can use the Scanner class to read in the corresponding attributes for a Student object, then create a Student with those variables/inputs.
Was This Post Helpful? 0
  • +
  • -

#6 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: create array which gets the user input

Posted 07 March 2010 - 06:04 PM

You can use a Scanner object, and get 3 String Objects for each Student object(represents first name, last name, and number). then just create the Student Object passing those variales as parameters:
Scanner input = new Scanner(System.in);

String fName = input.nextLine();
String lName = input.nextLine();
String sNumber = input.nextLine();
students[i] = new Student(fName, lName, sNumber);


have the part of getting the String Objects and creating new Student inside a loop which will iterate as the length of the students array.

when using a Scanner object, don't forget to import:
import java.util.Scanner;

Scanner API:
http://java.sun.com/...il/Scanner.html
Was This Post Helpful? 1
  • +
  • -

#7 need_helpp  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 72
  • Joined: 08-September 09

Re: create array which gets the user input

Posted 07 March 2010 - 06:54 PM

View Postjapanir, on 07 March 2010 - 05:04 PM, said:

You can use a Scanner object, and get 3 String Objects for each Student object(represents first name, last name, and number). then just create the Student Object passing those variales as parameters:
Scanner input = new Scanner(System.in);

String fName = input.nextLine();
String lName = input.nextLine();
String sNumber = input.nextLine();
students[i] = new Student(fName, lName, sNumber);


have the part of getting the String Objects and creating new Student inside a loop which will iterate as the length of the students array.

when using a Scanner object, don't forget to import:
import java.util.Scanner;

Scanner API:
http://java.sun.com/...il/Scanner.html



I really appreciate your help. For some reason even though I changed the length to 10, it only reads 3 students. is there any reason for that?

import java.util.Scanner;

public class StudentTest {
	public static void main( String[] args)
	{ 
		
		System.out.println("Enter Student's fName, lName, and sNumber");
		Student[] students = new Student[10];
		
		for(int i = 0 ; i < students.length; i++)
	{ 
	Scanner input = new Scanner(System.in); 
	 
	String fName = input.nextLine(); 
	String lName = input.nextLine(); 
	String sNumber = input.nextLine(); 
	students[i] = new Student(fName, lName, sNumber);
	
	System.out.println(students[i]);
	    
	}	 
	}
	

}




Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8022
  • View blog
  • Posts: 31,133
  • Joined: 06-March 08

Re: create array which gets the user input

Posted 07 March 2010 - 08:23 PM

Don't create a new Scanner object in all iteration of your loop like in
        for(int i = 0 ; i < students.length; i++) 
        {  
            Scanner input = new Scanner(System.in);  
          


better to
        Scanner input = new Scanner(System.in);  
        for(int i = 0 ; i < students.length; i++) 
        {  
          


The I/O subsystem will get quite confuse on to which Scanner object delivering the input should be done

When you have a phone conversation with somebody, do you hang up and redial at each sentence ?
Was This Post Helpful? 2
  • +
  • -

#9 need_helpp  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 72
  • Joined: 08-September 09

Re: create array which gets the user input

Posted 08 March 2010 - 07:59 AM

View Postpbl, on 07 March 2010 - 07:23 PM, said:

Don't create a new Scanner object in all iteration of your loop like in
        for(int i = 0 ; i < students.length; i++) 
        {  
            Scanner input = new Scanner(System.in);  
          


better to
        Scanner input = new Scanner(System.in);  
        for(int i = 0 ; i < students.length; i++) 
        {  
          


The I/O subsystem will get quite confuse on to which Scanner object delivering the input should be done

When you have a phone conversation with somebody, do you hang up and redial at each sentence ?


Thank you for your help it solved the issue.
Is there a way to check if the student number already exist by using "equals" method?
Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 9037
  • View blog
  • Posts: 33,521
  • Joined: 27-December 08

Re: create array which gets the user input

Posted 08 March 2010 - 08:44 AM

You can iterate through the array up to i exclusive and check the Student objects using a nested for loop. If you override the equals() method in the Student class to compare Students by number, then yes, you can use the equals() method.
Was This Post Helpful? 0
  • +
  • -

#11 need_helpp  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 72
  • Joined: 08-September 09

Re: create array which gets the user input

Posted 08 March 2010 - 02:36 PM

View Postmacosxnerd101, on 08 March 2010 - 07:44 AM, said:

You can iterate through the array up to i exclusive and check the Student objects using a nested for loop. If you override the equals() method in the Student class to compare Students by number, then yes, you can use the equals() method.


I am not really familiar with equals() method and how to call it from StudentTest. Can you give an example?
Was This Post Helpful? 0
  • +
  • -

#12 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2695
  • View blog
  • Posts: 10,556
  • Joined: 15-July 08

Re: create array which gets the user input

Posted 08 March 2010 - 03:24 PM

Normally, when you compare objects, they compare memory locations. However, if you override the .equals() method in your custom class, you can check for custom things. For example, you can override the .equals() method to return true if the first name, last name, and student number are the same, however, the cool thing is, you can define whatever you want as "equal"

public class Student {
        
private String firstName;
private String lastName;
private String studentNumber;

   ...
   public boolean equals (Object o) {
       if (o instanceof Student) {
           Student s = (Student) o;
           if (s.getFirstName.equals(firstName) && 
               s.getLastName.equals(lastName) && 
               s.getStudentNumber.equals(studentNumber)) 
                   return true;
       }
       return false;
   }   

}//end class Student



edit: Thanks Mac!

This post has been edited by Dogstopper: 08 March 2010 - 06:00 PM

Was This Post Helpful? 1
  • +
  • -

#13 need_helpp  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 72
  • Joined: 08-September 09

Re: create array which gets the user input

Posted 08 March 2010 - 05:37 PM

How should I call this equals() method in my StudentTest.java so it will check if the student exists?
Was This Post Helpful? 0
  • +
  • -

#14 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2695
  • View blog
  • Posts: 10,556
  • Joined: 15-July 08

Re: create array which gets the user input

Posted 08 March 2010 - 05:53 PM

macosxnerd101 stated that in the equals method, if you ignore names and only check equality based on id, then you can loop through to see if students are equal. If so, then that student already exists...
Was This Post Helpful? 0
  • +
  • -

#15 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 9037
  • View blog
  • Posts: 33,521
  • Joined: 27-December 08

Re: create array which gets the user input

Posted 08 March 2010 - 05:55 PM

@Dogstopper: I think you're missing an ending curly brace for your equals() method, as you return false inside the if statement. So therefore, you will get a compilation error stating something like "missing return statement," as your method may return nothing if o isn't an instanceof Student.
public boolean equals (Object o) { 
       if (o instanceof Student) { 
           Student s = (Student) o; 
           if (s.getFirstName.equals(firstName) &&  
               s.getLastName.equals(lastName) &&  
               s.getStudentNumber.equals(studentNumber))  
                   return true; 
       return false; 
   }//end if
   //missing blanket return for equals() method, which will cause an error
//missing a brace for end equals()    


Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2