Reading From File Problem
I'm trying to read the inventory for a game in a text file. The inventory looks like this:
Code :
Sword 5
Shield 5 10
Cake
Sword 7
Shield 1 5
I need to read from a file and as I'm reading I need to know if the line read was a Sword, Shield or a Cake. Depending on what the item is I will do different things with it. It is important to know the numbers that follow the item. For example, shields have 2 ints, but swords only 1 int. The ints are the stats of the item.
Code :
Scanner read = new Scanner(new File("inventory.txt"));
while (read.hasNext()){
infile.findInLine("Shield");
}
Please help
Re: Reading From File Problem
A couple of ways to do it:
If you read in each line in the file as a String, you could look at the start of the String for the item name and then use that item name to control how the rest of the String was processed.
Or you could just read the next token from the file, test what kind of item it was and then have logic for item to read in the rest of the data for that item.
Re: Reading From File Problem
How do I look at the start of a String?
Re: Reading From File Problem
Use of the the String class's methods: index of or starts with for example. Read the API doc for how to use them and for other choices.
Re: Reading From File Problem
Obviously I don’t understand them, hence why I am looking for help. Thanks though.
Re: Reading From File Problem
Which method(s) are you having problems with? Can you post some sample code.
There is lots of sample code on the forum. Do a Search for the method(s) you want samples for.
Re: Reading From File Problem
Get off my post troll. I know you read above, so you know which methods I am having problems with.
Re: Reading From File Problem
I take it you want some one to write some code for you.
If you can't ask a question and explain what you're having problems you'll have a hard time getting anyone to help you.
Good luck.
Re: Reading From File Problem
Quote:
Originally Posted by
Neobs
Get off my post troll. I know you read my first post, so you know which methods I am having problems with.
That member you happen to be calling a troll is one of the top contributors to the forum, who has helped countless others and who is trying to help you, in their spare and valuable time, without pay.
@Neobs. I recommend you read the forum rules, and read the link in my signature entitled getting help, at least if you wish to receive help.
Re: Reading From File Problem
Quote:
Originally Posted by
Neobs
Get off my post troll. I know you read above, so you know which methods I am having problems with.
The only one trolling is you!!!
[-X[-X[-X[-X[-X[-X[-X[-X[-X[-X[-X[-X[-X[-X[-X
I've reported your rudeness. (I saw you put down Norm in another thread too. Norm's a nice guy. Don't pick on him.)
Also, the API is here:
String API that Norm was talking about
Re: Reading From File Problem
I would recommend using a BufferedReader to read each line and then using String.split("\\s+") to separate each section. You can then evaluate the values on each line in their own respective arrays to your liking.
Code :
BufferedReader br = new BufferedReader(new FileReader(//file here));
String line = null;
ArrayList<String> lines = new ArrayList<String>();
while((line = br.readLine()) !=null)
lines.add(line);
for(String s:line) {
String[] parts = s.split("\\s+");
//Do whatever you need to do.
}
Treat others with respect and they will respect you as well. Maybe they will even help you...
Myself and many others on this forum have been very grateful for Norm's helpfulness and desire for you to actually UNDERSTAND the solution to your problem, and I'm not about to let you treat him that poorly. I will, however, still offer my own help.