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.
    Last edited by SuitMyPants; 09-27-2010 at 05:49 AM.

  2. #2
    SuitMyPants is offline Member
    Join Date
    Sep 2010
    Posts
    3
    Rep Power
    0

    Default

    My teacher tells me this:

    # You may only use the Scanner and MatchResult classes from the util and regex packages, respectively
    # You may NOT use exceptions in this project (i.e. you may not throw or catch them).

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default

    Please edit your code and add code tags to make it more readable.
    Info here: Java Forums - BB Code List

    Check the logic in this for:
    for (int i=1; i<10; i=i-1)
    When will the condition be false?

Similar Threads

  1. Reading in Text Files
    By blueduiker in forum New To Java
    Replies: 4
    Last Post: 05-17-2010, 08:22 AM
  2. Reading in Text Files
    By blueduiker in forum New To Java
    Replies: 9
    Last Post: 05-17-2010, 05:45 AM
  3. Reg: Reading Files from Text file
    By balaji csc in forum New To Java
    Replies: 1
    Last Post: 11-09-2009, 11:06 AM
  4. Reading In Text Files
    By Dukey in forum New To Java
    Replies: 4
    Last Post: 04-04-2009, 11:53 PM
  5. Reading and Writing Text Files
    By kandt in forum New To Java
    Replies: 1
    Last Post: 11-12-2008, 03: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
  •