2D Array

read file into 2D Array and sort

Page 1 of 1

9 Replies - 10017 Views - Last Post: 05 May 2008 - 08:38 PM Rate Topic: -----

#1 lil_trangy  Icon User is offline

  • New D.I.C Head

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

2D Array

Posted 30 April 2008 - 05:16 PM

This is my task.

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;
	 nr = P7Helpers.read(a);
	 System.out.println("Data read in:  ");
	 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;

	  FileReader fr = new FileReader("seven.txt");
	  BufferedReader ins = new BufferedReader (fr);

	  s = ins.readLine();
	  while (s != null)
	  {
	   try
		{
		  a[row][col] = Integer.parseInt(s);
	  }
	 catch(NumberFormatException e)
	 {
	  System.out.println("Bad data");
	  s = ins.readLine();
	  System.exit(0);
   }
	  row++;
	  s = ins.readLine();
   }

	  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" :huh:

Is This A Good Question/Topic? 0
  • +

Replies To: 2D Array

#2 pbl  Icon User is offline

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

Reputation: 8346
  • View blog
  • Posts: 31,908
  • 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);
	  }


Was This Post Helpful? 0
  • +
  • -

#3 lil_trangy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#4 rgfirefly24  Icon User is online

  • D.I.C Lover
  • member icon


Reputation: 294
  • View blog
  • Posts: 1,533
  • Joined: 07-April 08

Re: 2D Array

Posted 01 May 2008 - 08:09 AM

View Postlil_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.
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

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

Reputation: 8346
  • View blog
  • Posts: 31,908
  • Joined: 06-March 08

Re: 2D Array

Posted 01 May 2008 - 01:25 PM

View Postrgfirefly24, on 1 May, 2008 - 08:09 AM, said:

View Postlil_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

Was This Post Helpful? 0
  • +
  • -

#6 lil_trangy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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/
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

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

Reputation: 8346
  • View blog
  • Posts: 31,908
  • 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());
Was This Post Helpful? 0
  • +
  • -

#8 lil_trangy  Icon User is offline

  • New D.I.C Head

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

Re: 2D Array

Posted 01 May 2008 - 06:54 PM

yay it worked! thanks :]
Was This Post Helpful? 0
  • +
  • -

#9 lil_trangy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

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

Reputation: 8346
  • View blog
  • Posts: 31,908
  • Joined: 06-March 08

Re: 2D Array

Posted 05 May 2008 - 08:38 PM

View Postlil_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
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1