By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,365 Members | 2,946 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,365 IT Pros & Developers. It's quick & easy.

Need help with 3 elements of programming. Thanks!

P: 53
Ok I am working on a Hall Pass program for my computer programming class. There are 3 things that I am confused on.

1. Reading a file.

2. Taking that data read from the file and putting it into an array.

3. A way to access the array, and check if it matches what the user inputed (like a login program)

This is what the text file looks like (unless you have a easier / more efficient way of doing it, I was reading somewhere, about tokenizer's although I don't completely understand how to use them.)

0002525325
John
00234234
Chris
43244232
Mark

So, basically what i want the program to do is, the user enters a id number. The text file is checked and if the id number that is entered equals one of these then their name is displayed.

Lastly, I just want to say, i DO NOT want you to write this program for me, I just need help, i need a place to start, i need to understand what I am actually doing.

Heres some code that i think might help

Expand|Select|Wrap|Line Numbers
  1. while ((tempStr = br.readLine()) != null)  //reading the file
  2.             line.add(tempStr);
  3.  
  4.             int[][] map = new int[line.size()][];
  5.  
  6.             for (int i = 0; i < map.length; i++)
  7.             {
  8.                 tempStr = (String) line.get(i);
  9.  
  10.                 StringTokenizer st = new StringTokenizer(s, ",");  //explain please?
  11.  
  12.                 int[] RTarray = new int[st.countTokens()]; //explain?
  13.  
  14.                 map[i] = RTarray;
  15.  
  16.             }

Thanks for the help, i wan to say this again, i dont want someone to do my homework, i want someone to help me get my homework done.
Oct 1 '07 #1
Share this Question
Share on Google+
30 Replies


hsn
100+
P: 237
hsn
ok
this problem is not hard. actually it is very easy one.
i will give you HINTS in how to answer this question

-you don't need a string tokenizer, because a string tokenizer is used to seperate words in a string
For an example
string s="hey how are you"
a string tokenizer could seperate each word
so you can store hey in a string and then how and so on...

- to read from the file use a getline() function and then store it in the array..
read the file in a while loop and say , while the file is not in the end
do so and so and so
and then compare the input with the information that you stored in the arrays

I hope it was good

Good LUCK

regards
hsn
Oct 1 '07 #2

10K+
P: 13,264
Ok I am working on a Hall Pass program for my computer programming class. There are 3 things that I am confused on.

1. Reading a file.

2. Taking that data read from the file and putting it into an array.

3. A way to access the array, and check if it matches what the user inputed (like a login program)

This is what the text file looks like (unless you have a easier / more efficient way of doing it, I was reading somewhere, about tokenizer's although I don't completely understand how to use them.)

0002525325
John
00234234
Chris
43244232
Mark

So, basically what i want the program to do is, the user enters a id number. The text file is checked and if the id number that is entered equals one of these then their name is displayed.

Lastly, I just want to say, i DO NOT want you to write this program for me, I just need help, i need a place to start, i need to understand what I am actually doing.

Heres some code that i think might help

Expand|Select|Wrap|Line Numbers
  1. while ((tempStr = br.readLine()) != null)  //reading the file
  2.             line.add(tempStr);
  3.  
  4.             int[][] map = new int[line.size()][];
  5.  
  6.             for (int i = 0; i < map.length; i++)
  7.             {
  8.                 tempStr = (String) line.get(i);
  9.  
  10.                 StringTokenizer st = new StringTokenizer(s, ",");  //explain please?
  11.  
  12.                 int[] RTarray = new int[st.countTokens()]; //explain?
  13.  
  14.                 map[i] = RTarray;
  15.  
  16.             }

Thanks for the help, i wan to say this again, i dont want someone to do my homework, i want someone to help me get my homework done.
Read this.
Oct 1 '07 #3

10K+
P: 13,264
ok
this problem is not hard. actually it is very easy one.
i will give you HINTS in how to answer this question

