Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Scanner Class

  1. #1
    Junior Member
    Join Date
    Oct 2017
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Scanner Class

    Need help writing a method that repeatedly prompts the user for the name of an input file until the user enters
    the name of an existing file; then creates and returns a Scanner for the input file. Also how to create a try/catch block that handles FileNotFoundException.

    //Repeatedly prompts the user for the name of an input file until the user enters
    //the name of an existing file; then creates and returns a Scanner for the input file.
    //See the lecture notes for your section and/or the textbook for examples.
    //Use a try/catch block to catch and handle any FileNotFoundException's that occur
    public static Scanner getInputScanner(Scanner console){
    Scanner result = null;
    while (result == null) {
    System.out.print("input file name? ");
    String name = console.next();
    try {
    result = new Scanner(new File(name));
    }
    catch (FileNotFoundException e) {

    }
    }
    return result;
    }

    This is my code thus far.

  2. #2
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    268
    My Mood
    Amused
    Thanks
    8
    Thanked 18 Times in 18 Posts

    Default Re: Scanner Class

    Where is main method ?
    Whatever you are, be a good one

Similar Threads

  1. What exactly is the purpose of a Scanner class?
    By ekrocks in forum Java Theory & Questions
    Replies: 4
    Last Post: April 7th, 2014, 10:33 PM
  2. Using the Scanner class
    By Brocco767 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 26th, 2012, 07:47 PM
  3. use Delimiter, Scanner Class
    By izzahmed in forum What's Wrong With My Code?
    Replies: 12
    Last Post: October 25th, 2011, 05:27 PM
  4. [SOLVED] Problem in Coin-counter with scanner class
    By coccoster in forum File I/O & Other I/O Streams
    Replies: 6
    Last Post: March 25th, 2009, 08:46 AM
  5. Replies: 1
    Last Post: May 13th, 2008, 08:08 AM