Trying to read data from a txt file

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 2037 Views - Last Post: 11 November 2009 - 06:26 PM Rate Topic: -----

#1 putha-nee  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 168
  • Joined: 04-October 09

Trying to read data from a txt file

Posted 11 November 2009 - 01:28 PM

I'm trying to read data from a text file but for some reason, its only reading the first line of the txt file and not the rest.

ArrayList<String> contactInfo = new ArrayList<String>(); // ArrayList to hold information
		try
		{
			File read = new File("contactlist.txt");
		 	Scanner in = new Scanner(new File("contactlist.txt")); // scanner to read in the txt file
				FileInputStream fis = new FileInputStream(read);
		   	BufferedInputStream bis = new BufferedInputStream(fis);
			DataInputStream dis = new DataInputStream(bis);

			String name = "";
			String pNumber = "";

			//ArrayList<String> contactInfo = new ArrayList<String>(); // ArrayList to hold information
			
			while(in.hasNextLine()) // Loops through until the txt file is done
			{
	 			name = in.next() + " " + in.next(); // Gets the first name then adds space and Last name
	 			pNumber = in.next(); // gets the Phone Number
			in.nextLine(); // clears buffer to next line
	 			contactInfo.add(name); contactInfo.add(pNumber);
			}
			
			
			
			
			
		}
		catch(IOException e)
		{
			System.out.println("************** Error *****>>>>>>");
			System.out.println("* This File is unexisting or unreadable");
		}

This post has been edited by putha-nee: 11 November 2009 - 01:29 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Trying to read data from a txt file

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10662
  • View blog
  • Posts: 39,592
  • Joined: 27-December 08

Re: Trying to read data from a txt file

Posted 11 November 2009 - 01:35 PM

Can you post the file? I think you may not have a line-break that Scanner recognizes in there. Also, pay attention to the bolded part of the quote, which I took directly from the Scanner 6 API for the nextLine() method.

Quote

Advances this scanner past the current line and returns the input that was skipped. This method returns the rest of the current line, excluding any line separator at the end. The position is set to the beginning of the next line.

Since this method continues to search through the input looking for a line separator, it may buffer all of the input searching for the line to skip if no line separators are present.

Was This Post Helpful? 0
  • +
  • -

#3 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2874
  • View blog
  • Posts: 11,047
  • Joined: 15-July 08

Re: Trying to read data from a txt file

Posted 11 November 2009 - 01:39 PM

macosxnerd101, just to clarify, what are the line separators it searches for? "\r"? "\n"? both? more? Some OS text files store their data differently, so I'm just asking perhaps for putha-nee's sake.
Was This Post Helpful? 0
  • +
  • -

#4 sakshamkum  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 19
  • View blog
  • Posts: 232
  • Joined: 09-June 09

Re: Trying to read data from a txt file

Posted 11 November 2009 - 01:48 PM

you may need to use FileReader, BuferedReader if it is a text file. one problem could be that the input Stream is not closed with the method close(). the place where it went wrong may also be the statement that calls the next() function.
Was This Post Helpful? 0
  • +
  • -

#5 putha-nee  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 168
  • Joined: 04-October 09

Re: Trying to read data from a txt file

Posted 11 November 2009 - 01:52 PM

Bart Simpson 1(416)123-4567
Homer Simpson 1(416)987-6543
Marge Simpson 1(905)546-5469
John Doe 1(905)845-4464

There is the input file where the first line is a blank on purpose, then you have the lines formatted as:
[firstName] [lastName] [phoneN]

All of the elements are separated by spaces (has to be formatted like that)
Was This Post Helpful? 0
  • +
  • -

#6 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Trying to read data from a txt file

Posted 11 November 2009 - 01:52 PM

next() is a solid method in the API in scanner and Scanner will work perfectly fine for reading in a txt file.

I have been told the format is:
FirstName LastName PhoneNumber
The first 2 next() are to combine the first and last name, and then last one if for the Pone number as a string.

@pathnee
name = in.next() + " " + in.next(); // Gets the first name then adds space and Last name
				 pNumber = in.next(); // gets the Phone Number
			in.nextLine(); // clears buffer to next line


Remove the in.nextLine() that clears the buffer, and change pNumber to .nextLine()
Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10662
  • View blog
  • Posts: 39,592
  • Joined: 27-December 08

Re: Trying to read data from a txt file

Posted 11 November 2009 - 02:07 PM

View PostDogstopper, on 11 Nov, 2009 - 04:39 PM, said:

macosxnerd101, just to clarify, what are the line separators it searches for? "\r"? "\n"? both? more? Some OS text files store their data differently, so I'm just asking perhaps for putha-nee's sake.


Dos & Windows expect a newline as: \r\n
Unix & Linux: \n
Macs: \r

However, given the File layout, Fuzzyness' solution is better.
Was This Post Helpful? 0
  • +
  • -

#8 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2779
  • View blog
  • Posts: 11,764
  • Joined: 20-September 08

Re: Trying to read data from a txt file

Posted 11 November 2009 - 02:21 PM

You're trying to read the same file twice: once with the Scanner, and once with a DataInputStream. Use just the Scanner

This post has been edited by g00se: 11 November 2009 - 02:22 PM

Was This Post Helpful? 0
  • +
  • -

#9 putha-nee  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 168
  • Joined: 04-October 09

Re: Trying to read data from a txt file

