18

I want the user to enter information again in the first while loop after pressing any key on the keyboard. How do I achieve that? Am I doing something wrong with the while loops. SHould I just have one while loop?

 import java.util.Scanner;

 public class TestMagicSquare
 {
  public static void main(String[] args)
 {    
    boolean run1 =  true;
    boolean run2 = true;

    Square magic = new Square();

    Scanner in = new Scanner(System.in);

    while(run1 = true)
    {
        System.out.print("Enter an integer(x to exit): ");
        if(!in.hasNextInt())
        {
            if(in.next().equals("x"))
            {
                break;
            }

            else
            {
                System.out.println("*** Invalid data entry ***");               
            }                    
        }
        else
        {
            magic.add(in.nextInt());
        }
     }

    while(run2 = true)
    {
        System.out.println();
        if(!magic.isSquare())
        {
            System.out.println("Step 1. Numbers do not make a square");            
            break;
        }
        else
        {
            System.out.println("Step 1. Numbers make a square");
        }

        System.out.println();
        if(!magic.isUnique())
        {
            System.out.println("Step 2. Numbers are not unique");
            break;
        }
        else
        {
            System.out.println("Step 2. Numbers are unique");
        }

        System.out.println();
        magic.create2DArray();
        if(!magic.isMagic())
        {
            System.out.println("Step 3. But it is NOT a magic square!");
            break;
        }
        else
        {
            System.out.println("Step 3. Yes, it is a MAGIC SQUARE!");
        }

        System.out.println();
        System.out.print("Press any key to continue...");// Here I want the simulation
        in.next();
        if(in.next().equals("x"))
        {
            break;
        }
        else
        {
            run1 = true;
        }
      }
    }

   }
user2840682
  • 351
  • 2
  • 11
  • 22
  • 1
    You can't. Basically Java's implementation of the stdin will only return when the use presses [Enter]. You could use a JNA or JNI solution, but that might be more work then is worth it... – MadProgrammer Nov 08 '13 at 23:42
  • 1
    Add an action listener to the keyboard. – Isaiah Taylor Nov 08 '13 at 23:44
  • What are you doing with all those `runx` booleans? You only assign `true` values to them all the time. What's their purpose? – Andrei Nicusan Nov 08 '13 at 23:45
  • I am just going to link to this question: http://stackoverflow.com/questions/1066318/how-to-read-a-single-char-from-the-console-in-java-as-the-user-types-it . The top answer may give you some insight. – Lan Nov 08 '13 at 23:50

4 Answers4

24

You can create this function (good only for enter key) and use it where ever you want in your code:

 private void pressAnyKeyToContinue()
 { 
        System.out.println("Press Enter key to continue...");
        try
        {
            System.in.read();
        }  
        catch(Exception e)
        {}  
 }
E235
  • 8,802
  • 16
  • 74
  • 122
3

1) See while(run1 = true) and while(run2 = true)

= is assignment operator in java. use == operator to compare primitives

2) You can do like this

while(in.hasNext()){

}
Prabhakaran Ramaswamy
  • 24,936
  • 10
  • 54
  • 63
  • `true` (if you indulge the pun of words), but that doesn't solve OP's issue. Actually an infinite loop is a very common choice to have with a `Scanner`. – Mena Nov 08 '13 at 23:50
  • was about to answer. I'll +1 your answer only because nothing in it is wrong per se, yet you aren't solving OP's issue. However, solving OP's issue would imply recoding half his/her mess, so I'm voting to close instead. – Mena Nov 08 '13 at 23:55
1

Before getting into implementation details, I think you need to step back and re-examine your algorithm a bit. From what I gather, you want get a list of integers from the user and determine if they form a magic square. You can do the first step in a single while loop. Something like this pseudo-code:

while (true)
    print "Enter an integer (x to stop): "
    input = text from stdin
    if input is 'x'
        break
    else if input is not an integer
        print "non integer value entered, aborting..."
        return
    else
        add input to magic object

After that, you can output details about the numbers:

if magic is a magic square
    print "this is a magic square"
else
    print "this is not a magic square"

// etc, etc.....
upcrob
  • 165
  • 11
1

My answer still only works with the Enter key but it does so without leaving stuff on the input stream that could get in the way later (System.in.read() can potentially leave things on the stream that get read in on the next input). So I read the whole line (here I use Scanner but I guess that isn't actually necessary, you just need something to get rid of the entire line in the input stream):

public void pressEnterKeyToContinue()
{ 
        System.out.println("Press Enter key to continue...");
        Scanner s = new Scanner(System.in);
        s.nextLine();
}
Jay Laughlin
  • 144
  • 1
  • 8