Results 1 to 5 of 5
  1. #1
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    213
    Rep Power
    10

    Default [SOLVED] Press any key to continue??

    I would like to have the user press ENTER or press any key to continue.

    The following snippet did not work. I also tried using Scanner.in as well, unsuccessfully.

    Java Code:
    package homenetwork.bkr.training;
    
    import java.io.IOException;
    import java.lang.management.ManagementFactory;
    import java.util.*;
    
    /**
     * This program shows data corruption when multiple threads access a data structure.
     * @author Administrator
     */
    public class UnsynchBankTest {
    		public static void main (String[] args)
    		{
    			final boolean debug = true;
    			
    ...
    //Here I want to write the PID of the application so I can monitor it in jconsole, using:
    
    System.out.println("PID = " + ManagementFactory.getRuntimeMXBean().getName());
    
    //User should press any key to continue...
    [B]??[/B]
    ...
    //Use a function to call jconsole + pid automatically before the following lines start to execute...
    			
    			Bank b = new Bank(NACCOUNTS, INITIAL_BALANCE);
    			int i;
    			for (i = 0; i < NACCOUNTS; i++)
    			{
    				TransferRunnable r = new TransferRunnable(b, i, INITIAL_BALANCE);
    				Thread t = new Thread(r);
    				t.start();
    			}
    
    		}
    		public static final int NACCOUNTS = 10;
    		public static final double INITIAL_BALANCE = 2000;
    }
    Attempted resolutions
    Here's how I tried to simplify the problem of reading user input...

    Java Code:
    import java.io.IOException;
    import java.util.*;
    import java.io.*;
    
    public class UserInput {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		
    		//FIX does not work
    		System.out.println("Press ENTER to continue");
    			try {
    				//failed attempt 1
    				System.in.read();
    				
    				//failed attempt 2
    				Scanner in = new Scanner(System.in);
    				int i = in.nextInt(); //is this optional since I don't require to read the value?
    				
    				//attempt 3
    				System.out.println("Enter something and hit ENTER:"); 
    				//open up standard input
    				BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    				String s = br.readLine(); [B]//I don't really need to store the input, so this variable is "wasted", is it?[/B]
    			} catch (IOException e) {
    				e.printStackTrace();
    				System.exit(1);
    			}		
    
    	}
    
    }
    NOTES (related links)
    1. Suggestion for reading an ENTER key. This does not work!
    2. Executing an external program. Good article!
    3. Scanner API
    4. Java interactive comman-line input Good article!

    :confused:
    Last edited by jon80; 06-14-2009 at 07:43 PM.

  2. #2
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    213
    Rep Power
    10

    Default

    This works...

    import java.io.IOException;
    import java.io.*;

    public class UserInput {

    /**
    * @param args
    */
    public static void main(String[] args) {

    try {

    System.out.println("Enter your fucking name:");
    //open up standard input
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String s = br.readLine();
    } catch (IOException e) {
    e.printStackTrace();
    System.exit(1);
    }

    }

    }

  3. #3
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    9

    Default

    Add a buffered stream/reader to the input stream and call readLine()

    EDIT:too slow
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  4. #4
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    213
    Rep Power
    10

    Default

    Is it possible to have it press any key to continue through some pre-defined method?

  5. #5
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    9

    Default

    If you define it.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

Similar Threads

  1. Program doesn't continue...
    By Reiyn in forum New To Java
    Replies: 7
    Last Post: 10-07-2008, 10:28 PM
  2. press any key to continue
    By dotnet007 in forum New To Java
    Replies: 3
    Last Post: 05-11-2008, 05:19 AM
  3. How to use Continue
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 07:46 PM
  4. Press any key to continue/press enter
    By JT4NK3D in forum New To Java
    Replies: 1
    Last Post: 11-17-2007, 10:27 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
  •