This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Java in General and the fly likes Reading Seperate Int and String From a file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Reading Seperate Int and String From a file" Watch "Reading Seperate Int and String From a file" New topic
Author

Reading Seperate Int and String From a file

Judane James
Greenhorn

Joined: Oct 08, 2012
Posts: 11
I am trying to do the Hunt the Wumpus program in text-based. Reading from a text file and generating the console to the user. However it seems I am having trouble with the declaration as well as the output, here are my files and the errors I am getting.
My errors seems to be generating from the for loops, when I remove it I get no error (this also includes when I remove the last 4 statements that place the results back into the caveLayout declaration. But without the for loop, how to I read the remainder of the Int() withing the file?






Error:
Welcome to **Hunt The Wumpus!**
To move to an adjacent cave, enter 'M' and the cave number.
To shoot an arrow into a cave, enter 'S' and the cave number.
Exception in thread "main" java.lang.IllegalStateException: Scanner closed
at java.util.Scanner.ensureOpen(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at gamePlay.newGame(gamePlay.java:26)
at wurmGame.main(wurmGame.java:25)
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 3974
    
  17

Exception in thread "main" java.lang.IllegalStateException: Scanner closed

In line 23 of the second code block you close the Scanner, which is used to read from the file. This is at the end of the for loop. At the start of the for loop you try to read from the Scanner. So the first time through the loop you read the value, do the work, then close the Scanner. Then you start the second loop and the first thing you try to do is read from the Scanner and the above error gets generated. How do you think you can fix this?


Steve
Judane James
Greenhorn

Joined: Oct 08, 2012
Posts: 11
Initially I did not have the close() but rather no loop. then I added the loop and the errors started. I am unsure how to progress.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

If you're reading from a Scanner multiple times in a loop, then you have to close() the Scanner after the loop has completed. Just like if you're only reading a single thing--you don't close it until after you're done using it.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 3974
    
  17

Think through the steps that are causing. You close the Scanner at the bottom of the loop, then try to you try to use it again at the top of the next loop. How do you fix that? Think about 'unwrapping' the loop. Would you expect this to work?

How would you fix that code?
Judane James
Greenhorn

Joined: Oct 08, 2012
Posts: 11
I am completely stuck, initially I had no close. then I added the loop then it all started to error.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36599
    
  16
I was worried by your cave layout class. It has a visited field. Surely that visited field doesn’t apply to the layout. Surely it would apply to an individual cave. So why haven’t you got a cave class?
Judane James
Greenhorn

Joined: Oct 08, 2012
Posts: 11
the cave class is the caveLayout. I renamed it for my sake. I got it to work using this method: I been trying to test to see if it works but a println doesn't give me the results I want without switching it to an array.
Judane James
Greenhorn

Joined: Oct 08, 2012
Posts: 11
I have figured out the root of my problem. here is an example. If I am reading a text file with a set of numbers in every other line and a string in every other line. But I want to only pick up the Int only. I tested this but it only prints out the first set, never the rest of int.
What is the best way to keep printing out the next series of ints?

The text file:
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Your loop

says, "keep going as long as the next token is an int, then stop."

But that's not what you want to do. That means you'll stop as soon as you've read the first line of ints, because the next token is not an int. What you want is "keep going as long as there is more data," and then in the processing of each line, either handle it as ints or as text as appropriate. You really shouldn't be checking hasNextInt() at all, since you know exactly where you require ints, and if they're not there, it's an error and you should throw an exception.

Judane James
Greenhorn

Joined: Oct 08, 2012
Posts: 11
Thank you...at this point I cannot progress =)
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Judane James wrote:Thank you...at this point I cannot progress =)


Why not?
Judane James
Greenhorn

Joined: Oct 08, 2012
Posts: 11
I got it to read the integers but then what next, I tried storing them into an array but that didn't work. the idea is to create a layout of sorts (which I attempted to use the Map class).
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Are you still trying to get help? If so, you'll have to provide more details, such as an SSCCE(⇐click) that shows what you tried (without a bunch of fluff unrelated to the specific problem you're having) and a more precise description of the problem than "it didn't work."
Judane James
Greenhorn

