Results 1 to 3 of 3
Thread: Reading text files
- 09-27-2010, 05:46 AM #1
Member
- Join Date
- Sep 2010
- Posts
- 3
- Rep Power
- 0
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
I have this written so far.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 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.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");
}
}
}
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.
- 09-27-2010, 05:53 AM #2
Member
- Join Date
- Sep 2010
- Posts
- 3
- Rep Power
- 0
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).
- 09-27-2010, 02:39 PM #3
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
-
Reading in Text Files
By blueduiker in forum New To JavaReplies: 4Last Post: 05-17-2010, 08:22 AM -
Reading in Text Files
By blueduiker in forum New To JavaReplies: 9Last Post: 05-17-2010, 05:45 AM -
Reg: Reading Files from Text file
By balaji csc in forum New To JavaReplies: 1Last Post: 11-09-2009, 11:06 AM -
Reading In Text Files
By Dukey in forum New To JavaReplies: 4Last Post: 04-04-2009, 11:53 PM -
Reading and Writing Text Files
By kandt in forum New To JavaReplies: 1Last Post: 11-12-2008, 03:15 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks