This site requires JavaScript, please enable it in your browser!
Greenfoot back
Omniscience
Omniscience wrote ...

2012/4/1

Using Scanner class.

Omniscience Omniscience

2012/4/1

#
Ah, it is good to be back. This time, i've been playing around with the Scanner class. I'm having trouble "transferring" the contents of the Scanner (whose source is the keyboard of the user), into a String variable. This needs to be done, because the user needs to input their name, and I need to be able to store that name, before the actual gameplay begins. Here is where I've got to so far:
//boolean input set to false. 
//String playerName set to null. 

while (input == false) {
            System.out.println("Enter your username!");
            Scanner player = new Scanner(System.in);
            if (player.hasNext(Pattern.compile("// "))) {
                playerName = player.toString(); //to turn the contents of player into a String once a space is detected.
                System.out.println("Your user name is " + playerName + ".");                
                input = true;
                break;
                            }
        }
As usual, I'm would be really grateful for any helpful feedback! Thanks a ton. :)
Omniscience Omniscience

2012/4/2

#
The above code doesn't seem to run, and I would really like some help as to how to store a name the user types in. Thankyou!
davmac davmac

2012/4/2

#
I'm dubious about a couple of things: if (player.hasNext(Pattern.compile("// "))) { The regex pattern is "// ", which means "two slashes followed by a space". The javadoc for the hasNext(...) method says:
Returns true if the next complete token matches the specified pattern. A complete token is prefixed and postfixed by input that matches the delimiter pattern.
Given that spaces are part of the delimiter pattern, they can never appear in a token, so the 'hasNext' as you have written it must always return false. If you removed the space, it would only return true if the input was '//' which is probably not what you want either. I think you probably meant to use backslashes rather than forward slashes, but that would just give you an unnecessarily quoted space, and as I already said, a space cannot be part of the token. Secondly, on 08: playerName = player.toString(); //to turn the contents of player into a String once a space is detected. Check the documentation for Scanner.toString() - it doesn't do what you seem to think it does.
Omniscience Omniscience

2012/4/2

#
@davmac thanks for the reply! Before posting, I saw online that someone with a similar problem said to directly send the contents of the Scanner, real-time, to the StringBuffer, but I'm unsure on how you do this, even after looking at the docs. Afterwards, I'm supposed to use toString() on the String Buffer. Any programmatic help on how to do so?
davmac davmac

2012/4/2

#
I don't see any reason to use a StringBuffer. Just use Scanner.next() to get the next token from the scanner - it returns a String; that's your player name. There's no need to call hasNext() either - just read the next token immediately.
Omniscience Omniscience

2012/4/3

#
I tried to do so, still having no success. Maybe you'll see something I do not? Thanks!
//"playerName" and "button" declared globally...
//the rest is all declared within the act statement.
String userKey = Greenfoot.getKey();
        if (userKey == null) {
            System.out.println("Enter your name...");
            Scanner player = new Scanner(System.in);
            button = player.next(); 
            playerName = playerName.concat(button); 
            System.out.println("Your name is " + playerName);
        }
        if (userKey == "right") {            
            input = true;
        }
davmac davmac

2012/4/3

#
Assuming playerName is null before that code is entered, it would throw an exception due to line 08; but you didn't mention getting an exception. What is the value of playerName before that code executes?
Omniscience Omniscience

2012/4/3

#
@davmac yep, playerName was set to null. I've changed it to "". Aside from that, do you see anything else wrong in the code? It doesn't even run... am I using concat right?
davmac davmac

2012/4/3

#
When I set player = "" and run the same code, it works fine for me. What exactly do you mean by "doesn't even run"? If you mean that the control flow isn't reaching *this* code, then there's nothing you can change in *this* code to solve that. (As for concat, you're not using it incorrectly, but - I don't even understand why you're using it at all. You've added it in since your first attempt, but I don't see why. Why are you using it?).
Omniscience Omniscience

2012/4/29

#
Ah snap, forgot to reply! Well, the reason why the code wasn't running was to do with the control flow in the end. It was the rogue code button = player.next(). When I removed it, the code worked again and all the problem was solved. And I realised, fortunately, that you can just add strings together! How convenient... yeah so concat(...) was unnecessary. Thanks for all the help you given me davmac!
davmac davmac

2012/4/30

#
Ah snap, forgot to reply! Well, the reason why the code wasn't running was to do with the control flow in the end. It was the rogue code button = player.next(). When I removed it, the code worked again and all the problem was solved.
Great that you've got it solved. I don't think the fix was just to remove 'button = player.next()' though, since if you remove that you're no longer reading anything from the scanner (unless you're doing that earlier).
And I realised, fortunately, that you can just add strings together!
My point about this was that you don't need to add the strings together. If you add "" + "abc" you get "abc". You said that playerName was set to "" so why add something to it? Just reassign it.
You need to login to post a reply.