Joined: Oct 08, 2012
Posts: 11
How you you use a comparison in MapHash. I am trying to write an if statement that says if the user enters a number, that has a value located in the current Selected key then output is true.

I am currently using -- protected static MultiMap caves = new MultiHashMap(); Which contains a <Key, Value> but every key has multiple values.

[code]
if(action.toLowerCase().charAt(0) == 'm' && gameMap.caves.get(Integer.toString(player.getCurrentCave()).contentEquals(target)){
player.setCurrentCave(target);
System.out.println("You are in room "+player.viewMap());
[/code]

That current if statement is wrong.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Judane James wrote:How you you use a comparison in MapHash.


I assume you mean HashMap, but I don't understand your question.

You can see if a map contains a particular key by using the containsKey() method, and you can see what the value associated with that key is by using the get() method.

I am trying to write an if statement that says if the user enters a number, that has a value located in the current Selected key then output is true.

Again, not sure exactly what you mean here, but it sounds like it might be something like this:


I leave it as an exercise for the reader to figure out the difference between the two.
Judane James
Greenhorn

Joined: Oct 08, 2012
Posts: 11
Within the http://commons.apache.org/collections/apidocs/index.html there is a public boolean containsValue(java.lang.Object key, java.lang.Object value) but when I call for it it does not display that particular one. It calls only containsValue(java.lang.Object value).
I am using the org.apache.commons.collections.MultiHashMap

My declaration is MultiMap caves = new MultiHashMap();
I have stored multiple values in each key.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Judane James wrote:Within the http://commons.apache.org/collections/apidocs/index.html there is a public boolean containsValue(java.lang.Object key, java.lang.Object value) but when I call for it it does not display that particular one. It calls only containsValue(java.lang.Object value).


Not sure what you're saying here.
Judane James
Greenhorn

Joined: Oct 08, 2012
Posts: 11
I don't know how much more to explain? I gave you the link and the method. And explained that the particular method with key, value isnt listed, only the key
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 3974
    
  17

[quote=Judane James]I don't know how much more to explain? I gave you the link and the method. And explained that the particular method with key, value isnt listed, only[/quote]
First, you posted a link to the overall API, not to the class or method you care about.

Second, look at the API for MultiMap.

Edit: Hmm, not sure why quotes arent working...
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Judane James wrote:the particular method with key, value isnt listed, only the key


This is the part I don't understand.

Where is that not listed? It's obviously listed somewhere, since you know of its existence. Are you saying that when you try to use it in code, the compiler gives an error? In this code?




That current if statement is wrong.


If this is what you're talking about then

1) I don't know what you mean by "the if statement is wrong".

2) I'm not going to try to decipher that long, complex if line. Break it into pieces. Use varaibles.

Finally, if the docs say the method exists, then it does. So either you're not using the type you think you are, or you have a different version of that library than the docs that you're looking at.

Judane James
Greenhorn

Joined: Oct 08, 2012
Posts: 11
I am using commons-collections-3.2.1 and the api says public boolean containsValue(java.lang.Object key,
java.lang.Object value) Deprecated. Checks whether the collection at the specified key contains the value.

Parameters:value - the value to search for Returns:true if the map contains the valueSince:Commons Collections 3.1

It also says its not due to be removed till 4.0

Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 3974
    
  17

Look again. What class / interface has that method? What method does MultiMap have?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

I'm having a really hard time getting a clear picture of your problem. There are fractured, vague snippets spread across this thread, but I'm not going to try to put them together.

In one post, please provide:

1) An SSCCE that shows what you're trying to do.

2) A brief but clear explanation of any relevant info about your goal or requirement that's not manifestly evident from the code.

3) A clear, precise description of whatever problems you're having doing that. Note that, "The if statement is wrong," is neither clear nor precise.

Thanks!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reading Seperate Int and String From a file
 
Similar Threads
simple game - design choice - classes - accessing variables
Help with creating a game.
Help with my game.
PLEASE HELP
How can I add elements in Hashmap?