# 2D Array

Page 1 of 1

## 9 Replies - 11947 Views - Last Post: 05 May 2008 - 08:38 PMRate 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=50669&amp;s=f60698b21661a6c8c464594a3987a0b0&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 lil_trangy

Reputation: 0
• Posts: 28
• Joined: 31-March 08

# 2D Array

Posted 30 April 2008 - 05:16 PM

Read a sequence of id numbers, telephone numbers and social security numbers into a two-dimensional array of 50 rows.
Each row in the array will contain an individual's id number, telephone number and SSN.
Do not read the input data from the keyboard.
Put a test file on the disk of atleast 15 rows and use it to test your program.
Sort the array rows into ascending id number order.
Then create a two-dimensional array where each row contains the social security number and the row index of the row which contains the SSN in the original array.
This array is referred to as the 'look -up table'
Sort the 'look-up table' in ascending order, based on the social security numbers(SSN).
Search the 'look-up table' for a social security number and retrieve the id number and telephone number of the person associated with the telephone number from the original array using the index number.
Write the search method so that multiple social security numbers can be accessed.

This is what i have so far.

My data:
```320 2543 65540
567 3047 31107
453 5647 85634
122 3708 30566
857 8464 20995
651 9826 10057
152 1253 35098
507 6873 68742
365 2486 35772
341 2555 36567
250 3468 56874
874 6251 87513
503 8755 32401
668 3547 96741
245 2154 55610
102 9554 65046
690 2579 35491
987 6475 20874
542 3541 80141
339 4157 31475
872 3254 65008
146 3587 68321
656 8752 85221
755 9740 54004
344 3648 65400
985 9871 83465
205 6872 92415
788 2486 54004
254 2546 35480
482 8912 75645
635 7861 21684
999 5213 52867
645 8963 18565
365 2843 89765
357 1506 64148
897 3502 65414
546 8752 15004
874 2348 15160
547 3272 48932
180 3870 32481
516 8463 54756
868 7043 20058
675 1258 54469
102 3861 98514
863 2015 28854
614 6355 35184
862 3674 25487
854 1305 37891
213 9355 87841
301 2505 30960
```

my Main:
```import java.util.*;
import java.io.*;

public class test7
{
public static void main (String[] args) throws IOException
{
int[][] a = new int[50][3];
int nr;
HelpersTest7.print(a);
}
}

```

Another Class:
```import java.util.*;
import java.io.*;

public class HelpersTest7
{
public static int read(int[][] a) throws IOException
{
int row = 0;
int col = 0;
String s;

while (s != null)
{
try
{
a[row][col] = Integer.parseInt(s);
}
catch(NumberFormatException e)
{
System.exit(0);
}
row++;
}

ins.close();
return row;
}

public static void print(int[][] a)
{
int i, j;
for (i = 0; i < a.length; i++)
{
System.out.println();
for (j = 0;j<a[i].length; j++)
{
System.out.println(a[i][j]);
}
}
return;
}
}

```

when i first did this, this print out:

```Data read in:

320
2543
65540

567
3047
31107

453
5647
85634

122
3708
30566

857
8464
20995

651
9826
10057

152
1253
35098

507
6873
68742

365
2486
35772

341
2555
36567

250
3468
56874

874
6251
87513

503
8755
32401

668
3547
96741

245
2154
55610

102
9554
65046

690
2579
35491

987
6475
20874

542
3541
80141

339
4157
31475

872
3254
65008

146
3587
68321

656
8752
85221

755
9740
54004

344
3648
65400

985
9871
83465

205
6872
92415

788
2486
54004

254
2546
35480

482
8912
75645

635
7861
21684

999
5213
52867

645
8963
18565

365
2843
89765

357
1506
64148

897
3502
65414

546
8752
15004

874
2348
15160

547
3272
48932

180
3870
32481

516
8463
54756

868
7043
20058

675
1258
54469

102
3861
98514

863
2015
28854

614
6355
35184

862
3674
25487

854
1305
37891

213
9355
87841

301
2505
30960

```

but for some reason, it's not working anymore
i get no error when i compile it but when i try to run it, i get "Bad Data"

Is This A Good Question/Topic? 0

## Replies To: 2D Array

### #2 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: 2D Array

Posted 30 April 2008 - 08:34 PM

