What I'm attempting to do is read a text document to pass in variables into an arraylist of shape objects that I will use to create shapes. I don't need any help with the actual drawing of the shapes, working with the arraylist, or the initial scanning setup. I thought about doing a while loop to read each word/int using the single whitespace in between as the delimiter. Where I get stuck is what exactly I should do with information I don't need. For example, I would test the line to see if it said start via an if statement. I don't need the word "picture", but I do need the character "A". It's also throwing me because I have only worked with formatted files before where each line had the same data in the same way.
I've watched a bunch of videos and looked into tutorials on parsing information in a text file, but I found they were working with a formatted file. I've worked with that before, and that makes sense to me. I feel like the right steps are to create a while loop that uses the .hasNext() to go through the file, and to use nested if statements to parse the lines. If someone wouldn't mind pointing me to a tutorial or documentation that would assist me in this, I'd be grateful. Perhaps a switch would work better to avoid a crazy amount of code, but maybe I'm thinking about this in a lame way. I inserted systemouts so I could see what things it was putting inside. I see that the following code will be an issue if it doesn't follow the same pattern. Thank you for your input!
int circX, circY, circSize;
String circName, currentInput, junk;
while (input.hasNext())
{
currentInput = input.next();
if (currentInput.equals("start"))
{
junk = input.next();
circName = input.nextLine();
junk = input.next(); //this line gives me an error, and if I remove, it errors at next command saying there isn't such an element
circX = input.nextInt();
circY = input.nextInt();
circSize = input.nextInt();
}
}
The text file has information such as the following:
start picture A
circle 0 100 20
circle 100 100 20
circle 50 150 20
end picture
draw picture A blue 10 10
dance picture A 10 10
erase
draw picture A red 100 100
erase
draw picture A pink 100 10
another example of a test file:
start picture A
circle 50 100 20
coloredcircle 0 100 20 green
end picture
draw picture A blue 10 10
dance picture A 30 30
erase
start picture B
rectangle 0 100 20 40
rectangle 100 100 40 20
rectangle 200 100 40 20
Sshape 55 55 55 55 55 yellow Elf
Sshape 55 25 35 45 65 red Ogre
end picture
draw picture B yellow 10 10
erase
draw picture A blue 10 10