Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



Help with two dimensional array java

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 30-11-2009
Member
 
Join Date: Nov 2009
Posts: 518
Help with two dimensional array java
  

Hi
I am trying to create an two dimensional array that will store student id and grade. The program has to set the size of the array according to the user input. Then the array should be built and finally the user should be requested to add the information for the student and the grade. All this will be stored in the array. I am not sure to figure out what is wrong here , below is my program.
Code:
public class Classavg1
{
public static void main(String[] args)
{
int stu; //The number of stu
int grd = 0;
int ID = 0;
int avg;

**Scanner object**
//Create a Scanner object for keybrd input.
Scanner keybrd = new Scanner(System.in);

//Get the number of stu.
System.out.print("How many stu are in your class? ");
stu = keybrd.nextInt();

//Create an array to hold that number of stu.
int scr[][] = new int [stu][ID];

**code to get student id and grde**
for ( int id = 0; id <scr.length; id++)
{
for (int grd = 0; grd < grd; grd++);
{
System.out.print("Enter student's ID ");
ID = keybrd.nextInt();
System.out.print("Enter Student's grdes ");
scr[id][grd] = keybrd.nextInt();
}
}
}
}


Last edited by GlassFish : 30-11-2009 at 01:58 PM.
Reply With Quote
  #2  
Old 30-11-2009
Member
 
Join Date: Jan 2008
Posts: 1,515
Re: Help with two dimensional array java

Hi
I read your program and I have done some modification to it, just check it out
Code:
public class Classavg1
{
public static void main(String[] args)
{
int std; //The number of std
int grd = 0;
int ID = 0;
int avg;
Scanner keybrd = new Scanner(System.in);
System.out.print("How many std are in your class? ");
std = keybrd.nextInt();
int scr[][] = new int [std][2]; //we hold id and grde for each //student so number of cols=2
for ( int id = 0; id <scr.length; id++)
{
//for (int grd = 0; grd < grd; grd++);
//{
System.out.print("Enter student's ID ");
ID = keybrd.nextInt();
System.out.print("Enter Student's grdes ");
scr[id][0]=ID;
scr[id][1] = keybrd.nextInt();
//}
}
}
}
Reply With Quote
  #3  
Old 30-11-2009
Member
 
Join Date: Feb 2008
Posts: 1,848
Re: Help with two dimensional array java

Hi
hope you have tried the above posted program, it should work with you. Your mistakes were, ID=0 it means you are trying to make an array of
Quote:
src[std][0],
int scr[][] = new int [stu][ID];
**code to get student id and grde**
for ( int id = 0; id <scr.length; id++)
{
for (int grd = 0; grd < grd; grd++);
{
System.out.print("Enter student's ID ");
ID = keybrd.nextInt();
System.out.print("Enter Student's grdes ");
scr[id][grd] = keybrd.nextInt();
}
}
and this will definitely give you any error if you try to acces it because it is any array of empty array.
Reply With Quote
  #4  
Old 30-11-2009
Member
 
Join Date: Nov 2009
Posts: 518
Re: Help with two dimensional array java

Hello
Thanks a lot for your help guys. I have successfully solved this program and got the required output. Really it would not have been possible to solve the problem without you guys. Thanks a lot again.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Help with two dimensional array java"
Thread Thread Starter Forum Replies Last Post
Array of objects in java Ash maker Software Development 5 27-02-2010 02:05 AM
Two dimensional array to a function Chrisch Software Development 3 27-11-2009 12:47 PM
Dynamically allocate a two-dimensional array Chrisch Software Development 3 24-10-2009 11:43 AM
Three Dimensional Array program in Java. nonose Software Development 3 10-08-2009 07:49 PM
JAVA array Daren Software Development 2 06-03-2009 06:13 PM


All times are GMT +5.5. The time now is 03:21 PM.