Results 1 to 10 of 10
  1. #1
    totalspelnerd is offline Member
    Join Date
    Mar 2012
    Location
    Sweden
    Posts
    88
    Rep Power
    0

    Default Scanner Ignoring input

    I making a program that is using a while loop, the problem i'm having is to stop it. I want the loop to go on until i write "stop" or something in the console. I've noticed when i write "stop = sc.nextLine();" it pauses the program until i type something. Is this possible?

  2. #2
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    292
    Rep Power
    4

    Default Re: Scanner Ignoring input

    Can you post code you have so far so we can see what you are doing..

  3. #3
    totalspelnerd is offline Member
    Join Date
    Mar 2012
    Location
    Sweden
    Posts
    88
    Rep Power
    0

    Default 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 :(");
    		}
    	}
    }

  4. #4
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    292
    Rep Power
    4

    Default Re: Scanner Ignoring input

    I've noticed when i write "stop = sc.nextLine();" it pauses the program until i type something
    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..

  5. #5
    totalspelnerd is offline Member
    Join Date
    Mar 2012
    Location
    Sweden
    Posts
    88
    Rep Power
    0

    Default 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.

  6. #6
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    292
    Rep Power
    4

    Default 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...

  7. #7
    totalspelnerd is offline Member
    Join Date
    Mar 2012
    Location
    Sweden
    Posts
    88
    Rep Power
    0

    Default 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.

  8. #8
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    292
    Rep Power
    4

    Default 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)...

  9. #9
    totalspelnerd is offline Member
    Join Date
    Mar 2012
    Location
    Sweden
    Posts
    88
    Rep Power
    0

    Default 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.

  10. #10
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    292
    Rep Power
    4

    Default Re: Scanner Ignoring input

    Take a good look, threads are somewhat tricky. Google for some examples on threads, especially for stoping/distroying it since all those easy methods are deprecated now...

Similar Threads

  1. Scanner input into array
    By wizar in forum New To Java
    Replies: 1
    Last Post: 12-09-2011, 04:57 AM
  2. Converting Scanner input to JOptionPane
    By Mideoan in forum New To Java
    Replies: 1
    Last Post: 03-09-2011, 11:26 PM
  3. End Scanner int input with text value
    By BillyB in forum New To Java
    Replies: 3
    Last Post: 03-07-2011, 05:13 PM
  4. how to reverse some input from scanner
    By moncur in forum New To Java
    Replies: 24
    Last Post: 11-09-2010, 02:59 PM
  5. Scanner input problem
    By slayer_azure in forum New To Java
    Replies: 3
    Last Post: 05-26-2008, 10:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •