Results 1 to 6 of 6
  1. #1
    Kristoba is offline Member
    Join Date
    Mar 2010
    Posts
    3
    Rep Power
    0

    Default Help reading a text file.

    Hi, I'm new here and I'm having some trouble with a java program.

    I have to read a text file (The user provides the name). The program must follow these rules as well.

    The first rule is that a word consists of what the next() method of the Scanner class will return. The next() method returns a String that consists of all the characters in the input up to, but not including, the next “whitespace character”, where blank spaces, tabs, and newline characters are the whitespace characters.

    The second rule: If the word has a single- or double-quotation mark as the first character of the word, remove it.

    The third rule: Look at the last character of the word. If this last character is any one of •single-quotation mark, double-quotation mark, semi-colon, colon, period, comma, hyphen, exclamation point, question mark, remove it.


    So far i have this

    public class Trimming
    {
    public static void man(String [] args) throws IOException
    {
    File inputFile = new File( "Sample.txt" );
    Scanner fileScan = new Scanner( inputFile );
    int number = fileScan.nextInt();


    Any suggestions/help?

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    What?! :confused:
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    Kristoba is offline Member
    Join Date
    Mar 2010
    Posts
    3
    Rep Power
    0

    Default

    That is exactly what i am thinking haha. So what i basically have to do is print each word to a line, excluding special punctuation.

  4. #4
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    So, start with just one line and get that sorted first:
    Java Code:
    String line = "this \'is\' a \"line\" !of! the $file$"";
    // do some magic here
    Output:
    this
    is
    a
    line
    of
    the
    file

    The first thing that comes to mind is String.split().
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  5. #5
    Kristoba is offline Member
    Join Date
    Mar 2010
    Posts
    3
    Rep Power
    0

    Default

    Okay, so how exactly would i be able to get the words in in the text file to appear in the program. This is the only problem i am having. :) I'm using Scanner of course.

    Say Name.txt is:

    hello my name is John.

    Output would appear exactly as what you showed.

  6. #6
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    Uhm, using the nextLine() method from Scanner? I thought that would be obvious from the API.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

Similar Threads

  1. Reading in a text file
    By TheRealHoff in forum AWT / Swing
    Replies: 10
    Last Post: 02-07-2010, 11:47 PM
  2. reading text file
    By trofyscarz in forum New To Java
    Replies: 1
    Last Post: 02-05-2010, 02:24 AM
  3. Reading a text file
    By diegosened in forum New To Java
    Replies: 4
    Last Post: 01-15-2010, 11:32 PM
  4. Reading two text file and sum them up
    By matt_well in forum New To Java
    Replies: 36
    Last Post: 07-22-2008, 02:55 AM
  5. Reading text file
    By Lennon-Guru in forum New To Java
    Replies: 1
    Last Post: 12-15-2007, 11:38 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
  •