Thursday, July 9, 2009

What is the java equivalent of ' system("pause") ' in c++?

. im a newbie in java:: in c++ ' system("pause") ', makes the program pause until a keystroke. what is it in java, thanks.

What is the java equivalent of ' system("pause") ' in c++?
u wanted to pause the computer until key stroke is pressed. one simple method is to read from the console a character so that the system will hold until any key is pressed.this is how i actually haulted the program in C and C++. here u will do





import java.io.*;


public class pause{


public static void main( String[] a )


{


try{


DataInputStream din = new DataInputStream( System.in );





// do ur task here





din.readLine(); // when u want to hold simply call this


}


catch( IOException e)


{


System.out.println(" IOException throwed ");


}


}


}





good luck...
Reply:It depends on the compiler..I usually write my own pause function like this one...but yahoo is messing it up %26gt;.%26lt;


On the lines where it says cin.ignore be sure to take out the spaces after the _ and before and after %26lt;std::streamsize%26gt;. It's suppose to be all together but had to type it that way so you could see it.











void pause()


{


std::cin.clear();


std::cin.ignore(std::numeric_ limits %26lt;std::streamsize%26gt; ::max());


//Clear whatever's still in the buffer


std::cout %26lt;%26lt; "Press Enter to continue . . .\n";


std::cin.ignore(std::numeric_ limits %26lt;std::streamsize%26gt; ::max(),


'\n');


}


No comments:

Post a Comment