Hi,
Could someone please help me, I'm a bit stuck and don't know where to start.
I want to create a program where I have to use multiple arrays

eg: processing the data of a file, the file contains details of several groups of students(group 1,2 3,...) and each group has around 10 students and the details of each student (name,dob,address,grade...)is also there.

I figured out that I need a 2 dimensional array to store data of one group, but how do I store the details of each group, should I use a tree dimensional array, a parallel array or could I do it with a nested loop. And if I use an array how can I separate each group from one another.(I'm a bit new to java and programming and we haven't done this sort of programs yet, so any help would be much welcomed.)
:S

Recommended Answers

All 8 Replies

I rarely need to use two dimensional arrays and I have *never* needed to use a three dimensional array. If you create an Object that represents one student, then you can use a one dimensional array and store a bunch of Students in it. You mentioned having three student groups. In order to properly design a program, you need to consider how these student groups are related, and how they are different. For example, since they are student groups, I am assuming they have certain things in common (typical students all have transcripts for example, and all have to pay tuition). So where does this knowledge fit into a Java program? Well, it helps you to design classes that make sense. If you don't know about Java classes, Java Objects, and inheritance, I suggest you start getting familiar before trying to do this program. The basic idea is that you can design a Student class and you can store in it everything you want to about a Student. Then you can create an array (1 dimensional), create a bunch of Students, and put the Students in the array. If you want to, you could make your Student class be the parent class, and then you would have the three following classes: "public class StudentGroupOne extends Student", "public class StudentGroupTwo extends Student", "public class StudentGroupThree extends Student".

Anyway, I hope that is helpful but it is hard to discuss it abstractly - if you know nothing about Objects and classes and inheritance and abstract classes and interfaces, read on all those topics. Then come back, re-read my post, and consider whether you actually need a 2d array or not. Also, if you want to give more details about what your 3 Student groups have in common, and what is different about them, and what each group can do, then I will help you design your classes if you still need my help.

No we haven't done classes and stuff, but I'll let you know if find a way. Thanx for the help anyway.

Hey I just thought of a simpler way!! In each group there are 10 students ,what if I create an array with the array size of 10 and put it inside a nested loop? I wonder if it'll work.......
:\

Why would you think that you came up with a "simpler way" than what I suggested when you don't even know what a class (arguably the most basic unit of OOP) is? Additionally, you not only dismissed my solution to the problem, but then asked another question without answering my question about your problem: to further explain the 3 groups of students. I obviously didn't ask that question for my own good, so the only plausible explanation is that anyone responding to your questions needs to know that information to properly help you. Again, I strongly suggest that you read about the topics I mentioned above, as they will make your project a lot easier to do and more correct. But if you insist on using arrays and only arrays, then at least specify these problem restrictions in advance, as well as explaining your project goals, requirements, and inputs thoroughly, so that people like me don't make the mistake of wasting 20 minutes trying to help you only to be told that you've found a "simpler way".

Hey, I'm sorry if I've offended you, but I thought that there's a simpler way because we have to do it with what we've learnt so far and we haven't done classes yet.
I did sort of find a simpler way but I keep getting stuck half way through, so I came for help again!

This is the file I want to read

"Group 1
No marks Name Nat Average

1 334 John Steffensen AUS 44.82
2 500 Takeshi Fujiwara ESA 46.92
3 651 Dimítrios Régas GRE 46.22
4 352 Chris Brown BAH 44.5
5 1050 Renn Yuow TRI 45.7
6 491 Arismendy Peguero DOM 44.92
7 897 Marcin Marciniszyn POL 45.83
8 626 Bastian Swillims GER 45.44

Group 2
No marks Name Nat Average

1 351 Avard Moncur BAH 45.27
2 571 Brice Panel FRA 46.02
3 1166 Lewis Banda ZIM 45.47
4 915 Félix Martínez PUR 46.86
5 1151 Jeremy Wariner USA 45.1
6 684 David Gillick IRL 45.35
7 595 Martyn Rooney GBR 45.47
8 846 Hermán López NCA 50.72 "

And this is the code I've written,

import java.util.*;
import java.io.*;

public class group{//start of class
	
	public static void main(String args[])
		throws FileNotFoundException{
			
			try{
				Scanner inFile=new Scanner(new FileReader("groups.txt"));
				String[] groupS=new String[100];/*to read data from the first loop*/
				int[] groupN=new int[100];
				String[] no=new String[100];
				String[] marks=new String[100];
				String[] name=new String[100];
				String[] nat=new String[100];
				String[] avg=new String[100];
				int x=0;
				int[] group=new int[8];/*to read data from the second loop*/
				int[] no2=new int[8];
				int[] marks2=new int[8];
				String[] nameFirst=new String[8];
				String[] nameLast=new String[8];
				String[] nat2=new String[8];
				double[] avg2=new double[8];
				int y=0;
				
				while(inFile.hasNext()&&(x<100)){//to write the group
					groupS[x]=inFile.next();
					groupN[x]=inFile.nextInt();
					System.out.println(groupS[x]+" "+groupN[x]);
					no[x]=inFile.next();
					marks[x]=inFile.next();
					name[x]=inFile.next();
					nat[x]=inFile.next();
					avg[x]=inFile.next();
					System.out.println("    "+no[x]+" "+marks[x]+"      "+name[x]+"               "+nat[x]+" "+avg[x]);
					
					while(inFile.hasNext()&&(y<8)){//to write data stored in each group
						no2[y]=inFile.nextInt();
						marks2[y]=inFile.nextInt();
						nameFirst[y]=inFile.next();
						nameLast[y]=inFile.next();
						nat2[y]=inFile.next();
						avg2[y]=inFile.nextDouble();
						System.out.println("    "+no2[y]+"    "+marks2[y]+"      "+nameFirst[y]+" "+nameLast[y]+"   "+nat2[y]+" "+avg2[y]);
						y++;	
					}
					System.out.println();
					x++;
					
				}
				inFile.close();
			}
			catch(IOException exe){
				System.out.println("File not found \n");
			}	
	}//end of main method
}//end of class

And this is the output,
--------------------Configuration: <Default>--------------------
Group 1
No marks Name Nat Average
1 334 John Steffensen AUS 44.82
2 500 Takeshi Fujiwara ESA 46.92
3 651 Dimítrios Régas GRE 46.22
4 352 Chris Brown BAH 44.5
5 1050 Renn Yuow TRI 45.7
6 491 Arismendy Peguero DOM 44.92
7 897 Marcin Marciniszyn POL 45.83
8 626 Bastian Swillims GER 45.44

Group 2
No marks Name Nat Average

1 351
Avard Moncur BAH 45.27 2

Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:840)
at java.util.Scanner.next(Scanner.java:1461)
at java.util.Scanner.nextInt(Scanner.java:2091)
at java.util.Scanner.nextInt(Scanner.java:2050)
at group.main(group.java:30)
Process completed.

The problem is once the first group is done, it goes to the second group and gets stuck in the first while loop. And I don't understand why it happens.
I know it's not a very pretty code, but from what I've learnt this is the best I can do.
Sorry to bother you again.

Hey, guess what!!! I used a for loop instead of the 2nd while loop,

for(y=0;y<8;y++){

and it worked!! :)
I don't know how that happened, but now I can get the whole 2 groups!! (It's still not very pretty though)

You aren't bothering me and I'm happy to help. Just in the future please explain in advance exactly what your restrictions are (I have to/only want to do this using arrays) and what your problem looks like- for example, it would have been hugely useful to know you had "Number, marks, name, nationality, average" for each group.

P.S. you could have had a class that looked like the following:

public class Student {
int number;
int mark;
String name;
String nationality; 
float average;


public Student(int number, int mark, String name, String nationality, float average){
this.number = number;
this.mark = mark;
this.name = name;
this.nationality = nationality;
this.average = average;
}

}

Then you would have only needed one array declared as Student[] students = new Student[100]; and you could have put the students in it by doing this:

//Assuming you already read in, from file, your variables (no, marks, name, nationality, and average)
Student stud = new Student(no, marks, name, nationality, average);

To read in the variables you'd need a slight change since I declared some of them as int, not as Strings... you'd need to use Scanner's nextInt() and nextFloat() methods to read in the int and the float. But overall, this would make your program much simpler. Either way, I'm glad you figured it out and I shouldn't have been so harsh.. Good luck with your exams & whatnot.

Oh, and if you wanted to print out the Students, you could further extend what I mentioned above by implementing the toString method, as I stubbed out below. If you get a chance over break, you should read about the Object class and about method overriding when you read about Classes.

public class Student {
//Everything else I posted earlier in here .. just showing you to add toString

public String toString(){
return "Name = " + this.name + " number = " + this.number; // or whatever you need to print goes here
}

}

Then you would go through your array and print everything out like so:

for (int i = 0; i < array.length; i++) System.out.println(array[i].toString());

P.P.S usually on daniweb we don't post code, at least not as much as I just did, but I figure since you already posted a working solution, there can't be any harm in it this time, and I wanted to demonstrate why I recommended what I did in the first place.

Hey. I've finished my assingment, thanks a lot for your help.

:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.