Java help with fileIO and exception handling

Discussion in 'Programming General' started by MohtasaUnique, May 19, 2013.

Java help with fileIO and exception handling
  1. Unread #1 - May 19, 2013 at 5:59 PM
  2. MohtasaUnique
    Joined:
    Sep 1, 2007
    Posts:
    6,681
    Referrals:
    2
    Sythe Gold:
    690
    Discord Unique ID:
    158831078964985856
    Discord Username:
    Tony#2235

    MohtasaUnique Grand Master
    Retired Global Moderator

    Java help with fileIO and exception handling

    Dunno how to do this shit.
    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?
     
  3. Unread #2 - May 21, 2013 at 4:51 PM
  4. MohtasaUnique
    Joined:
    Sep 1, 2007
    Posts:
    6,681
    Referrals:
    2
    Sythe Gold:
    690
    Discord Unique ID:
    158831078964985856
    Discord Username:
    Tony#2235

    MohtasaUnique Grand Master
    Retired Global Moderator

    Java help with fileIO and exception handling

    Figured it out
     
  5. Unread #3 - May 22, 2013 at 4:02 AM
  6. flaxpro91
    Joined:
    Apr 6, 2013
    Posts:
    20
    Referrals:
    0
    Sythe Gold:
    0

    flaxpro91 Newcomer

    Java help with fileIO and exception handling

    BufferedReader/Writer :p But there are always several ways of accomplishing the same task. Also your instance vars are on the inside of method and is good programing to declare those as null.
     
< What programming language to learn? | List of most commonly used Nouns in English vocab >

Users viewing this thread
1 guest


 
 
Adblock breaks this site