Probably and empty line at the end of your file with only a <CR>
Instead of just checking for s != null test also the length of the line

Do

```while (s != null && s.length() > 4)
{
try
{
a[row][col] = Integer.parseInt(s);
}

```

### #3 lil_trangy

Reputation: 0
• Posts: 28
• Joined: 31-March 08

## Re: 2D Array

Posted 01 May 2008 - 04:18 AM

i tried that and everything that i know n i still get Bad Data
i just dont understand why it worked the first time then now it doesnt argh.

This post has been edited by lil_trangy: 01 May 2008 - 04:19 AM

### #4 rgfirefly24

• D.I.C Lover

Reputation: 448
• Posts: 2,184
• Joined: 07-April 08

## Re: 2D Array

Posted 01 May 2008 - 08:09 AM

lil_trangy, on 1 May, 2008 - 04:18 AM, said:

i tried that and everything that i know n i still get Bad Data
i just dont understand why it worked the first time then now it doesnt argh.

does the bad data happen on your first pass through the while loop or sequential passes? Also have you tried to system.out.println your array to check what data is going into it? not sure what program your using to write this, but if they have a step over mode i'd use that to check the data that is actually entering the variables.

### #5 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: 2D Array

Posted 01 May 2008 - 01:25 PM

rgfirefly24, on 1 May, 2008 - 08:09 AM, said:

lil_trangy, on 1 May, 2008 - 04:18 AM, said:

i tried that and everything that i know n i still get Bad Data
i just dont understand why it worked the first time then now it doesnt argh.

does the bad data happen on your first pass through the while loop or sequential passes? Also have you tried to system.out.println your array to check what data is going into it? not sure what program your using to write this, but if they have a step over mode i'd use that to check the data that is actually entering the variables.

Good suggestion from rgfirefly and will also made one
"Bad data" is your interpretation of what happened... would like to see the real error message and the line causing it

``` catch(NumberFormatException e)
{
System.out.println("Bad data: " + e + " on line: /" + s + "/");

```

This post has been edited by pbl: 01 May 2008 - 01:31 PM

### #6 lil_trangy

Reputation: 0
• Posts: 28
• Joined: 31-March 08

## Re: 2D Array

Posted 01 May 2008 - 04:38 PM

this pops up

Bad data: java.lang.NumberFormatException: For input string: "320 2543 65540" on line: /320 2543 65540/

### #7 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: 2D Array

Posted 01 May 2008 - 05:10 PM

So you have your answer... quite clear... should have done that before
You try to translate into an int the whole line
You will have to separate the 3 numbers with a StingTokenizer for example

StringTokenizer st = new StringTokenizer(s);
a[row][0] = Integer.parse(st.nextToken());
a[row][1] = Integer.parse(st.nextToken());
a[row][2] = Integer.parse(st.nextToken());

### #8 lil_trangy

Reputation: 0
• Posts: 28
• Joined: 31-March 08

## Re: 2D Array

Posted 01 May 2008 - 06:54 PM

yay it worked! thanks :]

### #9 lil_trangy

Reputation: 0
• Posts: 28
• Joined: 31-March 08

## Re: 2D Array

Posted 05 May 2008 - 05:16 PM

can someone help me with sorting the array rows into ascending id number order.

i know how to sort values in ascending order but i'm a bit confused with it considering it's with 2D array

is there somewhere i can read up on this since i can't seem to find anything on it argh =/

This post has been edited by lil_trangy: 05 May 2008 - 05:17 PM

### #10 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: 2D Array

Posted 05 May 2008 - 08:38 PM

lil_trangy, on 5 May, 2008 - 05:16 PM, said:

can someone help me with sorting the array rows into ascending id number order.

i know how to sort values in ascending order but i'm a bit confused with it considering it's with 2D array

is there somewhere i can read up on this since i can't seem to find anything on it argh =/

If you did it with a single array you probably have something like that:

```// if I have to switch a[i] with a[j]
if(a[i] > a[j]) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}

```

now with a 2D array you will to switch the whole row...
so temp will be a int[] instead of a int
assuming you are testing on the first column

```// if I have to swith a[i][0], a[i][1] and a[i][2] with a[j][0], a[j][1] and a[j][2]
if(a[i][0] > a[j][0]) {
int[] temp = a[i];
a[i] = a[j];
a[j] = temp;
}

```

not a big change