-you don't need a string tokenizer, because a string tokenizer is used to seperate words in a string
For an example
string s="hey how are you"
a string tokenizer could seperate each word
so you can store hey in a string and then how and so on...

- to read from the file use a getline() function and then store it in the array..
read the file in a while loop and say , while the file is not in the end
do so and so and so
and then compare the input with the information that you stored in the arrays

I hope it was good

Good LUCK

regards
hsn
I hope you realize that this the Java (not C++) forum.
Oct 1 '07 #4

hsn
100+
P: 237
hsn
I hope you realize that this the Java (not C++) forum.
i do know that and this answer is good
i have tried it before and it worked
and also there is no difference between c++ and java because they are similer in how to answer a question
Oct 1 '07 #5

Expert 10K+
P: 11,448
@OP: read the API documentation for the Scanner or BufferedReader classes.
On top of that you should read about the Map interface. Believe me: you don't
want to use arrays for this because arrays are sooooo Fortranesque and arrays have
a fixed size 'n' and according to Murphy one day you want to read a file with a least
'n+1' id/name pairs and most important is: Maps are soft for your hands ;-)

kind regards,

Jos
Oct 1 '07 #6

hsn
100+
P: 237
hsn
ok
this problem is not hard. actually it is very easy one.
i will give you HINTS in how to answer this question

-you don't need a string tokenizer, because a string tokenizer is used to seperate words in a string
For an example
string s="hey how are you"
a string tokenizer could seperate each word
so you can store hey in a string and then how and so on...

- to read from the file use a getline() function and then store it in the array..
read the file in a while loop and say , while the file is not in the end
do so and so and so
and then compare the input with the information that you stored in the arrays

I hope it was good

Good LUCK

regards
hsn
SORRY i put getline() for reading from a file
but i ment that you can use a readline() function
sorry again
Oct 1 '07 #7

hsn
100+
P: 237
hsn
use vectors for storing the information that you want
you don't need to entering a size for the vector and anytime you want to add an item you just use addElement() function

when you use vector you have to choose a type for the vector
research more about it

GOOD LUCK
hsn
Oct 1 '07 #8

Expert 10K+
P: 11,448
use vectors
please don't.

kind regards,

Jos
Oct 1 '07 #9

hsn
100+
P: 237
hsn
please don't.

kind regards,

Jos
why not??????????

hsn
Oct 1 '07 #10

10K+
P: 13,264
why not??????????

hsn
... and readline() should really have been readLine()?
Read the API docs for the classes being discussed and you'll find out why you don't want to use a vector these days.

P.S Welcome to the nitpickers club.
Oct 1 '07 #11

hsn
100+
P: 237
hsn
... and readline() should really have been readLine()?
Read the API docs for the classes being discussed and you'll find out why you don't want to use a vector these days.

P.S Welcome to the nitpickers club.
listen i'm here to learn and help anyone
is there any problem with vectors
and if there is please tell me

hsn
Oct 1 '07 #12

Expert 10K+
P: 11,448
listen i'm here to learn and help anyone
is there any problem with vectors
and if there is please tell me

hsn
It synchronizes (expensive!) on elementary CRUD (Create, Read, Update, Delete)
access for no particular reason. Use Lists instead but for this particular little
problem a Map would be much much better,

kind regards,

Jos
Oct 1 '07 #13

P: 53
Heres some code that i have written, its not working, all the values that are printed out come out "Null". If you are wondering what the text file looks like refer to my first post. Thanks for the help guys. Also eventually this is not gunna be the main, and i know this is simple but, im gunna need to make this not the main and its gunna need to send the values to the main program. Im using blueJ btw.

