Scanner and JOptionPane not working properly together - java

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);

Related

Math.pow(x,2) area of square

i am running a code, but i dont seem to be able to get the scanner working, anyone got an idea how?
public class verk34 {
public static void main(String[] args) {
System.out.println(Math.pow(x,2));
}
}
the thing is inserting a number on your own without altering the code.
As per my understanding, you need to add Scanner for taking input from end users and importing the java.util.Scanner statement.
Scanner in = new Scanner(System.in);
int x = in.nextInt();
First, you need to import scanner and Math
import java.util.Scanner
import java.lang.Math
Then If I am understanding correctly the problem, you need to do this in two steps :
-Instantiate a scanner and collect user input as an integer.
Scanner sc = new Scanner(System.in);
System.out.println(" Please enter the square area :");
int userValue = sc.nextInt();
The nextInt method will lock process and wait for a keyboard event before continuing. So enter a nulber with your keyboard.
-Use System.out.println with given value.
System.out.println(Math.pow(userValue,2));

Why won't the dialog box come up when I run this code in Eclipse?

Please see my code below, why won't the input dialog box come up in Eclipse when I run the program?
package cookieTest;
import javax.swing.JOptionPane;
import java.util.Scanner;
public class CookieCalories
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int cookiesEaten = input.nextInt();
int caloriesPerCookie = 300 / 4;
int calorieIntake = cookiesEaten * caloriesPerCookie;
JOptionPane.showInputDialog("How many cookies did you eat?");
System.out.print("Your calorie intake was: " + calorieIntake);
input.close();
System.exit(0);
}
}
So in this case, how you're calling the input dialog and getting the data is not correct, it should look like this:
int cookiesEaten = Integer.parseInt(JOptionPane.showInputDialog(null, "How many cookies did you eat?"));
You don't need a scanner when using the JOptionPane.showInputDialog() function as the input dialog handles the scanner. As for the leading null parameter, because we're not dealing with a custom built Swing GUI, we don't have a parent. But Java expects one, calling it explicitly and giving it the null parent allows it to open the pane. (Without it, the program just hangs there forever it seems).
EDIT: Keep in mind, JOptionPane takes some time to open the window sometimes. (roughly 10-15 seconds for me).
Your code should look like this:
package cookieTest;
import javax.swing.JOptionPane;
import java.util.Scanner;
public class CookieCalories
{
public static void main(String[] args)
{
int cookiesEaten = Integer.parseInt(JOptionPane.showInputDialog(null, "How many cookies did you eat?"));
int caloriesPerCookie = 300 / 4;
int calorieIntake = cookiesEaten * caloriesPerCookie;
System.out.print("Your calorie intake was: " + calorieIntake);
System.exit(0);
}
}

Java : Could not load or find main class TrudiHw

I keep getting this error when trying to run my basic java code. It compiles with no errors at all but for some reason the program wont actually run. I am using textpad. Here is my code.
/*
*TrudiHw.java
*Trudi Farrell
*19/10/15
*/
import java.util.Scanner;
public class TrudiHw{
public static void main (String args[]){
int input;
int answer;
Scanner keyboard;
keyboard = new Scanner(System.in);
System.out.println("How many students are attendting the trip?");
input = keyboard.nextInt();
answer = input/5;
System.out.println("The amount of cars you will need is " +answer);
}
}

JOptionPane breaks scanner input if keyboard is pressed

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.

Reading an integer from the command prompt in Java using Scanner

I can't figure out what I'm doing wrong here.
My code:
import java.util.Scanner;
public class test
{
public static void main(String args[])
{
Scanner input_scanner = new Scanner(System.in);
System.out.printf("\nEnter number:");
int num_shapes = input_scanner.nextInt();
System.out.printf("\n%d", num_shapes);
}
}
Each time the program is run, I enter an integer, press the enter key, am taken to a new line, enter a new integer, and hit the enter key again. The first integer is then displayed.
How can I get it to display the first integer directly after it is entered, without having to enter a second integer?
I've tried it with and without
input_scanner.nextLine();
following the line containing 'nextInt()', but get the same thing either way.
Any help in resolving this problem is greatly appreciated.
this works perfectly
import java.util.Scanner;
public class mainclass{
public static void main(String args[])
{
Scanner input_scanner = new Scanner(System.in);
System.out.println("Enter number:");
int num_shapes = input_scanner.nextInt();
System.out.println(num_shapes);
}
}

Categories

Resources