473,405 Members | 2,261 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

reading lines

why doesnt this work i get null values any suggestions? thanks.
Expand|Select|Wrap|Line Numbers
  1.  
  2.      if (event.getSource() == submit)
  3.         {
  4.            try {
  5.         BufferedReader in = new BufferedReader(new FileReader("names.txt"));
  6.        if (counter < 3)
  7.        {
  8.            namestring = in.readLine();
  9.            System.out.println(namestring);
  10.            namearray[a] = namestring;
  11.              //namearray.add(a,namestring);
  12.            counter++;
  13.            a++;
  14.         }
  15.         else if (counter > 3)
  16.         {
  17.           idstring = in.readLine();
  18.  
  19.  
  20.            idint = Integer.parseInt(idstring);
  21.           //  idarray.add(b,idstring);
  22.           idarray[b] = idint;
  23.           counter++;
  24.           b++;
  25.         }
  26.  
  27.  
  28.         in.close();
  29.         for(int a = 0;  a< 3; a++)
  30.         {
  31.             System.out.println(namearray[a]);
  32.         }
  33.         for(int b = 0;  b< 3; b++)
  34.         {
  35.             System.out.println(idarray[b]);
  36.         }
  37.  
Oct 21 '07 #1
5 1066
JosAH
11,448 Expert 8TB
What do you do when counter ==3?

kind regards,

Jos
Oct 21 '07 #2
oh. thats an error. would i make the second one >2?
Oct 21 '07 #3
JosAH
11,448 Expert 8TB
oh. thats an error. would i make the second one >2?
If a number is *not* < 3 it is >= 3; < and >= are complementary; try it with a
couple of examples.

kind regards,

Jos
Oct 21 '07 #4
the txt looks like this
greg
john
joe
54343
45777
33444

i run this program, and it displays

greg
greg
null
0
0
0

whats up? thanks.


Expand|Select|Wrap|Line Numbers
  1.  if (event.getSource() == submit)
  2.         {
  3.            try {
  4.         BufferedReader in = new BufferedReader(new FileReader("names.txt"));
  5.        if (counter <= 3)
  6.        {
  7.            namestring = in.readLine();
  8.  
  9.            namearray[a] = namestring;
  10.              //namearray.add(a,namestring);
  11.            counter++;
  12.            a++;
  13.         }
  14.         else if (counter > 3)
  15.         {
  16.           idstring = in.readLine();
  17.  
  18.  
  19.            idint = Integer.parseInt(idstring);
  20.           //  idarray.add(b,idstring);
  21.           idarray[b] = idint;
  22.           counter++;
  23.           b++;
  24.         }
  25.  
  26.  
  27.         in.close();
  28.         for(int a = 0;  a< 3; a++)
  29.         {
  30.             System.out.println(namearray[a]);
  31.         }
  32.         for(int b = 0;  b< 3; b++)
  33.         {
  34.             System.out.println(idarray[b]);
  35.         }
  36.  
  37.     } catch (IOException e) {
  38.     }
Oct 21 '07 #5
JosAH
11,448 Expert 8TB
You do realize that you're opening a new BufferedReader everytime you press
that button don't you? A new BufferedReader starts reading from the start over
and over again.

kind regards,

Jos
Oct 21 '07 #6

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

Similar topics

6
by: Raymond Hettinger | last post by:
Found in a pamphlet at a pre-school: --------------------------------------- Reading improves vocabulary Reading raises cultural literacy through shared knowledge Reading develops writing skills...
20
by: sahukar praveen | last post by:
Hello, I have a question. I try to print a ascii file in reverse order( bottom-top). Here is the logic. 1. Go to the botton of the file fseek(). move one character back to avoid the EOF. 2....
6
by: KevinD | last post by:
assumption: I am new to C and old to COBOL I have been reading a lot (self teaching) but something is not sinking in with respect to reading a simple file - one record at a time. Using C, I am...
4
by: Gaijinco | last post by:
I had a file named nap.in which looks like this: 4 10:00 12:00 Lectures 12:00 13:00 Lunch, like always. 13:00 15:00 Boring lectures... 15:30 17:45 Reading 4 10:00 12:00 Lectures 12:00 13:00...
3
by: The Cool Giraffe | last post by:
Regarding the following code i have a problem. void read () { fstream file; ios::open_mode opMode = ios::in; file.open ("some.txt", opMode); char *ch = new char; vector <charv; while...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.