java help


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: java help

Hybrid View

  1. #1
    Join Date
    Dec 2005
    Posts
    4

    java help

    I'm at college and i'm new to java, i'm currently creating the following program.

    The problem is if i assign the array with a number it works fine but if i define it with a letter that will hold a number it gets error

    The reason i placed a letter was so that the array isn't a fixed value so they could enter as many students as they want by typing it in via the D in the StudentID array hope thats sounds right

    Code with the Array numbers defined this one works

    //Program for Assignment of Software Development
    //Richard Meredith

    PHP Code:
    import java.util.*;

    public class 
    Assigns

    {
        
    //public static int D ;
        
    public static String studentID[] = new String[2];        
        public static 
    int marks[][] = new int[10][8];            
        public static 
    Scanner kybd = new Scanner(System.in);        
        public static 
    String response;        
        public static 
    int Total 0;
        public static 
    int FinalTotal[] = new int[2];
        



             public static 
    void main (String[]args)
            {    
                
    menu();
            }

                    
            public static 
    void menu()                        // Entry function
            
    {

            
    int input;
            
    System.out.println("   Please select an option\n");
            
    System.out.println("1. Enter Student ID and Exam marks");
            
    System.out.println("2. Display current Data");
            
    System.out.println("3. Display Total marks");
            
    System.out.println("4. Dislay Grades");
            
    System.out.println("5. Save Data");
                
            
    input kybd.nextInt();

                switch(
    input)
                {
                    case 
    1:

                    {    
                        
                        
    GetID();
                    }
                
                    case 
    2:
                    
                    {
                        
    Display();            
                    }

                    case 
    3:
                    
                    {
                        
    //Stats        
                    
    }
                    
                    case 
    4:
                    
                    {
                           
    //Modify            
                    
    }

                    case 
    5:
                    
                    {
                          
    //Search                            
                    
    }
                    case 
    6:
                    {
                         
    //Save
                    
    }

                    case 
    7:
                    {
                        
    //Quit
                    
    }
                }



            }


            public static 
    void GetID()    
            {    


            for (
    int c 02;c++)
            {
                    
            
    System.out.println("Please Enter the student ID");
            
    studentID[c] = kybd.next();

            
    System.out.println("Please enter the eight exams marks");
        for (
    int a 08; ++a
        {
            
    System.out.println("Enter mark" " " +( 1) + " ");
            
    marks[c][a] = kybd.nextInt();
        
                     if (
    marks[c][a] > 20)
            {
              
    System.out.println("Entry out of score limit");
              
    ;
              
    ;
              
    System.out.println("Enter mark" " " +( 1) + " ");
              
    marks[c][a] = kybd.nextInt();
            }
            else
            if(
    marks[c][a] < -1)
            {
             
    System.out.println("Entry out of score limit");
            
    ;
            
    ;
            
    System.out.println("Enter mark" " " +( 1) + " ");
            
    marks[c][a] = kybd.nextInt();
            }
        }
                    
        }
                
                
                
        }



        public static 
    void Display()
        {
            
    //System.out.println("\nStudentID   1  2  3  4  5  6  7  8  %  Grade");

                
    for (int c 02c++)
                { 
                    
                
    System.out.print("\n"studentID[c] + " ");
                
                for (
    int a 08; ++a)
                {
                
    System.out.print("  " marks[c][a] + " ");
                }
                
    MarksTotal(c);
                
    Grade();
                }
                
                
                
                
            }            


            public static 
    void MarksTotal(int c)
            {
            for (
    02; ++c)            
            {
                
    Total 0;
                for (
    int a 08; ++a
                {
                    if (
    marks[c][a] > -1)
                    {
                              
    Total Total marks[c][a];         
                    
    FinalTotal[c] = Total;
                    }
                    
                }
                

            }
                
    System.out.print(Total);        
            }
            

            public static 
    void Grade()
            {
                if (
    Total 70)
                {
                    
    System.out.print(" Distiniction");
                }
                if (
    Total <=70 && Total 60)
                {
                    
    System.out.print(" Merit");
                }
                if (
    Total 40 && Total <= 60)
                {
                    
    System.out.print(" Pass");
                }
                if (
    Total <= 40)
                {
                    
    System.out.print(" Fail");
                }
            }
        public static 
    void SaveFile()
        {

            
    //Save to file
        
    }

        public static 
    void LoadFile()
        {

        }
    }            

    CODE WITH ARRAY LETTER INSTEAD OF NUMBER THIS DOESN'T WORK

    //Program for Assignment of Software Development
    //Richard Meredith

    import java.util.*;

    public class Assigns

    {
        public static int D ;
        public static String studentID[] = new String[D];        
        public static int marks[][] = new int[10][8];            
        public static Scanner kybd = new Scanner(System.in);        
        public static String response;        
        public static int Total = 0;
        public static int FinalTotal[] = new int[D];
        



             public static void main (String[]args)
            {    
                menu();
            }

                    
            public static void menu()                        // Entry function
            {

            int input;
            System.out.println("   Please select an option\n");
            System.out.println("1. Enter Student ID and Exam marks");
            System.out.println("2. Display current Data");
            System.out.println("3. Display Total marks");
            System.out.println("4. Dislay Grades");
            System.out.println("5. Save Data");
                
            input = kybd.nextInt();

                switch(input)
                {
                    case 1:

                    {    
                        
                        GetID();
                    }
                
                    case 2:
                    
                    {
                        Display();            
                    }

                    case 3:
                    
                    {
                        //Stats        
                    }
                    
                    case 4:
                    
                    {
                           //Modify            
                    }

                    case 5:
                    
                    {
                          //Search                            
                    }
                    case 6:
                    {
                         //Save
                    }

                    case 7:
                    {
                        //Quit
                    }
                }



            }


            public static void GetID()    
            {    
            System.out.println("please enter number of students");
            D = kybd.nextInt();

            for (int c = 0; c < D;c++)
            {
                    
            System.out.println("Please Enter the student ID");
            studentID[c] = kybd.next();

            System.out.println("Please enter the eight exams marks");
        for (int a = 0; a < 8; ++a) 
        {
            System.out.println("Enter mark" + " " +( a + 1) + " ");
            marks[c][a] = kybd.nextInt();
        
                     if (marks[c][a] > 20)
            {
              System.out.println("Entry out of score limit");
              a = a ;
              c = c ;
              System.out.println("Enter mark" + " " +( a + 1) + " ");
              marks[c][a] = kybd.nextInt();
            }
            else
            if(marks[c][a] < -1)
            {
             System.out.println("Entry out of score limit");
            a = a ;
            c = c ;
            System.out.println("Enter mark" + " " +( a + 1) + " ");
            marks[c][a] = kybd.nextInt();
            }
        }
                    
        }
                
                
                
        }



        public static void Display()
        {
            
    //System.out.println("\nStudentID   1  2  3  4  5  6  7  8  %  Grade");

                for (int c = 0; c < D; c++)
                { 
                    
                System.out.print("\n"+ studentID[c] + " ");
                
                for (int a = 0; a < 8; ++a)
                {
                System.out.print("  " + marks[c][a] + " ");
                }
                MarksTotal(c);
                Grade();
                }
                
                
                
                
            }            


            public static void MarksTotal(int c)
            {
            for (c = 0; c < D; ++c)            
            {
                Total = 0;
                for (int a = 0; a < 8; ++a) 
                {
                    if (marks[c][a] > -1)
                    {
                              Total = Total + marks[c][a];         
                    FinalTotal[c] = Total;
                    }
                    
                }
                

            }
                System.out.print(Total);        
            }
            

            public static void Grade()
            {
                if (Total > 70)
                {
                    System.out.print(" Distiniction");
                }
                if (Total <=70 && Total > 60)
                {
                    System.out.print(" Merit");
                }
                if (Total > 40 && Total <= 60)
                {
                    System.out.print(" Pass");
                }
                if (Total <= 40)
                {
                    System.out.print(" Fail");
                }
            }
        public static void SaveFile()
        {

            //Save to file
        }

        public static void LoadFile()
        {

        }
    }


    Can anyone point me in the right direction 
    Last edited by Rico3k; 12-05-2005 at 06:00 AM.

  2. #2
    Join Date
    Aug 2005
    Posts
    43
    the problem is that the program does not know the value of D when it starts, so it does not know how much space to allocate for the array. If you want an array to dinamically grow or shrink in size during runtime, do a search on how to use the ArrayList or LinkedList classes.

  3. #3
    Join Date
    Dec 2005
    Posts
    4
    public static void GetID()
    {
    System.out.println("please enter number of students");
    D = kybd.nextInt();

    It gets its value from keyboard entry

  4. #4
    Join Date
    Mar 2004
    Posts
    635
    What's the error you're getting?

Similar Threads

  1. Replies: 2
    Last Post: 06-14-2006, 03:16 PM
  2. Java vs. .Net. A questionnaire
    By Basil in forum .NET
    Replies: 1
    Last Post: 05-13-2005, 06:46 AM
  3. learning c# very confusing.
    By Mike Tsakiris in forum .NET
    Replies: 11
    Last Post: 10-04-2002, 05:32 PM
  4. Replies: 0
    Last Post: 01-31-2002, 11:08 PM
  5. Re: VB vs. Visual Age for Java
    By JJ in forum Enterprise
    Replies: 1
    Last Post: 07-06-2000, 04:50 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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center