Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: How to limit scanner time and stop it with a condition

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default How to limit scanner time and stop it with a condition

    Hello,

    For a game, I have differents times of input :

    -first : 1 or 2 or 3
    -second : 1 or 2
    -third : 0 to 7 or 8

    I want to know how to limit the time that one scanner will be "display" ?
    How to stop it, if the input is equal at 1 or 2,for exemple ?

    Maybe, if I make a class step which will represent the step of input, I will only make a switch, but I don't know what to put inside...

    For the moment, I only have a basic scanner method :
    		private static int result()
    		{
    			int res = -2;
    			try
    			{
    				Scanner touche = new Scanner(System.in);
    				String lettre = touche.nextLine();
    				res = Character.getNumericValue(lettre.charAt(0));
    			}
    			catch (java.lang.StringIndexOutOfBoundsException exception)
    			{
    			}
    			return res;
    		}

    The notion of time is only missing, can you help me ?

    Thanks


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: How to limit scanner time and stop it with a condition

    I want to know how to limit the time that one scanner will be "display" ?
    Show me how you "display" a scanner. I do not know what that means.

    Are you using the Timer class?

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: How to limit scanner time and stop it with a condition

    Hello,

    Display is like time of activate or time that the user can input something.
    With one of your previous thread, a made a little code :
    import java.util.Scanner;
    import java.util.Random;
     
    public class TimedScanner implements Runnable
    {
     
    	private Scanner scanner;
    	private StringBuilder buffer;
    	private boolean reading;
    	static Thread t;
     
    	public TimedScanner()
    	{
    		scanner = new Scanner(System.in);
    		buffer = new StringBuilder();
    		reading = false;
    		t = new Thread(this);
    		t.setDaemon(true);
    		t.start();
    	}
     
    	public String nextLine(long time)
    	{
    		reading = true;
    		String result = null;
    		long startTime = System.currentTimeMillis();
    		while (System.currentTimeMillis() - startTime < time && result == null)
    		{
    			try
    			{
    				Thread.sleep(30);
    			}
    			catch (InterruptedException e)
    			{
    			}
    			synchronized (buffer)
    			{
    				if (buffer.length() > 0)
    				{
    					Scanner temp = new Scanner(buffer.toString());
    					result = temp.nextLine();
    				}
    			}
    		}
    		t.interrupt();
    		reading = false;
    		return result;
    	}
     
    	@Override
    	public void run()
    	{
    		while (scanner.hasNextLine())
    		{
    			String line = scanner.nextLine();
    			synchronized (buffer)
    			{
    				if (reading)
    				{
     
    					buffer.append(line);
    					buffer.append("\n");
     
    				}
    				else
    				{
    					// flush the buffer
    					if (buffer.length() != 0)
    					{
    						buffer.delete(0, buffer.length());
    					}
    				}
    			}
    		}
    	}
    }
     
    public class Solitaire {
     
    		private static int etapeSaisieJeu;
     
    		private static  int getRandomInt(int limite) {  
    		    return (new Random()).nextInt(limite) ;  
    		} 
    		private static int resultatSaisieUtilisateur()
    		{
    			int res = -2;
    			long tempsDebut = System.currentTimeMillis();
    			switch(etapeSaisieJeu)
    			{
    			case 1 : res = fiabilisationSaisie(1,2,3,-1,-1,-1,-1,-1,-1); break;
    			case 2 : res = fiabilisationSaisie(1,2,-1,-1,-1,-1,-1,-1,-1);break;
    			case 3 :if(typePlateau == 1)
    					{
    						res = fiabilisationSaisie(0,1,2,3,4,5,6,-1,-1);
    					}
    					else if(typePlateau == 2)
    					{
    						res = fiabilisationSaisie(0,1,2,3,4,5,6,7,-1);
    					}
    					else 
    					{
    						res = fiabilisationSaisie(0,1,2,3,4,5,6,7,8);
    					}
    					 break;
    			case 4 : res = fiabilisationSaisie(2,4,6,8,-1,-1,-1,-1,-1);break;
    			default : break;
    			}
    			long tempsFin = System.currentTimeMillis();
    			calculDifferenceDeTemps(tempsFin,tempsDebut,0);
    			return res;
    		}
     
    		private static int fiabilisationSaisie(int valeurAttendue1,int valeurAttendue2,int valeurAttendue3,int valeurAttendue4,int valeurAttendue5,int valeurAttendue6,int valeurAttendue7,int valeurAttendue8,int valeurAttendue9)
    		{
    			int res = -2;
    			try
    			{
    				//TimedScanner scanner = new TimedScanner();
    				//String lettre = scanner.nextLine(10000);
    				Scanner sc = new Scanner(System.in);
    				String lettre = sc.nextLine();
    				System.out.println("a"+lettre+"a");
    				if (lettre == null)
    				{
    					System.out.println("More faster, next time");
    					res = correctionSaisie(valeurAttendue1,valeurAttendue2,valeurAttendue3,valeurAttendue4,valeurAttendue5,valeurAttendue6,valeurAttendue7,valeurAttendue8,valeurAttendue9);
    				}
    				else
    				{
    					res = elementAUnePositiondonnee(lettre,0);
    					//TimedScanner.t.interrupt();
    				}
    			}
    			catch (java.lang.StringIndexOutOfBoundsException exception)
    			{
    			}
    			return res;
    		}
     
    		private static int correctionSaisie(int valeurAttendue1,int valeurAttendue2,int valeurAttendue3,int valeurAttendue4,int valeurAttendue5,int valeurAttendue6,int valeurAttendue7,int valeurAttendue8,int valeurAttendue9)
    		{
    			int res = -1;
    			int max = Math.max(valeurAttendue1, Math.max(valeurAttendue2, Math.max(valeurAttendue3,Math.max(valeurAttendue4,Math.max(valeurAttendue5, Math.max(valeurAttendue6,Math.max(valeurAttendue7,Math.max(valeurAttendue8,valeurAttendue9))))))));
    			if ((res != valeurAttendue1) || (res != valeurAttendue2) || (res != valeurAttendue3) || (res != valeurAttendue4) || (res != valeurAttendue5) || (res != valeurAttendue6) || (res != valeurAttendue7) || (res != valeurAttendue8) || (res != valeurAttendue9))
    			{
    				res = getRandomInt(max);
    			}
    			return res;
    		}
     
    		public static void main (String[] args)
    		{
    			int res = -2;
    			etapeSaisieJeu = 1
    			res= resultatSaisieUtilisateur();
    			System.out.println(res);
    			etapeSaisieJeu = 2
    			res= resultatSaisieUtilisateur();
    			System.out.println(res);
    			etapeSaisieJeu = 3
    			res= resultatSaisieUtilisateur();
    			System.out.println(res);
    			etapeSaisieJeu = 4
    			res= resultatSaisieUtilisateur();
    			System.out.println(res);
    		}
    }

    Now, I think that the thread t is always alive when I put something. Because for the first time of scanner, all is good. But after, If I enter 10 times the same value, my string lettre, is always null.

    Thanks you for your help

Similar Threads

  1. How do i limit the input time
    By itayj in forum Java Theory & Questions
    Replies: 6
    Last Post: October 24th, 2011, 03:47 PM
  2. [SOLVED] Calculating when the condition is during a time frame
    By YoungMaker in forum Java Theory & Questions
    Replies: 2
    Last Post: September 28th, 2011, 08:39 AM
  3. [SOLVED] How to stop the same window from having more than one instance of it open at a time.
    By javapenguin in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 9th, 2011, 05:47 PM
  4. set time limit on running of a thread
    By z.zojaji in forum Threads
    Replies: 3
    Last Post: July 10th, 2010, 10:57 AM
  5. Replies: 4
    Last Post: April 27th, 2010, 01:18 AM