Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

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

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
[8551 byte] By [Rico3k] at [2007-11-11 7:25:43]
# 1 Re: java help
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.
chimps at 2007-11-11 22:38:18 >
# 2 Re: java help
public static void GetID()
{
System.out.println("please enter number of students");
D = kybd.nextInt();

It gets its value from keyboard entry
Rico3k at 2007-11-11 22:39:23 >
# 3 Re: java help
What's the error you're getting?
Phaelax at 2007-11-11 22:40:22 >