Results 1 to 3 of 3

Thread: Using Arrays

  1. #1
    RevGav is offline Member
    Join Date
    May 2010
    Posts
    1
    Rep Power
    0

    Default Using Arrays

    I have been given a task and although some people may think im after easy answers i just wanna know what on earth im doing so very very wrong. Ive spent weeks on this and its still not working at all. I basically need options 4 and 5 to return the list of arrays for the students. the array itself seems coded ok (no errors) but when it runs it returns null??

    heres the code:
    Many thanks!

    Java Code:
    /*
     *
     *
     */
    
    import java.util.Scanner;
    
    class test1 
     { 
    	 public static void main(String[] args) ;
    	 student mystudent[0] = new student[50]]; 
    				
    
     	public static void menu()
     	{
     		System.out.println("1. Numeracy");
     		System.out.println("2. Literacy");
     		System.out.println("3. Add another course and enroll to it");
     		System.out.println("4. List of students by age");
     		System.out.println("5. List of students by course.");
     		System.out.println("6. Exit");
     	}
    
    	public static void main (String[]args) 
     	{  
     		
     		Scanner input = new Scanner(System.in);
     		
     		String studName, newCourse;
     		int studId, studAge, studTelNr, option=0;
     		
     		System.out.println("Welcome");
     		
     		while (option!=6)
     		{
     			System.out.println("Please choose your desired course");
     				
     			menu();
     			
     			System.out.println("");
     			option = input.nextInt();
     			
     			if (option!=6)
     			{	
     				
     				if (option==1)
     				{
     					System.out.println(" Numeracy:");
     					System.out.println("Student ID number ");
     					studId = input.nextInt();
     					System.out.println("Student Name ");
     					studName = input.next();
     					System.out.println("Age ");
     					studAge = input.nextInt();
     					System.out.println("Student telephone Number ");
     					studTelNr = input.nextInt();
     				
     					mystudent[0] = new student (studId, studName, studAge, studTelNr, "Numeracy"); // - put the new student to the array
    					mystudent++								  // - increase itemindex
     				}
     				
     				if (option==2)
     				{
     					System.out.println("Literacy:");
     					System.out.println("\nYour student ID number ");
     					studId = input.nextInt();
     					System.out.println("Your Name ");
     					studName = input.next();
     					System.out.println("Your Age ");
     					studAge = input.nextInt();
     					System.out.println("Your telephone number ");
     					studTelNr = input.nextInt();
     				
     					mystudent[0] = new student (studId, studName, studAge, studTelNr, "Literacy");
    				
    				
     				}
     		
     				if (option==3)
     				{
     					
     					System.out.println("Additional course: ");
     					newCourse = input.next();
     					System.out.println("\nYour student ID number ");
     					studId = input.nextInt();
     					System.out.println("Your Name ");
     					studName = input.next();
     					System.out.println("Your Age ");
     					studAge = input.nextInt();
     					System.out.println("Your telephone number ");
     					studTelNr = input.nextInt();
     				
     					mystudent[0] = new student (studId, studName, studAge, studTelNr,newCourse "New Course");
    				
     				}
     				
     				if (option==4)
     				{
     					System.out.println(mystudent.getID() + "  " + mystudent.getAge());
    		
    
     				}	
     				
     				
     				if (option==5)
     				{
     					System.out.println(mystudent.getID() + "  " + mystudent.getCourse());
     																//  output is "null" too. 
     				}
     			}
     		}	
     	}
    
    	void sortbyage()
    		{// - sort the array by age
    			student tmp = null;
    			int i=0;
    			int j=1;
    			while(i<=mystudent)
    			{
    				while(j<=mystudent)
    				{
    					if(mystudent[i].getAge()>mystudent[j].getAge())
    					{
    						tmp = mystudent[i];
    						mystudent[i]=mystudent[j];
    						mystudent[j]=tmp;
    					}
    					else
    					{//if	
    						j++;
    					}//else
    				}//for j		
    				i++;
    			}//for i
    		
    			
    		
    		}
    		
    }
    Last edited by Eranga; 05-25-2010 at 05:06 AM. Reason: code tags added

  2. #2
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Lightbulb

    student mystudent[0] = new student[50]];
    two main methods

    public static void main(String[] args) ;
    and

    public static void main (String[]args)
    {

    Scanner input = new Scanner(System.in);

    String studName, newCourse;
    int studId, studAge, studTelNr, option=0;

    System.out.println("Welcome");

    ...}

    most horrible things :rolleyes:
    Maybe someone finds more ;)
    If my answer helped you. Please click my "REP" button and add a comment
    Have a Good Java Coding :)

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    @OP: Please use code tags when you posting again here in the forum. Unformated codes are really hard to read. If you don't know how to do it, please check on my forum signature.

Similar Threads

  1. store array of arrays in array of arrays
    By joost_m in forum New To Java
    Replies: 4
    Last Post: 04-19-2010, 11:32 AM
  2. Arrays.sort... why sorting all arrays in class?
    By innspiron in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 02:40 AM
  3. Help with arrays
    By Skorge in forum New To Java
    Replies: 1
    Last Post: 11-24-2009, 08:35 AM
  4. Need help with 2D arrays...
    By rrsv2 in forum New To Java
    Replies: 3
    Last Post: 11-30-2008, 04:15 AM
  5. need help with arrays
    By Jman in forum New To Java
    Replies: 17
    Last Post: 07-21-2008, 03:34 AM

Posting Permissions

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