Welcome to Dream.In.Code
Become a Java Expert!

Join 149,477 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,809 people online right now. Registration is fast and FREE... Join Now!




Java program

 
Reply to this topicStart new topic

Java program

aknish
18 Apr, 2007 - 07:48 PM
Post #1

New D.I.C Head
*

Joined: 18 Apr, 2007
Posts: 1


My Contributions
the program instructions are:

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.

The text book that I am using is Java, An Introduction to Problem Solving & Programming - Walter Savitch

i have the following:
CODE

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

public class Lab6
{
int[]a = new int[30];
int mr;
mr = Class.read(a);
class (main1);
{
public static in read
(int[]x)x.length;
}

int[][]a = new int[30][3];
int nRows,numrows,numcols;
nRows=Class.read(a);
class(main2);
{
public static int read(int[][]x)

x.length=numrows;
x[]length=numcols;
}
        count = Helper.read(a);
        System.out.println("Data read in: ");
        Helper.print(a, count);
        Helper.sort(a, count);
        System.out.println("Data sorted by ID: ");
        Helper.print(a, count);
        Helper.lookup(a, b, count);
        //Helper.print(b, count);
        Helper.sort(b, count);
        System.out.println("Data sorted by SS w/ row index: ");
        Helper.print2(b, count);
}
}


please tell me where i am wrong/right

thanks a lot
User is offlineProfile CardPM
+Quote Post

AngeluS
RE: Java Program
18 Apr, 2007 - 10:06 PM
Post #2

D.I.C Head
Group Icon

Joined: 10 Apr, 2007
Posts: 229


Dream Kudos: 25
My Contributions
Please provide us with the error you are getting and place your code within code-tags when posting on this forum.
User is offlineProfile CardPM
+Quote Post

lil_trangy
RE: Java Program
28 Apr, 2008 - 12:30 PM
Post #3

New D.I.C Head
*

Joined: 31 Mar, 2008
Posts: 28


My Contributions
hey, i have to do the same program and i'm having a little trouble..can someone help?

i have the following:


in Main,

CODE


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

public class Lab_7
{
   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: ");
     P7Helpers.print(a);
   }
}



in another class,

CODE

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

public class P7Helpers
{
   public static int read(int[][] a) throws IOException
   {
    int r = 0;
    String s;

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

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

      ins.close();
      return r;
   }



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



and i got this little error:
CODE

./P7Helpers.java:19: incompatible types
found   : int
required: int[]
          a[r] = Integer.parseInt(s);
                                 ^
1 error



help please =/
User is offlineProfile CardPM
+Quote Post

pbl
RE: Java Program
28 Apr, 2008 - 01:28 PM
Post #4

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,560



Thanked: 231 times
Dream Kudos: 75
My Contributions
CODE

./P7Helpers.java:19: incompatible types
found   : int
required: int[]
          a[r] = Integer.parseInt(s);
                                 ^
1 error

your a is a int[][]

so you need to double index it like:

a[row][r] = Integer.parseInt(s);
User is offlineProfile CardPM
+Quote Post

lil_trangy
RE: Java Program
28 Apr, 2008 - 02:01 PM
Post #5

New D.I.C Head
*

Joined: 31 Mar, 2008
Posts: 28


My Contributions
but it comes up with 'Bad Data' instead of printing out the data file..why is that huh.gif
User is offlineProfile CardPM
+Quote Post

lil_trangy
RE: Java Program
28 Apr, 2008 - 07:16 PM
Post #6

New D.I.C Head
*

Joined: 31 Mar, 2008
Posts: 28


My Contributions
help anyone? =/
User is offlineProfile CardPM
+Quote Post

Ellie
RE: Java Program
30 Apr, 2008 - 05:21 AM
Post #7

D.I.C Regular
Group Icon

Joined: 17 Jan, 2007
Posts: 427



Thanked: 4 times
Dream Kudos: 150
My Contributions
Well, you're still using a 1D array in other parts of your programme like the print function, so that won't work
System.out.println(a[i]);

Have you changed the whole program to use a 2D array? If so, could you post your new code? What's in the text file? Maybe it does not contain numbers, one to a line?


User is offlineProfile CardPM
+Quote Post

lil_trangy
RE: Java Program
30 Apr, 2008 - 01:35 PM
Post #8

New D.I.C Head
*

Joined: 31 Mar, 2008
Posts: 28


My Contributions
this is what i have in my data file

CODE

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:

CODE

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);
   }
}


in another Class:
CODE
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:
CODE

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 sleep.gif
i get no error when i compile it but when i try to run it, i get "Bad Data" huh.gif
User is offlineProfile CardPM
+Quote Post

lil_trangy
RE: Java Program
30 Apr, 2008 - 04:17 PM
Post #9

New D.I.C Head
*

Joined: 31 Mar, 2008
Posts: 28


My Contributions
i thought it be better if i open this as my own thread so if u could give me some pointers, this is my thread

http://www.dreamincode.net/forums/index.php?showtopic=50669
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 03:00PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month