-
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 = 0; c < 2;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 < 2; 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 < 2; ++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()
{
}
}
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.
-
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.
-
public static void GetID()
{
System.out.println("please enter number of students");
D = kybd.nextInt();
It gets its value from keyboard entry
-
What's the error you're getting?
Similar Threads
-
Replies: 2
Last Post: 06-14-2006, 03:16 PM
-
Replies: 1
Last Post: 05-13-2005, 06:46 AM
-
By Mike Tsakiris in forum .NET
Replies: 11
Last Post: 10-04-2002, 05:32 PM
-
By Anonymous in forum Java
Replies: 0
Last Post: 01-31-2002, 11:08 PM
-
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
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks