import java.util.Scanner; public class Lab3_Ex1 { public static void main (Strin
ID: 3614007 • Letter: I
Question
import java.util.Scanner;public class Lab3_Ex1
{
public static void main (String [] args)
{
Scanner keyboard = new Scanner(System.in);
float x;
int y;
char ch1, ch2;
String name;
String input;
System.out.print( "Enter a character: ");
input = keyboard.next();
ch1 = input.charAt(0);
System.out.print( "Enter a number: ");
y = keyboard.nextInt();
System.out.print( "Enter another character:");
input = keyboard.next();
ch2 = input.charAt(0);
System.out.print( "Enter a name:"); name = keyboard.next();
System.out.print( "Enter a floating point value:");
x = keyboard.nextFloat();
System.out.println(" ch1 = " + ch1);
System.out.println( "y = " + y);
System.out.println( "ch2 = " + ch2);
System.out.println( "Name is " + name);
System.out.println( "x = " + x);
System.exit(0);
}
}
Re-write the above program to use JOptionPane dialog boxes for allinput
and output operations.
Explanation / Answer
publicclass Lab3_Ex1
{
publicstatic voidmain (String [] args)
float x;
String stringY; // this variable will store the int as astring and we convert string to int later.
String strFloat; // this variable will store the float as a string and weconvert string to int later.
int y;
char ch1,ch2;
String name;
String input;
input=JOptionPane.showInputDialog(null, "Enter acharacter: "); // using JOptionpane we are storing theinput
ch1 = input.charAt(0);
stringY=JOptionPane.showInputDialog(null, "Enter a number:");// usingJOptionpane we are storing the input
y=Integer.parseInt(stringY); //here we are converting string to int.
input=JOptionPane.showInputDialog(null, "Enter anothercharacter: ");
ch2 = input.charAt(0);
name=JOptionPane.showInputDialog(null, "Enter a name:");
strFloat=JOptionPane.showInputDialog(null, "Enter a floatingpoint value: ");
JOptionPane.showMessageDialog(null," ch1 =" + ch1,"Character#1",JOptionPane.INFORMATION_MESSAGE);
JOptionPane.showMessageDialog(null, "y =" + y,"Integer#1",JOptionPane.INFORMATION_MESSAGE);
JOptionPane.showMessageDialog(null, "ch2 =" + ch2, "Character#2",JOptionPane.INFORMATION_MESSAGE);
JOptionPane.showMessageDialog(null,"Name is" + name, "Name",JOptionPane.INFORMATION_MESSAGE);
JOptionPane.showMessageDialog(null, "x =" + x, "Floatingnumber",JOptionPane.INFORMATION_MESSAGE);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.