JOptionPane breaks scanner input if keyboard is pressed - java

I've simplified the issue to the code below.
When ran, you see a message.
If you use the mouse and click on 'ok', then you can continue to type new commands.
If you press 'enter' or 'space' on the keyboard, then keyboard input breaks. I.e one can type things into the command line but a 'return' doesn't do anything.
import java.io.IOException;
import java.util.Scanner;
import javax.swing.JOptionPane;
public class MainClass {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
while(true){
System.out.println("hello, please enter your command");
String UsrCmd = sc.nextLine();
System.out.println("you tyep: " + UsrCmd);
JOptionPane.showMessageDialog (null,"Dialog Text","Title Text",JOptionPane.QUESTION_MESSAGE);
//Work around for Linux users:
//Runtime.getRuntime().exec("/usr/bin/zenity --info --text='hello'");
}
}
}
(I'm new to S.o.f. I apologize for mistakes in advance. Please kindly correct me where I make them. Thank you).
JR 1.7 Fedor 20 64bit.

It should work normally, since you dont have any statement that could stop the while execution, or a break; command. However, I would advice you to use JDK(java developer kit) instead of JRE.

Related

Scanner and JOptionPane not working properly together

I have a Scanner and i am getting day of the week from console then i want to show day in JOptionPane.showMessageDialog method but its not working.
public class TestClass {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String day = scan.nextLine();
JOptionPane.showMessageDialog(null, day);
}
}
what is wrong?
thanks in advance.
Not sure if this helps, but I could find no issue with your code. Sharing what I've tried and maybe that'll help you debug your issue.
package simple.concepts.com;
import java.util.Scanner;
import javax.swing.JOptionPane;
public class Test {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter day");
String day = scan.nextLine();
JOptionPane.showMessageDialog(null, day);
scan.close();
}
}
While giving the input :
Enter day : Monday
(Press enter)
You should be seeing the pop-up with 'Monday'
EDIT:
Try running eclipse with administration rights. Restart and run again. Also run the program once and minimize all the windows, you should see it. It constantly takes input until and unless you close the pop-up.
As a temporary solution you could create a JFrame and call the JOptionpane with the frame as it's parent component.
import javax.swing.*
// Other imp code
JFrame m = new JFrame();
JOptionpane.showMessageDialog(m,day);

How to Exit Java Window after a command in Blue-J?

I am making a Program to find Values of Roots of a given Quadratic Equation.
This is the code -
import java.util.*;
class Success
{
public static void main(String[] args)
{
String input1;
Scanner sc=new Scanner(System.in);
System.out.println();
System.out.println();
System.out.println("-------------------------------------------------------------------");
System.out.println("-------------------------------------------------------------------");
System.out.println("!! Success !!");
System.out.println("------- (-_-) -------");
System.out.println();
System.out.println("Let's Try Again!!!");
System.out.println();
System.out.println("Enter anything to Exit or 1 to go back to Main Menu.");
input1 = sc.nextLine();
if(input1.equals("1"))
{
System.out.print('\f');
Main_Menu.main(args);
}
else
{
System.exit(0);
}
}
}
Here I use System.exit(0); to exit the program. But using this just minimizes the window. Is there any way to close the window.
P.S - This is just a class which links to Main_Menu Class if the entered value is 1. This is not the full code!
Quadratic Equations - BlueJ Program - Image
Thank You
The call to System.exit(0) just terminates the currently running Java Virtual Machine. It doesn't necessarily close the terminal window. A Java program does not know anything about it's context, so it cannot access the window it is using for its output.
However, there are two different ways to clear the terminal in BlueJ. You can get BlueJ to automatically clear the terminal before every interactive method call. To do this, activate the 'Clear screen at method call' option in the 'Options' menu of the terminal. You can also clear the terminal programmatically from within your program. Printing a formfeed character (unicode 000C) clears the BlueJ terminal, for example:
System.out.print('\u000C');
This will work in the BlueJ terminal, but is not guaranteed to have the same effect in all terminals. You could, for example, create a method with this in it and call that method whenever you want to clear the terminal screen.
public void clearScreen()
{
System.out.print('\u000C');
}

Scanner.hasNext() not working properly when running in Eclipse

