Results 1 to 9 of 9
  1. #1
    csanch11 is offline Member
    Join Date
    Oct 2013
    Posts
    46
    Rep Power
    0

    Default Reading the name of the file from the keyboard

    THE TASK IS TO....

    Write a program that extracts words from a file. For the purposes of this program, a word is defined as a series of adjacent letters. Only print words that are at least four and no more than 12 letters long. Print each word on a different line.

    I KNOW HOW TO DO THAT PART BUT THIS NEXT PART IS KIND OF CONFUSING....

    The program should read the name of the file from the keyboard.
    ************************************************** ************************************************** **************
    I need to get the filename from the user for this particular program. Usually I would have the name of the file prewritten into the source code like this....
    Scanner input = new Scanner(new File("gettsy_burg.txt");

    I tried different ways but I just can't seem to figure it out....

  2. #2
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,097
    Rep Power
    6

    Default Re: Reading the name of the file from the keyboard

    Why wouldn't you be able to figure it out when you have the entire internet of examples available to you?

    Come on man. I learned this stuff when there was no (affordable / publicly accessible) internet and I did it without problems. You have it about a hundred times easier than me.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    csanch11 is offline Member
    Join Date
    Oct 2013
    Posts
    46
    Rep Power
    0

    Default Re: Reading the name of the file from the keyboard

    I already know how to do that... I guess what I'm really asking is how to rearrange Scanner input = new Scanner(new File("gettsy_burg.txt"); since I want the user to input the filename instead of the filename being prewritten by the programmer.

    This is what I have...

    Scanner keys = new Scanner(System.in);
    System.out.println("Enter the filename: ");
    String fileName = keys.nextLine();
    keys = new Scanner(new File(fileName));

  4. #4
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,097
    Rep Power
    6

    Default Re: Reading the name of the file from the keyboard

    Good. So what's wrong with that code? Seems fine to me.

    Note that you don't have to reuse that 'keys' variable, you can also just use two different variables to hold your two different scanners.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    csanch11 is offline Member
    Join Date
    Oct 2013
    Posts
    46
    Rep Power
    0

    Default Re: Reading the name of the file from the keyboard

    If I get rid of the second 'keys' and replace it with another variable what should be its data type?? I tried replacing the second 'keys' with another variable initialized by the data type String, but it says that "Scanner cannot be converted into a String."

  6. #6
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,097
    Rep Power
    6

    Default Re: Reading the name of the file from the keyboard

    ... I really have no idea what I should say now, your questions and the code you posted earlier are in complete conflict with each other. The code you posted indicates that you know this stuff (variables, data types), but your last question really says that you do not. Did you copy/paste all that without trying to understand it first?

    You also didn't answer my question: what is wrong with what you have?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    csanch11 is offline Member
    Join Date
    Oct 2013
    Posts
    46
    Rep Power
    0

    Default Re: Reading the name of the file from the keyboard

    Scanner keys = new Scanner(System.in);
    System.out.println("Enter the filename: ");
    String fileName = keys.nextLine();
    keys = new Scanner(new File(fileName));


    while (fileName.hasNext()) {
    String oneLine = fileName.nextLine(); //reads the line in the file
    System.out.println(oneLine);
    int length = oneLine.length();
    if (length >= 4 && length <= 12) {
    System.out.println(oneLine);
    }
    }


    ******
    the .hasNext inside the boolean expression is underlined in red, it says "Cannot find symbol"
    Last edited by csanch11; 02-20-2014 at 09:54 AM.

  8. #8
    csanch11 is offline Member
    Join Date
    Oct 2013
    Posts
    46
    Rep Power
    0

    Default Re: Reading the name of the file from the keyboard

    Should I change fileName into keys instead? Which makes sense since fileName is only a String and is just the name of the file not the actual content of the document.

    while (keys.hasNext()) {
    String oneLine = keys.nextLine();
    Last edited by csanch11; 02-20-2014 at 10:06 AM.

  9. #9
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,097
    Rep Power
    6

    Default Re: Reading the name of the file from the keyboard

    I think in stead of continuously bombarding this thread with one question after another, you should rather sit back and THINK about this for a moment. Don't just try stuff and hope for the best, think about it, understand it, then do it.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Replies: 0
    Last Post: 12-15-2012, 01:37 AM
  2. reading data from keyboard
    By ashwin kumar in forum New To Java
    Replies: 5
    Last Post: 06-10-2011, 01:51 PM
  3. Replies: 9
    Last Post: 10-20-2009, 10:52 AM
  4. Replies: 3
    Last Post: 05-09-2009, 11:31 PM
  5. Reading Keyboard
    By ushigley in forum New To Java
    Replies: 4
    Last Post: 08-20-2008, 01:50 PM

Posting Permissions

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