# Please Help! 2D Arrays

Page 1 of 1

## 1 Replies - 1156 Views - Last Post: 14 May 2009 - 04:38 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=105041&amp;s=593d0c83a3634e8953693767fe91e37a&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 guss46

• New D.I.C Head

Reputation: 0
• Posts: 1
• Joined: 13-May 09

# Please Help! 2D Arrays

Posted 13 May 2009 - 06:35 PM

Below is my code, and explaination is at the bottom

```public void searchStudent() throws Exception {
Scanner scanner;		   									//Set Scanner
scanner = new Scanner(System.in);
Student[][] allstudentObjArray = new Student[10][12];
Scanner allStu = new Scanner(new File("allstudents2.txt"));
int i=0;
//	boolean found = false;

//This is where your problem lays your reading it into the wrong thing soilder.
Scanner sa1 = new Scanner(allStu.nextLine());

while(allStu.hasNextLine())
{
for (int i3 = 0; i3 < allstudentObjArray.length; i3++)
{
//Delimit this line with scanner

sa1.useDelimiter(":");

for(int i2 = 0;i2 < allstudentObjArray.length;i2++)
{
//Try sa1 for stuAll below??
//Try Student for stuAll below??
//Try get rid of the "new". why create new object
allstudentObjArray[i3][i2] = new stuAll(sa1.next());
i2++;
}
i3++;
}
}

System.out.println(allstudentObjArray[0][0]);
System.out.println(allstudentObjArray[1][0]);
System.out.println(allstudentObjArray[2][0]);
System.out.println(allstudentObjArray[3][0]);
```

Pretty much what Im trying to do here is, go through each line of the text file with values delimited by :
there are 11 lines, with 13 values in each line all seperated by a ":"

I want to put these into this 2D array and then take them out for use as needed by my program.

Im trying to use two for loops, one for the line to go and then one for the values.

I seem to be doing something wrong, it seems to be happening around the stage when I try to put the delimited value in
one by one.

Am I going about this the right way, or should I be heading down a different path?

Or is there something small I'm just not doing right,

say around this line "allstudentObjArray[i3][i2] = new stuAll(sa1.next());"

Is This A Good Question/Topic? 0

## Replies To: Please Help! 2D Arrays

### #2 baavgai

• Dreaming Coder

Reputation: 6608
• Posts: 13,948
• Joined: 16-October 07

## Re: Please Help! 2D Arrays

Posted 14 May 2009 - 04:38 AM

A 2D array is almost certainly not what you want. You ultimately want a single array of Student, yes? You should probably use List<Student>.

Here's your error:
```// define 2D array of Student
Student[][] allstudentObjArray = new Student[10][12];
//...
// add an instance of stuAll, with a single string constructor
// to an array of Student objects
// did you really want to do this?
allstudentObjArray[i3][i2] = new stuAll(sa1.next());

```

Without knowing how Student looks:
```Student getStudentFromLine(String line) {
String [] field = line.split(":");
Student s = new Student();
s.setName(field[0]);
//...
return s;
}

public void searchStudent() throws Exception {
//Scanner scanner = new Scanner(System.in);
//Student[][] allstudentObjArray = new Student[10][12];

Student[] students = new Student[10];
int studentCount = 0;
Scanner allStu = new Scanner(new File("allstudents2.txt"));

while(allStu.hasNextLine()) {
students[studentCount++] = getStudentFromLine(allStu.nextLine());
}
System.out.println(students[0]);

```

Hope this helps.
Was This Post Helpful? 0

Page 1 of 1

 .related ul{list-style-type:circle;font-size:12px;font-weight:bold;}.related li{margin-bottom:5px;background-position:left 7px!important;margin-left:-35px;}.related h2{font-size:18px;font-weight:bold;}.related a{color:blue;}