I wrote a simple program to loop and find max of a set of numbers input by the user as:
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int currMax, currEl;
currMax = sc.nextInt();
while (sc.hasNext()) {
currEl = sc.nextInt();
currMax = (currEl > currMax ? currEl : currMax);
}
sc.close();
System.out.println(currMax);
} // end function main
}
I am using Eclipse on Windows.
When I run it the first time it runs fine, and considers Ctrl-Z as EOF and exits the loop. But second time onwards, it does not seem to read the EOF. I am unable to explain this, or fix this behavior.. what do you think is going on, and how do I fix it??
Follow-up: The problem happens with Eclipse, and not when I use cmd line. I suspect this is what is happening -- if I use cmd line, I can do Ctrl-Z and then hit Enter, but if I use Eclipse, I believe as soon as I hit Ctrl-Z, s.hasNext() evaluates to false and the above program terminates.

how to add a kb scanner in a println ? and how to make it so the code doesnt go all at once

Basically i want it to ask you what your name is,
so like
System.out.println("Stranger: what is your name?");
then here like System.out.println("Name:");kb scanner //so i dont have to skip a line
and also how would i make it so that after the first dialog you would have to hit space or a key to get the next system.out.println
import java.io.*;
import java.util.Scanner;
public class tester {
public static void main(String[] args) {
System.out.println("Stranger: Good morning");
System.out.println("Stranger: you had a terrible Dream..");
System.out.println("Stranger: what is your name?");
System.out.println("Name:");
Scanner kbReader = new Scanner (System.in);
String s = kbReader.next();
System.out.println("Nice to meet you " + s);
System.out.println("my name is Master Wizard.");
System.out.println("");
System.out.println("");
System.out.println("");
System.out.println("");
System.out.println("");
}
}
Thanks guys! im sorry im new to this stuff lol, even tho this is basic, my blue pelican book doesnt mention this stuff
I'm not completely sure what you're asking, but perhaps what you mean is that you don't want a new line after System.out.println("Name:");, and if that is the case all you must do is change it to System.out.print("Name: ");. println appends a \n character, print does not.
The pause can be accomplished by doing kbReader.next();, I believe.
Perhaps you mean to use:
System.out.print("Name: ");
String s = kbReader.nextLine(); // better to use nextLine
Using print instead of println will allow the user to type their answer on the same line as the prompt. I'm not sure if I fully understand your question though.
Edit
You state:
Thank you this worked ! wish i could vote you, but also how would i make it so all the code doesnt shoot out at once??
You're welcome. To slow down the println's there are several things you could do:
you could use a java.util.Timer if you want to have each println be called every such and such msecs.
or for the same thing, use Thread.sleep(...)
or if you want to wait for the user to press enter for the next question, prompt the user for them to do this, and use kbReader.nextLine() to pause program flow while waiting ofr the user to respond.

Java Menu Loop Problems; Program loops too soon?

I am working on an assignment where I have to write a program to encrypt and decrypt Caeser ciphers. The part I am having trouble with though is not encrypting or decrypting, but another one of the requirements, which was that I have to provide a menu so that the user can choose to encrypt, decrypt, or quit. Furthermore, the program should keep prompting the use until the use selects quit. My code so far is:
import java.util.*;
public class CaeserShiftTester
{
public static void main (String[] args)
{
Scanner in = new Scanner(System.in);
String choice = "";
while (!choice.equalsIgnoreCase("C"))
{
System.out.println("\nPlease select an option");
System.out.println("[A] Encrypt Code");
System.out.println("[B] Decrypt Code");
System.out.println("[C] Quit");
choice = in.next();
if(choice.equalsIgnoreCase("A"))
{
System.out.println("Please enter your key:");
final int KEY = in.nextInt();
System.out.println(CaeserShiftEncryption.shiftAlphabet(KEY));
System.out.println("\nPlease enter your message:");
String message = in.nextLine();
System.out.println(CaeserShiftEncryption.encryptCode(message,KEY));
}
if(choice.equalsIgnoreCase("C"))
{
System.out.println();
}
}
}
}
My problem is, after the "New Alphabet" is printed out to the screen, the program loops back to the very beginning, asking the user to choose a, b, or c. The use never gets a chance to enter a message to be encrypted. Unfortunately, I am required to print out the New Alphabet that is generated, and I can't think of what might be wrong here. I hope you guys can help me out.
Also, the shiftAlphabet and encryptCode methods are both fully functional.
Take a look on this website: http://www.java-made-easy.com/java-scanner.html
particularly
don't try to scan text with nextLine(); AFTER using nextInt() with the same scanner! It doesn't work well with Java Scanner, and many Java developers opt to just use another Scanner for integers
Problems occur with the Scanner class when mixing different type of scans and then using the same Scanner instance with nextLine.
When you get such a problem try to create a new Scanner instance. A simple cure would be to have 1 Scanner for using nextLine and another for everything else.
In the past I've had problems with using the Scanner when it came to next and nextLine, the cure for me was to simply stick to only using nextLine.

Categories

Resources