Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3

Thread: Files...

  1. #1
    New to the CF scene
    Join Date
    Feb 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Angry Files...

    Hello, I need to find a way to whether a person's name exists in a file...and if it exists graph the data in the file, and if it doesn't exist simply state that it doesn't exist. this is what I have so far...but i can't seem to find a way for the program to read the user input in the file...

    public class Names{

    public static void main (String[] args) throws FileNotFoundException {

    Scanner console = new Scanner(System.in);
    Scanner input = new Scanner(new File ("names.txt"));
    System.out.print("name? ");
    String name = console.next();
    }
    }

  • #2
    Codeasaurus Rex
    Join Date
    Jun 2008
    Location
    Redmond, WA
    Posts
    660
    Thanks
    31
    Thanked 100 Times in 94 Posts
    You read from the file in a very similar way that you read from the console.

    Code:
    while( input.hasNext() ){
         System.out.println( "Line: " + input.nextLine() );
    }
    You can read all the methods here: http://download.oracle.com/javase/1....l/Scanner.html
    Unless otherwise stated, any code posted is most likely untested and may contain syntax errors.
    My posts, comments, code, and suggestions reflect only my personal views.
    Web Portfolio and Code Snippets: http://shanechism.com

  • #3
    New to the CF scene
    Join Date
    Feb 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Hmm

    What if I simply wanna state whether that name exists in the file or not by saying "name exists" and "name doesn't exist".


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •