14 Replies - 1608 Views - Last Post: 19 March 2009 - 02:51 PM Rate Topic: -----

#1 webmin  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 256
  • Joined: 21-May 08

The best way to scan in time

Posted 18 March 2009 - 12:14 PM

Well, I just started learning about file input in Java, and this one has me a bit confused. Part of our problem discuss how this guy has his inventory of cd's stored in a text file, with things like CD ID, CD Title, Number of Tracks, and CD Time (the amount of time playing all the songs will take). The last one is what I'm interested in because in the file, it's supposed to formatted as such mm:ss. I know I could print out the numbers in that format simply by %d:%d, but can you scan it in like that, or do I need to scan that in as a string and then tokenize it using the : as the deliminator. Which way would you suggest. Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: The best way to scan in time

#2 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: The best way to scan in time

Posted 18 March 2009 - 12:40 PM

Yeah, as soon as you have all of the other data, you can switch the delimiter to :. Then read the minutes and seconds.
Was This Post Helpful? 0
  • +
  • -

#3 webmin  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 256
  • Joined: 21-May 08

Re: The best way to scan in time

Posted 18 March 2009 - 09:13 PM

So you're suggesting that I scan everything else in first using the space as the delimiter, and the after everything else is in switch the delimiter to a :?

And uhh, if that's the case, how do I tell my program to look for that instead of a space?

Thanks
Was This Post Helpful? 0
  • +
  • -

#4 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: The best way to scan in time

Posted 18 March 2009 - 09:18 PM

Scanner scan = new Scanner(System.in);

// after you read everything else in.

scan.useDelimiter(":");

Was This Post Helpful? 1
  • +
  • -

#5 webmin  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 256
  • Joined: 21-May 08

Re: The best way to scan in time

Posted 18 March 2009 - 09:58 PM

Ok, this is what I have so far:

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

public class CDTracker
{
	public static void main( String[] args )
	{
		int cdID, min, sec, totMin = 0, totSec = 0, songCount = 0;
		String cdTitle, outStr = "This is the info scanned in:\n";
		String formatStr = "%d %s %d:%d";
		
		Scanner importIt = null;
		
		File inputFile = new File( "cd.txt" );
		
		try
		{			
			importIt = new Scanner( inputFile );
			
			while( importIt.hasNext() )
			{
				cdID    = importIt.nextInt();
				cdTitle = importIt.next();
				scan.useDelimiter( ":" );
				min     = importIt.nextInt();
				sec     = importIt.nextInt();
				
				outStr += String.format( formatStr, cdID, cdTitle, min, sec );
			}
			System.out.println( outStr );
		}
		catch( FileNotFoundException e )
		{
			System.err.println( e.getMessage() );
			System.exit( 1 );
		}
		finally
		{
			importIt.close();
		}
		 
	}
}



But your suggestion seems to be throwing an error, can you help me with 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: The best way to scan in time

Posted 18 March 2009 - 10:00 PM

Another suggestion, ive never used it as a Scanner type. but i have assigned the time as a string. Then using the replace method to replace ":" to a space. and just used nextInt() method from the scanner class. Hope this helps!
Was This Post Helpful? 0
  • +
  • -

#7 webmin  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 256
  • Joined: 21-May 08

Re: The best way to scan in time

Posted 18 March 2009 - 10:04 PM

Never mind I answered my own question. Pay no attention to the last post. THanks

Ok got the last bit to work now that I figured out that I needed to use my own variable but now I get a new set of errors at runtime:

--------------------Configuration: <Default>--------------------
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:840)
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 CDTracker.main(CDTracker.java:25)

Process completed.


Not sure what would cause these, can anyone point me in the right direction?

Oh yeah this is what's in cd.txt:

4855 SongsofTommorrow 5:23
3266 AllIEverWanted 4:12

This post has been edited by webmin: 18 March 2009 - 10:07 PM

Was This Post Helpful? 0
  • +
  • -

#8 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

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

Re: The best way to scan in time

Posted 18 March 2009 - 10:06 PM

Could you post the new code please?
Was This Post Helpful? 0
  • +
  • -

#9 webmin  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 256
  • Joined: 21-May 08

Re: The best way to scan in time

Posted 18 March 2009 - 10:17 PM

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