Posted 11 November 2009 - 02:27 PM

Is this what you meant? I tried this and it still only reads the first Line. One other thing, in doing pnumber = in.nextLine(), isn't that telling it that the pNumber is on the next line?

	File read = new File("contactlist.txt");
		 	Scanner in = new Scanner(new File("contactlist.txt")); // scanner to read in the txt file
		

			String name = "";
			String pNumber = "";
			//ArrayList<String> contactInfo = new ArrayList<String>(); // ArrayList to hold information
		
			
			while(in.hasNextLine()) // Loops through until the txt file is done
			{
	 			name = in.next() + " " + in.next(); // Gets the first name then adds space and Last name
	 			pNumber = in.nextLine(); // gets the Phone Number
				//in.nextLine(); // clears buffer to next line
	 			contactInfo.add(name); contactInfo.add(pNumber);
			}
			
			
			
			
			
		}
		catch(IOException e)
		{
			System.out.println("************** Error *****>>>>>>");
			System.out.println("* This File is unexisting or unreadable");
		}






View PostFuzzyness, on 11 Nov, 2009 - 12:52 PM, said:

next() is a solid method in the API in scanner and Scanner will work perfectly fine for reading in a txt file.

I have been told the format is:
FirstName LastName PhoneNumber
The first 2 next() are to combine the first and last name, and then last one if for the Pone number as a string.

@pathnee
name = in.next() + " " + in.next(); // Gets the first name then adds space and Last name
				 pNumber = in.next(); // gets the Phone Number
			in.nextLine(); // clears buffer to next line


Remove the in.nextLine() that clears the buffer, and change pNumber to .nextLine()

Was This Post Helpful? 0
  • +
  • -

#10 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2874
  • View blog
  • Posts: 11,047
  • Joined: 15-July 08

Re: Trying to read data from a txt file

Posted 11 November 2009 - 02:30 PM

View Postputha-nee, on 11 Nov, 2009 - 03:27 PM, said:

Isn't that telling it that the pNumber is on the next line?


No, that is moving the reader to the next line and returning whatever of that line is remaining.
Was This Post Helpful? 0
  • +
  • -

#11 putha-nee  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 168
  • Joined: 04-October 09

Re: Trying to read data from a txt file

Posted 11 November 2009 - 02:31 PM

Okay but it still doesn't work because the code I just printed is still printing out only the first line. By the way, I need to store the phone number in pNumber which is why I had originally used the .next() but even with pNumber = in.nextLine(), its still not printing out anything after the first line

This post has been edited by putha-nee: 11 November 2009 - 02:32 PM

Was This Post Helpful? 0
  • +
  • -

#12 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2874
  • View blog
  • Posts: 11,047
  • Joined: 15-July 08

Re: Trying to read data from a txt file

Posted 11 November 2009 - 02:36 PM

What does your full current code look like?
Was This Post Helpful? 0
  • +
  • -

#13 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Trying to read data from a txt file

Posted 11 November 2009 - 02:37 PM

Add some debugging into your program. When its reading it in. Print out the variable. so...
fullName = in.next(); System.out.println(fullName);
fullName += " " + in.next(); System.out.println(fullName);
pNumber = in.nextLine(); System.out.println(pNumber);


That way you can see if it is even reading it in properly and if it is then you have a problem with storing it.
Was This Post Helpful? 0
  • +
  • -

#14 putha-nee  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 168
  • Joined: 04-October 09

Re: Trying to read data from a txt file

Posted 11 November 2009 - 02:56 PM

Okay, so its printing out all the data in the txt file but all the entries are not being stored in the arraylist prorperly. What I am trying to do is get the arraylist name's to be read into a combo box. Here is the code for the combo box.
JPanel p5 = new JPanel();
	//*************************************************************************
	for(String i : contactInfo)
		{
		JComboBox cb1;
		cb1 = new JComboBox();
		cb1.addItem(contactInfo.get(0));

		p5.add(cb1);
		}
	//*************************************************************************
	

	p5.setLayout(new GridLayout(1,1));
		cp.add(p5, BorderLayout.SOUTH);



Its for some reason not reading the first entry in the txt file which is a blank line and instead reading just the first name and bringing that up. I am guessing it is something with the loop I am using or the way that its setup?






View PostFuzzyness, on 11 Nov, 2009 - 01:37 PM, said:

Add some debugging into your program. When its reading it in. Print out the variable. so...
fullName = in.next(); System.out.println(fullName);
fullName += " " + in.next(); System.out.println(fullName);
pNumber = in.nextLine(); System.out.println(pNumber);


That way you can see if it is even reading it in properly and if it is then you have a problem with storing it.

This post has been edited by putha-nee: 11 November 2009 - 02:57 PM

Was This Post Helpful? 0
  • +
  • -

#15 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2874
  • View blog
  • Posts: 11,047
  • Joined: 15-July 08

Re: Trying to read data from a txt file

Posted 11 November 2009 - 02:59 PM

Maybe try before the loop?
in.nextLine();



And then that will skip your first line altogether then you begin the loop...?
before this:

fullName = in.next(); System.out.println(fullName);
fullName += " " + in.next(); System.out.println(fullName);
pNumber = in.nextLine(); System.out.println(pNumber);



Fuzziness was right, but maybe it didn't into account the blank line.

This post has been edited by Dogstopper: 11 November 2009 - 03:01 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2