The best way to scan in timein mm:ss format
Page 1 of 1
14 Replies - 949 Views - Last Post: 19 March 2009 - 02:51 PM
#1
The best way to scan in time
Posted 18 March 2009 - 12:14 PM
Replies To: The best way to scan in time
#2
Re: The best way to scan in time
Posted 18 March 2009 - 12:40 PM
#3
Re: The best way to scan in time
Posted 18 March 2009 - 09:13 PM
And uhh, if that's the case, how do I tell my program to look for that instead of a space?
Thanks
#4
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(":");
#5
Re: The best way to scan in time
Posted 18 March 2009 - 09:58 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() )
{
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?
#6
Re: The best way to scan in time
Posted 18 March 2009 - 10:00 PM
#7
Re: The best way to scan in time
Posted 18 March 2009 - 10:04 PM
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
#8
Re: The best way to scan in time
Posted 18 March 2009 - 10:06 PM
#9
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();
}
}
}
#10
Re: The best way to scan in time
Posted 18 March 2009 - 10:24 PM
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.
#11
Re: The best way to scan in time
Posted 18 March 2009 - 11:09 PM
#12
Re: The best way to scan in time
Posted 18 March 2009 - 11:21 PM
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
#13
Re: The best way to scan in time
Posted 18 March 2009 - 11:39 PM
Quote
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.
#14
Re: The best way to scan in time
Posted 18 March 2009 - 11:48 PM
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
#15
Re: The best way to scan in time
Posted 19 March 2009 - 02:51 PM
|
|

New Topic/Question
Reply




MultiQuote




|