Results 1 to 10 of 10
Thread: Scanner Ignoring input
- 04-14-2012, 07:02 PM #1
Member
- Join Date
- Mar 2012
- Posts
- 40
- Rep Power
- 0
- 04-14-2012, 09:25 PM #2
Senior Member
- Join Date
- Jan 2011
- Location
- Belgrade, Serbia
- Posts
- 227
- Rep Power
- 3
Re: Scanner Ignoring input
Can you post code you have so far so we can see what you are doing..
- 04-14-2012, 09:57 PM #3
Member
- Join Date
- Mar 2012
- Posts
- 40
- Rep Power
- 0
Re: Scanner Ignoring input
Here you go!
Java Code:import java.util.Scanner; import java.util.Random; public class Fishing { public static void main(String args[]) { Random rand = new Random(); Scanner sc = new Scanner(System.in); int number; double fishxp = 0; String stop = "fish"; System.out.println("Starting to fish!"); System.out .println("------------------------------------------------------"); try { while (stop.equals("fish")) { number = rand.nextInt(9) + 1; Thread.sleep(number * 1000); System.out.println("You caught a fish!"); System.out.println("You got 20 xp"); fishxp += 20; System.out.println("You now have " + fishxp + " xp"); System.out.println("------------------------------------------------------"); stop = sc.nextLine(); } } catch (Exception e) { System.out.println("Something wrong happened :("); } } }
- 04-14-2012, 10:39 PM #4
Senior Member
- Join Date
- Jan 2011
- Location
- Belgrade, Serbia
- Posts
- 227
- Rep Power
- 3
Re: Scanner Ignoring input
Well, this is expected behaviour, program waits for new input from user. Anithing but "fish" will stop the program. I'm not sure what is your intention, could you explain you logic in more detail..I've noticed when i write "stop = sc.nextLine();" it pauses the program until i type something
- 04-14-2012, 11:00 PM #5
Member
- Join Date
- Mar 2012
- Posts
- 40
- Rep Power
- 0
Re: Scanner Ignoring input
This is like a small part of a project that i'm working on (going to be something like runescape typing game). I have different features too, i just made this code from scratch to put it up here. The problem is that if i start to fish i can't get out of it, so i want to type "stop" whenever i feel like doing something else without the "Character" to "pause" after every fish it has caught. I want it to go on forever until i write "stop" or something in the console. I can't really explain more because this is pretty much all I want it to do.
- 04-14-2012, 11:26 PM #6
Senior Member
- Join Date
- Jan 2011
- Location
- Belgrade, Serbia
- Posts
- 227
- Rep Power
- 3
Re: Scanner Ignoring input
No, its probably my English but still I don't understand your intention. All I can see that program is running and if you type "fish" it will continue with loop but if you type anything else it will end. Sorry for not being able to help you, but I believe that someone else will understand your program logic and help you...
- 04-14-2012, 11:51 PM #7
Member
- Join Date
- Mar 2012
- Posts
- 40
- Rep Power
- 0
Re: Scanner Ignoring input
it's probably my English too. but forget that i need to type fish that is just a default value, it's meant to be that if i type anything (other than the random word that is "fish" right now(yes it's a random word i could have used "iloveprogramming", it's just a default value!)) the "character" will stop fishing. But I don't want to type the random word over and over to fish. for example, I want to fish AFK(make the player fish by itself without typing anything), so I want the program to skip that code until i written something.
Last edited by totalspelnerd; 04-14-2012 at 11:54 PM.
- 04-15-2012, 12:30 AM #8
Senior Member
- Join Date
- Jan 2011
- Location
- Belgrade, Serbia
- Posts
- 227
- Rep Power
- 3
Re: Scanner Ignoring input
Ok, if I understand correctly, you want program to do something over and over without interaction with user. At the same time, the user have option to type in console and if typed for example "stop" the program will exit??
The way I would try to achive this is by the use of Theards. In one thread put your "fishing" code and then you can easily control that thread from your "main" part of the program (based on console input)...
- 04-15-2012, 10:57 AM #9
Member
- Join Date
- Mar 2012
- Posts
- 40
- Rep Power
- 0
Re: Scanner Ignoring input
Thanks, That was what I meant. I'm going to look into threads a little bit more and see if I can make this work.
- 04-15-2012, 11:05 AM #10
Senior Member
- Join Date
- Jan 2011
- Location
- Belgrade, Serbia
- Posts
- 227
- Rep Power
- 3
Similar Threads
-
Scanner input into array
By wizar in forum New To JavaReplies: 1Last Post: 12-09-2011, 04:57 AM -
Converting Scanner input to JOptionPane
By Mideoan in forum New To JavaReplies: 1Last Post: 03-09-2011, 11:26 PM -
End Scanner int input with text value
By BillyB in forum New To JavaReplies: 3Last Post: 03-07-2011, 05:13 PM -
how to reverse some input from scanner
By moncur in forum New To JavaReplies: 24Last Post: 11-09-2010, 02:59 PM -
Scanner input problem
By slayer_azure in forum New To JavaReplies: 3Last Post: 05-26-2008, 10:49 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks