Results 1 to 6 of 6

Thread: Arrays

  1. #1
    tejens23 is offline Member
    Join Date
    Nov 2009
    Posts
    8
    Rep Power
    0

    Question Arrays

    I am working on a program that asks the user to input students names, their address, education level, GPA, etc. But I need to use an array so the names of the students are stored so they can be printed when the user exits the program. My code only prints the name of the last student entered. I tried using an array but I keep getting errors.

    Java Code:
    import java.io.*;
    class StudentApp
    {
    	public static void main(String[] args) throws Exception
    	{
    			Student s = null;
                            
                BufferedReader in;
                in = new BufferedReader(new InputStreamReader(System.in));
                System.out.println("Please select type of object to create:");
                
                System.out.println(
                        "1) Elementry School  2) High School 3) College Student   4)Exit");
                
                int n = Integer.parseInt(in.readLine());
    
                while (n != 4) {
                    if (n == 1) {
                        System.out.println(
                                "You have selected to create a profile of a Elementry School Kid ..... ");
                        
                        System.out.println("Please enter the full name: ");
                        String name = in.readLine();
                        
                        System.out.println("Please enter the Age: ");
                        int age = Integer.parseInt(in.readLine());
                        
                        System.out.println(
                                "Please enter the name of the Elementry School: ");
                        String eschool = in.readLine();
                        
                        System.out.println("Please enter the City: ");
                        String city = in.readLine();
                        
                        System.out.println("Please enter the name of the Teacher: ");
                        String tname = in.readLine();
                        
                        System.out.println("Please enter the Room No.: ");
                        int rno = Integer.parseInt(in.readLine());
                        
                        s = new elementarySchoolStudent(name, age, city, eschool,
                                tname, rno);
                    } 
                    
                    else if (n == 2) {
                        System.out.println(
                                "You have selected to create a profile of a High School Kid ..... ");
                        
                        System.out.println("Please enter the full name: ");                    
                        String name = in.readLine();
                        
                        System.out.println("Please enter the Age: ");
                        int age = Integer.parseInt(in.readLine());
                        
                        System.out.println("Please enter the City: ");
                        String city = in.readLine();
                        
                        System.out.println(
                                "Please enter the name of the High School: ");
                        String eschool = in.readLine();
                        
                        System.out.println(
                                "Please enter the name of the Counselor: ");
                        String tname = in.readLine();
                        
                        System.out.println("Please enter the Year of Graduation: ");
                        int ygr = Integer.parseInt(in.readLine());
                        
                        System.out.println("Please enter the GPA: ");
                        double gpa = Double.parseDouble(in.readLine());
                        
                        s = new highSchoolStudent(name, age, city, eschool, tname,
                                                  ygr, gpa);
                    } 
                    
                    else if (n == 3) {
                        System.out.println(
                                "You have selected to create a profile of a College Kid ..... ");
                        
                        System.out.println("Please enter the full name: ");
                        String name = in.readLine();
                        
                        System.out.println("Please enter the Age: ");
                        int age = Integer.parseInt(in.readLine());
                        
                        System.out.println("Please enter the City: ");
                        String city = in.readLine();
                        
                        System.out.println(
                                "Please enter the name of the College Name: ");
                        String cschool = in.readLine();
                        
                        System.out.println("Please enter the name of the Major: ");
                        String maj = in.readLine();
                        
                        System.out.println("Please enter the GPA: ");
                        double gpa = Double.parseDouble(in.readLine());
                        
                        s = new CollegeStudent(name, age, city, cschool, maj, gpa);
                    }
                   
                    System.out.println(s.printStudentReport());
                    System.out.println();
                    System.out.println();
                    System.out.println("Please select type of object to create:");
                    System.out.println(
                            "1) Elementry School  2) High School 3) College Student   4)Exit");
                   n = Integer.parseInt(in.readLine());
                }
                
                if (n == 4)
                {
                	System.out.println();
                	System.out.println();
    
                	System.out.println(s.getName());
                	
                	System.exit(0);
                }
            }
    }:confused:

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Where's your attempt with an array? Also, please post your full error message.

    Good luck!

  3. #3
    tejens23 is offline Member
    Join Date
    Nov 2009
    Posts
    8
    Rep Power
    0

    Default

    Oops! Sorry, I posted the wrong code. Here's the correct code (not literally correct because it doesn't work yet).

    Java Code:
    import java.io.*;
    class StudentApp
    {
    	public static void main(String[] args) throws Exception
    	{
    			Student[] s;
    			s = new Student[10];
                            
                BufferedReader in;
                in = new BufferedReader(new InputStreamReader(System.in));
                System.out.println("Please select type of object to create:");
                
                System.out.println(
                        "1) Elementry School  2) High School 3) College Student   4)Exit");
                
                int n = Integer.parseInt(in.readLine());
    
                while (n != 4) {
                    if (n == 1) {
                        System.out.println(
                                "You have selected to create a profile of a Elementry School Kid ..... ");
                        
                        System.out.println("Please enter the full name: ");
                        String name = in.readLine();
                        
                        System.out.println("Please enter the Age: ");
                        int age = Integer.parseInt(in.readLine());
                        
                        System.out.println(
                                "Please enter the name of the Elementry School: ");
                        String eschool = in.readLine();
                        
                        System.out.println("Please enter the City: ");
                        String city = in.readLine();
                        
                        System.out.println("Please enter the name of the Teacher: ");
                        String tname = in.readLine();
                        
                        System.out.println("Please enter the Room No.: ");
                        int rno = Integer.parseInt(in.readLine());
                        
                        for(Student student : s)
                        student = new elementarySchoolStudent(name, age, city, eschool,
                                tname, rno);
                    } 
                    
                    else if (n == 2) {
                        System.out.println(
                                "You have selected to create a profile of a High School Kid ..... ");
                        
                        System.out.println("Please enter the full name: ");                    
                        String name = in.readLine();
                        
                        System.out.println("Please enter the Age: ");
                        int age = Integer.parseInt(in.readLine());
                        
                        System.out.println("Please enter the City: ");
                        String city = in.readLine();
                        
                        System.out.println(
                                "Please enter the name of the High School: ");
                        String eschool = in.readLine();
                        
                        System.out.println(
                                "Please enter the name of the Counselor: ");
                        String tname = in.readLine();
                        
                        System.out.println("Please enter the Year of Graduation: ");
                        int ygr = Integer.parseInt(in.readLine());
                        
                        System.out.println("Please enter the GPA: ");
                        double gpa = Double.parseDouble(in.readLine());
                        
                        for(Student student : s)
                        student = new highSchoolStudent(name, age, city, eschool, tname,
                                                  ygr, gpa);
                    } 
                    
                    else if (n == 3) {
                        System.out.println(
                                "You have selected to create a profile of a College Kid ..... ");
                        
                        System.out.println("Please enter the full name: ");
                        String name = in.readLine();
                        
                        System.out.println("Please enter the Age: ");
                        int age = Integer.parseInt(in.readLine());
                        
                        System.out.println("Please enter the City: ");
                        String city = in.readLine();
                        
                        System.out.println(
                                "Please enter the name of the College Name: ");
                        String cschool = in.readLine();
                        
                        System.out.println("Please enter the name of the Major: ");
                        String maj = in.readLine();
                        
                        System.out.println("Please enter the GPA: ");
                        double gpa = Double.parseDouble(in.readLine());
                        
                        for(Student student : s)
                        student = new CollegeStudent(name, age, city, cschool, maj, gpa);
                    }
                   
                    for(Student student : s)
                    {
                    System.out.println(student.printStudentReport());
                    System.out.println();
                    System.out.println();
                    System.out.println("Please select type of object to create:");
                    System.out.println(
                            "1) Elementry School  2) High School 3) College Student   4)Exit");
                   n = Integer.parseInt(in.readLine());
                    }
                }
                
                if (n == 4)
                {
                	System.out.println();
                	System.out.println();
    
                	for(Student student : s)
                	System.out.println(student.getName());
                	
                	System.exit(0);
                }
            }
    }
    And the error:

    Java Code:
    Exception in thread "main" java.lang.NullPointerException
    	at StudentApp.main(StudentApp.java:108)

  4. #4
    tejens23 is offline Member
    Join Date
    Nov 2009
    Posts
    8
    Rep Power
    0

    Default

    Bye the way, how do get line numbers to appear when you post your source code here.

  5. #5
    senorbum is offline Member
    Join Date
    Aug 2009
    Posts
    76
    Rep Power
    0

    Default

    Basically what you are doing is adding the same student into the array 10 times.

    for(Student student : s)

    is a for each loop. So what you are doing in the loop is saying ' For each student in array s, do the following'. However, when you are asking for user input the user is only inputting the information for 1 student.

  6. #6
    tejens23 is offline Member
    Join Date
    Nov 2009
    Posts
    8
    Rep Power
    0

    Default

    This is what the program looks like:

    XML Code:
    Please select type of object to create:
    1) Elementry School  2) High School 3) College Student   4)Exit
    1
    
    You have selected to create a profile of a Elementry School Kid ..... 
    Please enter the full name: 
    Sally Jenkins
    
    Please enter the Age: 
    11
    
    Please enter the name of the Elementry School: 
    Sally K. Ride
    
    Please enter the City: 
    Germantown
    
    Please enter the name of the Teacher: 
    Ms. Keller
    
    Please enter the Room No.: 
    11
    
    Sally Jenkins  Age: 11  Attends: Sally K. Ride  Homeroom: 11  Teacher: Ms. Keller
    
    
    Please select type of object to create:
    1) Elementry School  2) High School 3) College Student   4)Exit
    4
    Java Code:
    Exception in thread "main" java.lang.NullPointerException
    	at StudentApp.main(StudentApp.java:117)
    Now I get this error after a making a selection on the 2nd iteration of the loop.

    This is what I changed:

    I replaced:
    Java Code:
    for(Student student : s)
                        student = new elementarySchoolStudent(name, age, city, eschool,
                                tname, rno);
    with:
    Java Code:
    for(int i = 0; i < s.length; i++){
                        s[i] = new highSchoolStudent(name, age, city, eschool, tname,
                                                  ygr, gpa);
    This change made the diefference but I'm not sure if I'm heading the right direction.

Similar Threads

  1. Need help regarding Arrays Help pls....
    By shaggyoo7 in forum New To Java
    Replies: 3
    Last Post: 01-14-2009, 05:36 AM
  2. 2D Arrays
    By Major90 in forum New To Java
    Replies: 6
    Last Post: 11-06-2008, 03:08 PM
  3. Help with Arrays
    By bri1547 in forum New To Java
    Replies: 4
    Last Post: 08-01-2008, 06:12 AM
  4. new to arrays
    By jimJohnson in forum New To Java
    Replies: 1
    Last Post: 04-08-2008, 03:45 PM
  5. 2D-Arrays
    By kbyrne in forum New To Java
    Replies: 1
    Last Post: 02-07-2008, 11:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •