Results 1 to 3 of 3
  1. #1
    SuitMyPants is offline Member
    Join Date
    Sep 2010
    Posts
    3
    Rep Power
    0

    Default Reading text files

    I'm suppose to read a file from input.txt and draw a picture of it based on its conents

    Input.text
    COLOR blue
    LINE 200 201 140 106
    COLOR magenta
    RECT 55 340 444 122
    COLOR gray
    OVAL 200 333 30 30
    COLOR pink
    STR hello 133 133
    I have this written so far.
    import java.io.*;
    import java.util.Scanner;
    import java.util.regex.MatchResult;

    public class ShapeMaker {

    public static void main(String args[]) {
    try {
    //set up a stream reader to read in the input file
    BufferedReader inputFile = new BufferedReader(new FileReader("C:\\Documents and Settings\\Administrator\\workspace\\Project1\\src\ \input.txt"));
    String line = null;

    //you may write code here

    //while there are commands to process from the input file
    while((line=inputFile.readLine())!=null){
    //figure out the type of shape
    Scanner s = new Scanner(line);
    String pattern = "(\\w*\\s)+(\\w*)"; //change this to a pattern
    s.findInLine(pattern);
    MatchResult result = s.match();

    for (int i=1; i<10; i=i-1)
    System.out.println(result.group(2));


    //System.out.println(result.group());

    //you may write code here

    s.close();
    }

    //you may write code here

    inputFile.close();
    }catch (IOException e) {
    System.err.println("Error reading file");
    }
    }
    }
    I changed the part where it says //change to pattern so when I print result group 2, I get from input text:"blue, magenta, red, etc." I was hoping to capture those into variables and then make a for statement that would set the shape colors... but I'm unable to do so.

    When I run the code, it repeats blue into an infinite loop. Is there a way to stop that and just get the word "blue" when I run it? I was hoping to do multiple for loops to store each color and then making a for statements to set colors.

  2. #2
    ceautery is offline Member
    Join Date
    Sep 2010
    Location
    Columbus, Ohio
    Posts
    4
    Rep Power
    0

    Default

    This may have been better off in the New To Java section, but since you're here...


    Java Code:
    for (int i=1; i<10; i=i-1) 
    System.out.println(result.group(2));
    You're subtracting from i, so it will continue to be less than 10. Easy typo, but you don't need to iterate through a loop here, you're already in one. Just take out the "for" line.

    Next, your regular expression is good, but it could be tweaked a little. You want one-or-more (+) word characters, not 0-or-more (*). And don't forget you'll have to account for possible spaces at the end of each match item.

    Good luck.
    Curtis

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Reading text files
    By SuitMyPants in forum New To Java
    Replies: 2
    Last Post: 09-27-2010, 03:39 PM
  2. Reading in Text Files
    By blueduiker in forum New To Java
    Replies: 4
    Last Post: 05-17-2010, 09:22 AM
  3. Reading in Text Files
    By blueduiker in forum New To Java
    Replies: 9
    Last Post: 05-17-2010, 06:45 AM
  4. Reading In Text Files
    By Dukey in forum New To Java
    Replies: 4
    Last Post: 04-05-2009, 12:53 AM
  5. Reading and Writing Text Files
    By kandt in forum New To Java
    Replies: 1
    Last Post: 11-12-2008, 04:15 AM

Posting Permissions

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