Thanks, sorry if i sound like a fool,but i am still new at java.


Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2.  
  3.   class FileReadTest { 
  4.  
  5.  
  6.  
  7.      public static void main (String[] args) {
  8.         FileReadTest t = new FileReadTest();
  9.         t.readMyFile();
  10.      } 
  11.  
  12.  
  13.    public void readMyFile() { 
  14.        String[] id;
  15.        String[] name;
  16.         id = new String[6];
  17.         name = new String[6];
  18.         String record = null;
  19.         String record1 = null;
  20.         int recCount = 0;
  21.         int recCount1 = 0;
  22.         int x1 = -1;
  23.         int x2 = 0;
  24.         try { 
  25.  
  26.        FileReader fr     = new FileReader("hall.txt");
  27.            BufferedReader br = new BufferedReader(fr);
  28.  
  29.            record = new String();
  30.            while ((record = br.readLine()) != null) {
  31.                record = id[x1+2];
  32.  
  33.               recCount++;
  34.               System.out.println(id[1]); 
  35.               System.out.println(id[3]); 
  36.               System.out.println(id[5]); 
  37.            }
  38.            record1 = new String();
  39.             while ((record1 = br.readLine()) != null) {
  40.                record1 = name[x1+2];
  41.  
  42.               recCount1++;
  43.               System.out.println(name[2]); 
  44.               System.out.println(name[4]);
  45.               System.out.println(name[6]);
  46.            } 
  47.  
  48.         } catch (IOException e) { 
  49.            // catch possible io errors from readLine()
  50.            System.out.println("Uh oh, got an IOException error!");
  51.            e.printStackTrace();
  52.         }
  53.  
  54.      } // end of readMyFile()
  55.  
  56.   } // end of class
Oct 1 '07 #14

10K+
P: 13,264
Heres some code that i have written, its not working, all the values that are printed out come out "Null". If you are wondering what the text file looks like refer to my first post. Thanks for the help guys. Also eventually this is not gunna be the main, and i know this is simple but, im gunna need to make this not the main and its gunna need to send the values to the main program. Im using blueJ btw.

Thanks, sorry if i sound like a fool,but i am still new at java.


Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2.  
  3.   class FileReadTest { 
  4.  
  5.  
  6.  
  7.      public static void main (String[] args) {
  8.         FileReadTest t = new FileReadTest();
  9.         t.readMyFile();
  10.      } 
  11.  
  12.  
  13.    public void readMyFile() { 
  14.        String[] id;
  15.        String[] name;
  16.         id = new String[6];
  17.         name = new String[6];
  18.         String record = null;
  19.         String record1 = null;
  20.         int recCount = 0;
  21.         int recCount1 = 0;
  22.         int x1 = -1;
  23.         int x2 = 0;
  24.         try { 
  25.  
  26.        FileReader fr     = new FileReader("hall.txt");
  27.            BufferedReader br = new BufferedReader(fr);
  28.  
  29.            record = new String();
  30.            while ((record = br.readLine()) != null) {
  31.                record = id[x1+2];
  32.  
  33.               recCount++;
  34.               System.out.println(id[1]); 
  35.               System.out.println(id[3]); 
  36.               System.out.println(id[5]); 
  37.            }
  38.            record1 = new String();
  39.             while ((record1 = br.readLine()) != null) {
  40.                record1 = name[x1+2];
  41.  
  42.               recCount1++;
  43.               System.out.println(name[2]); 
  44.               System.out.println(name[4]);
  45.               System.out.println(name[6]);
  46.            } 
  47.  
  48.         } catch (IOException e) { 
  49.            // catch possible io errors from readLine()
  50.            System.out.println("Uh oh, got an IOException error!");
  51.            e.printStackTrace();
  52.         }
  53.  
  54.      } // end of readMyFile()
  55.  
  56.   } // end of class
Better but still bad. I hope you read Jos' advice.
Oct 1 '07 #15

P: 53
By the way, i have to use some kind of array, thats what my instructor wants me to use.
Oct 1 '07 #16

Expert 10K+
P: 11,448
Thanks, sorry if i sound like a fool,but i am still new at java.
You don't sound like a fool but please do read up on Maps and BufferedReaders.
It'll remove so much code, as in:

Expand|Select|Wrap|Line Numbers
  1. String id;
  2. String name;
  3. BufferedReader br= // you got this from somewhere else
  4. Map<String, String> map= new HashMap<String, String>(); // your map
  5.  
  6. while ((id= br.readLine()) != null && (name= br.readLine()) != null) 
  7.    map.put(id, name); // store a new association
  8.  
