- Joined
- Sep 1, 2007
- Sythe Gold
- 690
- Messages
- 6,681
Dunno how to do this shit.
http://pastebin.com/F8u6ybGA
http://pastebin.com/F8u6ybGA
Code:
I have an assignment where I need to write a while loop code block that contains
a try*catch statement. The try block retrieves each line from an input file, and invokes
an isValid method I created to check if the format is correct, passing to it the line from the
file. If there are no more lines to parse, runProgram is set to false, the while loop terminates.
The catch block will catch an exception that I made. So far I have
public static void main(String[] args)
{
File file;
Scanner inputFile;
String fileName;
Scanner scan = new Scanner(file);
fileName = scan.nextLine();
boolean runProgram = true;
while(runProgram)
{
try
{
// for loop to check each line of my file
// invoke isValid
// Check if it's the last line in the file, and end program if so
}
catch(BankAccountException e)
{
System.out.println("Account Exception. Do you wish to quit? y/n");
String quit = scan.nextLine();
if(quit.equals("y"))
runProgram = false;
else
runProgram = true;
}
}
}
I just have no idea how to open a file, check the next line, use my isValid method (which
is just a StringTokenizer
that checks for
the correct format), and closes when it reaches the end of the file.
Here is my isValid method:
private static boolean isValid(String accountLine) throws BankAccountException
{
StringTokenizer strTok = new StringTokenizer(accountLine, ";");
boolean valid = true;
if(strTok.countTokens() == 2)
{
if(strTok.nextToken().length() == 10)
{
if(!strTok.nextToken().matches(".*[0-9].*"))
{
valid = true;
}
}
}
else
valid = false;
return valid;
}
I also have a question with the above method. If I call .nextToken() twice, am I right in
expecting the first iteration to deal with the first token, and the second to deal with the
second? Or will they both just check the first token?


