8 Replies - 4169 Views - Last Post: 30 August 2009 - 07:45 PM Rate Topic: -----

#1 RayneShock   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 22-October 08

fileScan

Posted 26 August 2009 - 05:58 PM

I am trying to create a program that excepts a .txt file input via the scanner. Once the file is read, I need it to scan each line for how many items are on that line. After going through each line, I need a counter to count up. Eventually I will want each item (seperated by commas) to be assigned to individual variables, so the purpose of the counter is that if one line does not have 9 items on it, it skips that line.

Here is my code so far, apparently I've created an infinite loop as the program never stops spitting out numbers even though the .txt file has run out of items. Below is only what I threw together to see if I could count each individual item, before I try to code it to do individual lines.

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

 public class Tester
 {
 	public static void main(String[] args) throws IOException
   {
		
		Scanner fileScan = new Scanner (new File("playlist.txt"));
		fileScan.useDelimiter(",");
		
		int linecount = 0;
		while (fileScan.hasNext())
		{
			linecount++;
			System.out.print(linecount);
		}
	}
}



The text file I am using is as follows:

Kashmir,508,Led Zeppelin,Mothership,Rock,5,25,null,null
Viva la Vida,244,Coldplay,Viva la Vida,Alternative,5,30,null,null
Bad Data Example,65,Not,Enough,Fields
Bittersweet Symphony,358,The Verve,Urban Hymns,Rock,4,19,null,null
Hush,265,Deep Purple,Shades of Deep Purple,Rock,4,15,null,null
Yahweh,262,U2,How to Dismantle an Atomic Bomb,Rock,5,31,null,null




Any and all help is appreciated. If I haven't been clear about what I'm trying to do let me know ^^;

This post has been edited by RayneShock: 26 August 2009 - 05:59 PM


Is This A Good Question/Topic? 0
  • +

Replies To: fileScan

#2 pbl   User is offline

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

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: fileScan

Posted 26 August 2009 - 06:45 PM

Use a Scanner(file) to read your file line by line
then use a Scanner(String, ",") to extract every token from that line

Scanner fileScanner = new Scanner(new File("a.txt"))
while(fileScanner.hasNext()) {
   String line = fileScanner.nextLine();
   Scanner lineScanner = new Scanner(line);
   while(lineScanner.hastNext()) {					 // <---- my mistake hasNext() not hastNext()
	  String word = lineScanner.next();
	  ....
   }
}


This post has been edited by pbl: 30 August 2009 - 07:06 PM

Was This Post Helpful? 0
  • +
  • -

#3 RayneShock   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 22-October 08

Re: fileScan

Posted 28 August 2009 - 04:07 PM

I appreciate the fast reply pbl, but I am receiving the following error message now:

Assignment1ClientTester.java:19: cannot find symbol
symbol  : method hastNext()
location: class java.util.Scanner
			  while(lineScanner.hastNext()) 
							   ^



I figured that would pop up if I didn't import the scanner utility, but I have it at the top of my code.

This post has been edited by RayneShock: 28 August 2009 - 04:07 PM

Was This Post Helpful? 0
  • +
  • -

#4 AbuJaFaR   User is offline

  • D.I.C Regular

Reputation: 13
  • View blog
  • Posts: 330
  • Joined: 13-December 07

Re: fileScan

Posted 28 August 2009 - 04:24 PM

The method is hasNext. :)

Pbl was in a rush :P
Was This Post Helpful? 0
  • +
  • -

#5 RayneShock   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 22-October 08

Re: fileScan

Posted 28 August 2009 - 04:30 PM

View PostAbuJaFaR, on 28 Aug, 2009 - 03:24 PM, said:

The method is hasNext. :)

Pbl was in a rush :P



I apologize for my lack of understanding, but which part of the code should be marked as .hasNext()?
Was This Post Helpful? 0
  • +
  • -

#6 AbuJaFaR   User is offline

  • D.I.C Regular

Reputation: 13
  • View blog
  • Posts: 330
  • Joined: 13-December 07

Re: fileScan

Posted 28 August 2009 - 04:49 PM

The method of Scanner class is hasNext.

hastNext doesnt exist.It was a fast-typing error.

Just remove the t from hastNext !
Was This Post Helpful? 0
  • +
  • -

#7 RayneShock   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 22-October 08

Re: fileScan

Posted 28 August 2009 - 05:09 PM

Ha thanks, I can't tell you how many times I read and reread that line.
Was This Post Helpful? 0
  • +
  • -

#8 pbl   User is offline

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

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: fileScan

Posted 30 August 2009 - 07:05 PM

View PostAbuJaFaR, on 28 Aug, 2009 - 03:24 PM, said:

The method is hasNext. :)

Pbl was in a rush :P

Sorry my mistake
Won;t even go edit my post to fix it
Was This Post Helpful? 0
  • +
  • -

#9 RayneShock   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 22-October 08

Re: fileScan

Posted 30 August 2009 - 07:45 PM

No worries at all, I simply appreciate the help! But it looks like I need more help. What I asked before isn't what I was needing I think. I may have overthought it. Basically I am going to be reading a text file, each line of the file is being used to describe a song, such as in iTunes. Once read in, I'll have them assigned to another objects constructor.

What I'm needing is that if any given line doesn't have the required amount of info (i.e. 9 pieces) then that line is skipped. I'm thinking of a try/catch combo, but I can't figure out how to word the catch part. The error I'm receiving when running (without the try/catch) is

Exception in thread "main" java.util.NoSuchElementException
	at java.util.Scanner.throwFor(Scanner.java:838)
	at java.util.Scanner.next(Scanner.java:1461)
	at java.util.Scanner.nextInt(Scanner.java:2091)
	at java.util.Scanner.nextInt(Scanner.java:2050)
	at Assignment1ClientTester.main(Assignment1ClientTester.java:43)


And here is the code that I am using:

import java.util.Scanner;
import java.io.*;
//Posting Code: 2f09jm102 
//Version 0.1
/*
 *Brief description of program here
 */
 
 public class Assignment1ClientTester
 {
 	public static void main(String[] args) throws IOException
   {
		
		Scanner lineScannerner = new Scanner(new File("playlist.txt"));
		lineScannerner.useDelimiter(",");
		while(lineScannerner.hasNext()) 
		{
			  String line = lineScannerner.nextLine();
			Scanner lineScanner = new Scanner(line);
			lineScanner.useDelimiter(",");
			  while(lineScanner.hasNext()) 
			{
   				String new_name = lineScanner.next();
		   		System.out.println(new_name);
				
				int new_time = lineScanner.nextInt();
				System.out.println(new_time);
				
				String new_artist = lineScanner.next();
				System.out.println(new_artist);
				
				String new_album = lineScanner.next();
				System.out.println(new_album);
			
				String new_genre = lineScanner.next();
				System.out.println(new_genre);
				
				int new_rating = lineScanner.nextInt();
				System.out.println(new_rating);
				
				int new_play_count = lineScanner.nextInt();
				System.out.println(new_play_count);
				
				Object new_audio = lineScanner.next();
				System.out.println(new_audio);
				
				Object new_album_art = lineScanner.next();
				System.out.println(new_album_art);
			}
		}
	}
}



Let me know if I need to provide any more details : )



EDIT: NEVERMIND!!! I've figured i out : ) Java API saved me this time. thanks!

This post has been edited by RayneShock: 30 August 2009 - 08:22 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1