kind regards,

Jos (<--- party pooper, spoil sport and thirsty ;-)
Oct 1 '07 #17

P: 53
I understand your trying to help me, but im going to be completely honest, i have no clue what a map is, or how to use one, but im trying to understand and read up on it at the moment.
Oct 1 '07 #18

P: 53
Would using an arraylist work?
Oct 1 '07 #19

Expert 10K+
P: 11,448
I understand your trying to help me, but im going to be completely honest, i have no clue what a map is, or how to use one, but im trying to understand and read up on it at the moment.
Good; reading documentation is a fine habit. If you get a grip on those maps we
can talk further.

kind regards,

Jos
Oct 1 '07 #20

Expert 10K+
P: 11,448
Would using an arraylist work?
Sort of, but take it from me: you don't want to use an (Array)List either: Maps are
your friend here.

kind regards,

Jos
Oct 1 '07 #21

hsn
100+
P: 237
hsn
so you are instructed to use arrays....
there is a way.
if you know how many users in the file just create two arrays one for the numbers that you have and the other for the strings
if you don't know how many users in the file maps, vectors, arraylist are good to be used
other than that
an idea came to me now,
read the file twice once for knowing how many users you have in you file
and the other to read them and store them

GOOD LUCK

hsn
Oct 1 '07 #22

hsn
100+
P: 237
hsn
but still if you can use maps, vectors, arraylist it will be better for sure than reading a file twice

hsn
Oct 1 '07 #23

Expert 10K+
P: 11,448
but still if you can use maps, vectors, arraylist it will be better for sure than reading a file twice

hsn
Slowly by slowly you'll converge to maps ;-)

kind regards,

Jos
Oct 1 '07 #24

P: 53
Yeah, i know how many id numbers and names are in the array, 3 each. I want to do this with arrays or arraylists, will someone just show me how this would work? I just need a layout or somthing to start with.

Thanks!
Oct 2 '07 #25

hsn
100+
P: 237
hsn
Yeah, i know how many id numbers and names are in the array, 3 each. I want to do this with arrays or arraylists, will someone just show me how this would work? I just need a layout or somthing to start with.

Thanks!
ok here is a way to answer your question using an array of string
read the input into the array in order
so a[0] will be id1 a[1] will be name1 a[2] for id2 and a[3] for name2 and so on......
after that just compare the input that the user will enter with the array.
that is it .

very simple way for answering your question

GOOD LUCK

hsn
Oct 2 '07 #26

Expert 10K+
P: 11,448
Since the OP is being lured into programming a Fortranesque solution, I'll show
my idea of how this little problem should be handled:

Expand|Select|Wrap|Line Numbers
  1. String id;
  2. String name;
  3. BufferedReader br= // you got this from somewhere else
  4. Map<String, String> map= new HashMap<String, String>(); // your map
  5.  
  6. // read all the id,name pairs and store them for later 
  7. while ((id= br.readLine()) != null && (name= br.readLine()) != null) 
  8.    map.put(id, name); // store a new association
  9.  
  10. // find a name given an id
  11. id= "0123456789";
  12. if ((name= map.get(id)) != null) 
  13.    System.out.println(id+" does not exist");
  14. else
  15.    System.out.println(id+": "+name);
  16.  
Happy hacking with the arrays.

kind regards,

Jos
Oct 2 '07 #27

P: 53
I have to do this with a GUI, so the user will enter it in a textfield, it runs what the user entered through the map, then displays the name.
Oct 2 '07 #28

P: 53
The map way seems like an awesome way to do it, thanks.
Oct 2 '07 #29

P: 53
But how does the map know which id is associated with each name?
Oct 2 '07 #30

10K+
P: 13,264
But how does the map know which id is associated with each name?
See Jos' comment on line number 8 of his code.
Oct 3 '07 #31

Post your reply

Sign in to post your reply or Sign up for a free account.