public class CDTracker
{
	public static void main( String[] args )
	{
		int cdID, min, sec, totMin = 0, totSec = 0, songCount = 0;
		String cdTitle, outStr = "This is the info scanned in:\n";
		String formatStr = "%d %s %d:%d";
		
		Scanner importIt = null;
		
		File inputFile = new File( "cd.txt" );
		
		try
		{			
			importIt = new Scanner( inputFile );
			
			while( importIt.hasNext() )
			{
				importIt.useDelimiter( " " );
				cdID    = importIt.nextInt();
				cdTitle = importIt.next();
				importIt.useDelimiter( ":" );
				min     = importIt.nextInt();
				totMin += min;
				sec     = importIt.nextInt();
				totSec += sec;
				
				outStr += String.format( formatStr, cdID, cdTitle, min, sec );			
			}
			System.out.println( outStr );
		}
		catch( FileNotFoundException e )
		{
			System.err.println( e.getMessage() );
			System.exit( 1 );
		}
		finally
		{
			importIt.close();
		}
		 
	}
}


Was This Post Helpful? 0
  • +
  • -

#10 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

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

Re: The best way to scan in time

Posted 18 March 2009 - 10:24 PM

If it is printed like this
Id
Title
Time
Then i htink your problem lies here
cdTitle = importIt.next();


When you call the scanner with the dilimeter it is still on that line, trying clearing the buffer after that piece of code. I think that should fix it.
Was This Post Helpful? 0
  • +
  • -

#11 webmin  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 256
  • Joined: 21-May 08

Re: The best way to scan in time

Posted 18 March 2009 - 11:09 PM

Two things in my cd.txt each record exists on one line, the other thing is what would I use to clear the scanner buffer. I'm not exactly sure I understand how that would help either. Can you explain this for me?
Was This Post Helpful? 0
  • +
  • -

#12 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

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

Re: The best way to scan in time

Posted 18 March 2009 - 11:21 PM

You would just use a importIt.nextLine()
Ok, so say i were to print a number and get it with importIt.nextInt();

1234*
CD TItle
*It will get the number and end after 4. Doesn't go to the next line so when you call the next scanner method, still thinks your on that line


You have to clear the buffer, the line, when everyting is printed on its own line like that. Only do that if you have numbers or if you use importIt.next()
.next() goes until it finds a white space, so if there is only one word on the line, goes to the end of that word and stays there. For that, i would suggest using nextLine() for the Cd Title, incase its a 2 word title, and by using nextLine() to get it, you wont have to clear the buffer.

This post has been edited by Fuzzyness: 18 March 2009 - 11:37 PM

Was This Post Helpful? 0
  • +
  • -

#13 webmin  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 256
  • Joined: 21-May 08

Re: The best way to scan in time

Posted 18 March 2009 - 11:39 PM

See if you can help me interpret this. This is the problem instructions word for word from my book:

Quote

A music buff has a CD Collection. He has a data file called cd.txt where each record contains the CD ID, the CD Title, and the minutes and seconds (format mm:ss) for each track on a given CD. That number of tracks on a CD varies. Write a program called CDTracker.java that outputs to a file the CD ID and title with the total time for tracks on that CD (format mm:ss). The output file should be sorted by CD ID. (Hint: The Scanner class has a method called hasNext that returns true if the scanner has another token in its input.)


If that's true then that means that my cd.txt file should look something like this:

CD ID CD Title mm:ss mm:ss mm:ss mm:ss mm:ss mm:ss mm:ss
and so on varying the amount of mm:ss for each record.
Was This Post Helpful? 0
  • +
  • -

#14 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

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

Re: The best way to scan in time

Posted 18 March 2009 - 11:48 PM

Dang I thought my teachers directions were crappy. Well, you need to ask your teacher what format that the .txt will be in. All on a seprate line, if it is id, title, mm:ss or the id, title, mm:ss for every track in a row. Your teacher needs to be more descriptive in his directions. You can only read stuff from a file if you know exactly the format it is saved. Get with him and ask the format and PM me, I can help you with it later today (its nearly 2 am). Goodluck and get back to me!
:)


P.S. - I think the data is saved as
ID Title mm:ss
on everyline, so i would say go with that and then if not you can change it easily.

This post has been edited by Fuzzyness: 18 March 2009 - 11:50 PM

Was This Post Helpful? 0
  • +
  • -

#15 webmin  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 256
  • Joined: 21-May 08

Re: The best way to scan in time

Posted 19 March 2009 - 02:51 PM

I've attached cd.txt and cdoutput.txt so that you can see how both should look.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1