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 5 of 5

Thread: Help writing some Scanner code

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help writing some Scanner code

    Hi.

    I'm writing a program that will do different sort of things to matrices (add, subtract, multiply) from a file. I'd like to ask the user which file they would like read, and then read the file for the matrices.

    How do I create a scanner object that will user input files to do that?


  2. #2
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Location
    Wales, Bangor & England, Warwickshire
    Posts
    820
    My Mood
    Cynical
    Thanks
    7
    Thanked 104 Times in 90 Posts

    Default Re: Help writing some Scanner code

    new Scanner(new File(new Scanner(System.in).nextLine()));
    chris[at]javaprogrammingforums[dot]com

    Prifysgol Bangor University, North Wales

  3. #3
    Junior Member
    Join Date
    Feb 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help writing some Scanner code

    Ok, I have that, but now I'd like to surround it by a try/catch phrase in case the user puts in a file that isn't recognized or can't be read.

    I know that I have to exit the program and return a negative value at some point, I'm just not sure where. Here's what I have:
    	public static void askForFileName() {
    		System.out.println("Please enter a file name ==> ");
    		try {
    			new Scanner(new File(new Scanner(System.in).nextLine()));
    		} catch (FileNotFoundException e) {
     
    			e.printStackTrace();
    		}
     
    	}
    Also, if I create a method that will read the file entered by the user here, should I give it a variable in this method and call it in the read method, or what would be the best way to go about that?
    Last edited by helloworld922; February 2nd, 2010 at 03:07 PM.

  4. #4
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Location
    Wales, Bangor & England, Warwickshire
    Posts
    820
    My Mood
    Cynical
    Thanks
    7
    Thanked 104 Times in 90 Posts

    Default Re: Help writing some Scanner code

    You are asking questions which contradict each other. You first asked how to open a file to be read based on user input. The you place it in a method called askForFileName() so all it should do is ask for a file name, not try to open it! You need to re-think how you are coding this, remember java is OOP and if this is an assignment, the previous line of code I gave you will give you next to no marks due to its poor presentation and readibility.

    Regards,
    Chris
    chris[at]javaprogrammingforums[dot]com

    Prifysgol Bangor University, North Wales

  5. #5
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 286 Times in 225 Posts
    Blog Entries
    4

    Default Re: Help writing some Scanner code

    Thread moved to: File I/O & Other I/O Streams - Java Programming Forums
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

Similar Threads

  1. Writing java applications for the iPod Touch
    By JavaPF in forum Java ME (Mobile Edition)
    Replies: 8
    Last Post: June 14th, 2011, 04:34 PM
  2. Reading and Writing Text Files
    By kappasig84 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: March 1st, 2010, 06:16 PM
  3. Writing Output To New File
    By Scottj996 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: January 6th, 2010, 06:25 PM
  4. Writing clone() method for LinkedList
    By vluong in forum Collections and Generics
    Replies: 6
    Last Post: October 27th, 2009, 08:41 AM
  5. I need help writing this program
    By kev2000 in forum Algorithms & Recursion
    Replies: 5
    Last Post: June 4th, 2009, 03:14 AM