Problem: Ask the user to enter 0-9999 and convert it into words using switch case and JOptionPane...
My question: I figure out how to convert the other numbers but if I entered 11-19, the output for example I enter 11 - "Ten One" how can I get the right output for this?
public class NumbertoWords {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
int num=Integer.parseInt(JOptionPane.showInputDialog("Enter a Number from [0-9999]"));
int thou=num/1000;
int temp=num%1000; // 9999
int hun=temp/100; //999 -> 9
int temp1=temp%100;
int tens=temp1/10; //99
int ones=temp%10;
String display="";
switch(thou){
case 9: display=" Nine Thousand"; break;
case 8: display=" Eight Thousand"; break;
}
switch (hun){
case 9: display=display+ " Nine Hundred"; break;
case 8: display=display+ " Eight Hundred"; break;
}
switch (tens){
case 9: display=display+ " Ninety"; break;
}
switch (ones){
case 9: display=display+ " Nine"; break;
}
JOptionPane.showMessageDialog(null, display,"Number to Words",JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
}
Related
I've recently tried to make some switch case function. Now, I'm currently trying to make an option but it seems it always skips the if part in case 1. The input just doesn't go in the if - else part.
#include <stdio.h>
int main(){
int option;
char choice;
int celcius;
menu:
printf("Welcome to program.\n");
printf("1. Input celcius\n");
printf("2. Convert To Kelvin\n");
printf("3. Convert To Fahrenheit\n");
printf("4. Convert To Reamur\n");
scanf("%d", &option);
switch(option){
case 1:
scanf("%d", &celcius);
printf("The value is : %d\n", celcius);
printf("Continue? : (y/n)\n"); scanf("%c", &choice);getchar();
if(choice == 'y' || choice == 'Y'){
goto menu;
}else if(choice == 'n' || choice == 'N');
break;
}
case 2:
printf("Please input the value: "); scanf("%d", &celcius);
int kelvin = celcius + 273;
printf("Converted Value is : %d\n", kelvin);
break;
case 3:
printf("Please input the value: "); scanf("%d", &celcius);
int fahrenheit = celcius + 32;
printf("Converted Value is : %d\n", fahrenheit);
break;
case 4:
printf("Please input the value: "); scanf("%d", &celcius);
int reamur = 5/4 * celcius;
printf("Converted Value is : %d\n", reamur);
break;
default:
printf("Wrong input.\n");
}
return 0;
}
(incomplete code because currently trying to make the case 1 works.)
Any solutions will help : )
You have a getchar() after the scanf() for choice. This is reading the newline character that is left in the input buffer after the scanf() for option.
You need to remove the getchar() and change the scanf() for choice to:
scanf(" %c", &choice);
The space before the %c will skip any whitespace characters in the input buffer.
I've been tasked with creating a program using a switch statement to print the month names according to the exact number of days in the month. The program also needs to display an "error" message if the number of days entered does not correspond to any month. My code achieves these two goals. However i am wondering if there is shorter way to get the "error" message using a "less than" statement for when the user enters a number less than 28
import java.util.Scanner;
public class months {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int numb_days = 0;
System.out.println("Enter the # of days");
numb_days = keyboard.nextInt();
//This program will print 'May' when you run it.
String monthName;
switch (numb_days) {
case 30: monthName = "April, June, September, November ";
break;
case 31: monthName = "January, March, May, July, August, November, October, December ";
break;
case 28: monthName = "February";
break;
case 29: monthName = "February (only on leap years)";
break;
case 1: monthName = "Unknown";
break;
case 2: monthName = "Unknown";
break;
case 3: monthName = "Unknown";
break;
case 4: monthName = "Unknown";
break;
case 5: monthName = "Unknown";
break;
case 6: monthName = "Unknown";
break;
case 7: monthName = "Unknown";
break;
case 8: monthName = "Unknown";
break;
case 9: monthName = "Unknown";
break;
case 10: monthName = "Unknown";
break;
case 11: monthName = "Unknown";
break;
case 12: monthName = "Unknown";
break;
case 13: monthName = "Unknown";
break;
case 14: monthName = "Unknown";
break;
case 15: monthName = "Unknown";
break;
case 16: monthName = "Unknown";
break;
case 17: monthName = "Unknown";
break;
case 18: monthName = "Unknown";
break;
case 19: monthName = "Unknown";
break;
case 20: monthName = "Unknown";
break;
case 21: monthName = "Unknown";
break;
case 22: monthName = "Unknown";
break;
case 23: monthName = "Unknown";
break;
case 24: monthName = "Unknown";
break;
case 25: monthName = "Unknown";
break;
case 26: monthName = "Unknown";
break;
case 27: monthName = "Unknown";
break;
default: monthName = "Unknown";
break;
}
System.out.println(monthName);
}
}
The default statement is sufficient for handling all other cases that aren't caught by the specific cases for 28-31. So you can remove all other cases 1-27.
If you are required to use a less than statement you can use it before using the switch statement:
if (numb_days < 28) {
month_name = "unknown";
} else {
// Switch statement goes here
}
OK, so I have this very simple, very inefficient program, but that's alright, the only thing that I would like to know is how to print the first five numbers of the queue as zeros without changing the values of ssn1 through ssn5.
For example I would like to print 000001111, and not 111111111, but still have those values stored in the queue. Could someone help me with that? I simply would like to hide them.
I am trying to convert the integers of the queue into a string, create a substring, and then print the dummyint five time followed by the substring.
import java.util.*;
public class SSNQueue {
public static void main (String args[]) {
Scanner scan = new Scanner(System.in);
Queue<Integer> ssn = new LinkedList<Integer>();
int Dummyssn = 0;
System.out.println("Please enter each digit number of SSN");
System.out.print("Enter digit number 1: ");
int ssn1 = scan.nextInt();
System.out.print("Enter digit number 2: ");
int ssn2 = scan.nextInt();
System.out.print("Enter digit number 3: ");
int ssn3 = scan.nextInt();
System.out.print("Enter digit number 4: ");
int ssn4 = scan.nextInt();
System.out.print("Enter digit number 5: ");
int ssn5 = scan.nextInt();
System.out.print("Enter digit number 6: ");
int ssn6 = scan.nextInt();
System.out.print("Enter digit number 7: ");
int ssn7 = scan.nextInt();
System.out.print("Enter digit number 8: ");
int ssn8 = scan.nextInt();
System.out.print("Enter digit number 9: ");
int ssn9 = scan.nextInt();
ssn.add(ssn1);
ssn.add(ssn2);
ssn.add(ssn3);
ssn.add(ssn4);
ssn.add(ssn5);
ssn.add(ssn6);
ssn.add(ssn7);
ssn.add(ssn8);
ssn.add(ssn9);
Integer.toString(ssn);
String Subssn = ssn.substring(5);
System.out.println("The SSN is:" + ssn);
System.out.println("The last four digits of the SSN are:" + Dummyssn + Dummyssn + Dummyssn + Dummyssn + Dummyssn + Subssn);
}
}
There are several issues here that make it unclear exactly what you are trying to do. For one thing, you read in multiple int values (ssn1, ssn2...ssn9) then add them to a queue. You then call Integer.toString(ssn) . . . I would expect an error on that line. It seems like what you want to do there is use a loop to read the values back out of the queue, assign them to a temporary String variable each time, and then call the substring function on your temporary variable and print out the result. As it is right now, you are calling Integer.toString(ssn) which is on the entire queue and I don't think that will do what you want. The queue is still typed to Integer (Queue). Not to mention that the toString method returns a value which you are supposed to read into another variable or use at the time you call it. Calling it on its own line like that doesn't really achieve anything because even if it is returning some value, it is not being kept by your code for later use.
include
/* Declare function prototypes */
float Calculate_Duty (int, int);
void Print_Duty (float);
int main (void)
{
/* Declare all variables to be used in the program */
char more_to_process;
int origin, category, quantity, num_ship=0;
float unit_price;
float cost, duty, total_ship=0;
float total_duty=0, tax_rate=0;
/* Begin to execute the program */
printf("Do you have more customs forms to process? Type: Y for yes or N for no \n");
scanf(" %c", &more_to_process);
while ((more_to_process =='Y') && (more_to_process!='N'))
{
printf ("What is the origin of the goods? Type: 1 for US, 2 for China, 3 for Brazil \n");
scanf ("%d", &origin);
printf ("What category of goods? Type: 1 for food, 2 for clothing, 3 for wood \n");
scanf ("%d", &category);
printf ("What is the quantity? \n");
scanf ("%d", &quantity);
printf ("What is the unit price? \n");
scanf ("%f", &unit_price);
total_ship= quantity * unit_price;
/* Calculate the duty of the shipment */
tax_rate= Calculate_Duty (origin, category);
duty= tax_rate * total_ship;
total_duty+= duty;
/* Print the duty of the shipment */
printf ("Origin \t Category \t Quantity \t Unit Price \t Shipment \t Tax Rate \t Duty \t \n");
printf ("%d \t %d \t \t %d \t \t %02.f \t \t %0.2f \t %0.2f \t \t %0.2f \t \n", origin,
category, quantity, unit_price, total_ship, tax_rate, duty);
Print_Duty (duty);
printf ("Do you have more customs forms to process? Type: Y for yes, N for no \n");
scanf (" %c", &more_to_process);
++num_ship;
}
printf ("Transaction Summary: \n");
printf ("Number of Shipments Processed = %d \n", num_ship);
printf ("Total Duties Collected = $ %0.2f \n", total_duty);
return 0;
}
/* Perform the function Calculate_Duty */
float Calculate_Duty (int origin, int category)
{
float duty;
switch (origin)
{
/* Case 1 is for US */
case 1:
switch (category)
{
case 1:
duty=0;
break;
case 2:
duty= 0;
break;
case 3:
duty= .05;
break;
}
break;
/* Case 2 is for China */
case 2:
switch (category)
{
case 1:
duty= .02;
break;
case 2:
duty= .03;
break;
case 3:
duty= .04;
break;
}
break;
/* Case 3 is for Brazil */
case 3:
switch (category)
{
case 1:
duty= .01;
break;
case 2:
duty= .02;
break;
case 3:
duty= .08;
break;
}
return duty;
}
/* Perform the Print_Duty function */
void Print_Duty (float duty)
{
printf ("The amount due is $ %0.2f \n", duty);
}
As I can see one } is missing from switch(origin), before return duty.
I'm new to Java. I want to be able to input operational characters in a Scanner.
My code should produce the following,
Enter two numbers: 13 6
What operation? *
What is 13 * 6? 78
Correct!
I'm using a Switch-statement to store the operations available for the user.
Those are: +, -, * and /.
I recieve a run time error everytime I write an operation after the output.
import java.util.Scanner;
public class Användardialog4 {
public static void main(String[]args){
Scanner sc = new Scanner(System.in);
int svar;
String op;
char operator;
System.out.println("Enter two numbers: ");
int tal = sc.nextInt();
int tal2 = sc.nextInt();
System.out.println("Operation?");
op = sc.nextLine();
operator = op.charAt(0);
switch(operator){
case '+':
System.out.println("What is "+tal+" + "+tal2);
svar = sc.nextInt();
if (svar == tal+tal2)
System.out.println("Correct!");
else
System.out.println("Wrong - The right answer is: "+tal+tal2);
break;
case '*':
System.out.println("What is "+tal+" * "+tal2);
svar = sc.nextInt();
if (svar ==tal*tal2)
System.out.println("Correct!");
else
System.out.println("Wrong - The right answer is: "+tal*tal2);
break;
case '-':
System.out.println("What is "+tal+" - "+tal2);
svar = sc.nextInt();
if (svar == tal-tal2)
System.out.println("Correct!");
else
System.out.println("Wrong - The right answer is: "+(tal-tal2));
break;
case '/':
System.out.println("What is "+tal+" / "+tal2);
svar = sc.nextInt();
if (svar == tal/tal2)
System.out.println("Correct!");
else
System.out.println("Wrong - The right answer is: "+tal/tal2);
break;
}
}
}
The run time error I get is:
Exception in thread "main" java.util.InputMismatchException at
java.util.Scanner.throwFor(Unknown Source) at
java.util.Scanner.next(Unknown Source) at
java.util.Scanner.nextInt(Unknown Source) at
java.util.Scanner.nextInt(Unknown Source) at
Användardialog4.main(Dialog4.java:16)
So my question is, how do I use characters in a Scanner class?
Isn't there a nextChar(); or something alike in Java?
Change op = sc.nextLine(); to op = sc.next();
public String next(); finds and returns the next complete token from this scanner. Since your operation is only one token/character, this is the way to go.
public String nextLine(); advances this scanner past the current line and returns the input that was skipped. Since you don't have endline-character in your stream, this fails.
int ans;
switch(operator){
case '+':
ans = tal + tal2;
break;
case '*':
ans = tal * tal2;
break;
case '-':
ans = tal - tal2;
break;
case '/':
ans = tal / tal2;
break;
}
System.out.println("What is "+tal+operator+tal2+"?");
svar = sc.nextInt();
if (svar == ans)
System.out.println("Correct!");
else
System.out.println("Wrong - The right answer is: "+ans);
A small change for code quality improvement. Removed redundant code.