I programing for my Java class and I am doing a imperial to metric conversor, everything is going good but I am having trouble to display the results from the array it is storing, I want it to display everything in the same dialog box, I am trying to use JOptionPane but it is not working and I really do not know what to try or how to do it.
package imperialconvertor;
import javax.swing.JOptionPane;
/**
* #author Mateus Holovaty
*/
public class ImperialConvertor {
public static void main(String[] args) {
//List of variables
MetricToImperial[] met = new MetricToImperial[10];
ImperialToMetric[] imp = new ImperialToMetric[10];
String optionInput;
String result = "";
int a = 0, b = 0;
boolean quit = false;
while(!quit){
optionInput = JOptionPane.showInputDialog("Enter \n1: for metric to imperial \n2: for imperial to metric \nQ: to quit");
switch(optionInput){
case "1":
if(a < met.length){
met[a] = getMet();
a++;
}
else
JOptionPane.showMessageDialog(null, "Cannot enter more values for Metric to Imperial");
break;
case "2":
if(b < imp.length){
b++;
}
else
JOptionPane.showMessageDialog(null, "Cannot enter more values for Imperial to Metric");
break;
case "Q":
quit = true;
break;
} //Switch end
} //Quit loop end
}
public static MetricToImperial getMet(){
//List of variables
String option;
MetricToImperial newMet = new MetricToImperial();
option = JOptionPane.showInputDialog("""
What computation:
1: Kilometer to Miles
2: Kilometer to Feet
3: Meter to Feet
4: Centimeters to Inch
5: Milimeters to Inch
6: Liters to Quarts
7: Liters to Gallons
8: Milliliters to Cups
9: Milliliters to Ounces
10: Kilogram to Tons
11: Kilogram to Pounds
12: Grams to Ounces
13: Grams to Pounds
14: Milligrams to Ounces
15: Celsius to Fahrenheit""");
switch(option){
case "1":
newMet.setKmMi();
newMet.getKmMi();
break;
case "2":
newMet.setKmFt();
break;
case "3":
newMet.setMFt();
break;
case "4":
newMet.setCmIn();
break;
case "5":
newMet.setMmIn();
break;
case "6":
newMet.setLQt();
break;
case "7":
newMet.setLGal();
break;
case "8":
newMet.setMlC();
break;
case "9":
newMet.setMlOz();
break;
case "10":
newMet.setKgT();
break;
case "11":
newMet.setKgLb();
break;
case "12":
newMet.setGOz();
break;
case "13":
newMet.setGLb();
break;
case "14":
newMet.setMlOz();
break;
case "15":
newMet.setCF();
break;
}
return newMet;
}
}
```
`
Related
import java.util.*;
public class assigment4number3
{
public static void main (String [] args)
{
int mon, tues, wed, thurs, fri;
mon=1; tues=2; wed=3;thurs=4;fri=5;
Scanner day = new Scanner(System.in);
System.out.println("Enter Day of week: mon, tues, weds, thurs, fri, sat, sun");
String week= day.next();
switch (week) {
case 1:
System.out.println(" Discrete Math");
System.out.println("Philosophy");
System.out.println("Calculus");
break;
case 2:
System.out.println("Modern World");
System.out.println("Java Programming");
break;
case 3:
System.out.println(" Discrete Math");
System.out.println("Philosophy");
System.out.println("Calculus");
break;
case 4:
System.out.println("Modern World");
System.out.println("Java Programming");
break;
case 5:
System.out.println("Discreet Math");
System.out.println("Philosophy");
break;
default:
System.out.println("Its the weekend");
break;
}
}
}
I'm trying to use input in my switch
I'm writing a code where I can impute a day of the week and my schedule should pop up, but I'm having trouble using an input in my switch
All of your case labels are of type int. Change them to String or switch on an int. Like,
int week = day.nextInt();
or
switch (week) {
case "1":
System.out.println(" Discrete Math");
System.out.println("Philosophy");
System.out.println("Calculus");
break;
case "2":
System.out.println("Modern World");
System.out.println("Java Programming");
break;
case "3":
System.out.println(" Discrete Math");
System.out.println("Philosophy");
System.out.println("Calculus");
break;
case "4":
System.out.println("Modern World");
System.out.println("Java Programming");
break;
case "5":
System.out.println("Discreet Math");
System.out.println("Philosophy");
break;
default:
System.out.println("Its the weekend");
break;
}
If you mean to use the values mon - fri instead of numbers, you could use those for your case labels. Like,
String week = day.next();
switch (week.toLowerCase()) {
case "mon":
System.out.println(" Discrete Math");
System.out.println("Philosophy");
System.out.println("Calculus");
break;
case "tues":
System.out.println("Modern World");
System.out.println("Java Programming");
break;
case "weds":
System.out.println(" Discrete Math");
System.out.println("Philosophy");
System.out.println("Calculus");
break;
case "thurs":
System.out.println("Modern World");
System.out.println("Java Programming");
break;
case "fri":
System.out.println("Discreet Math");
System.out.println("Philosophy");
break;
default:
System.out.println("Its the weekend");
break;
}
I am working on a menu program and I want to be able to enter as many selections as I want without the menu looping as well after the input, currently it either infinite-loops, or the program ends after one input, being unable to perform a different selection after the first. Also I want to have a case where I exit the menu if I press 0.
public void showMenu() {
System.out.println("Welcome!");
System.out.println("Select an option:\n" +
"1. Adunare\n" +
"2. Scadere\n" +
"3. Inmultire\n" +
"4. Impartire\n" +
"5. Comparare numere\n" +
"6. List To Hundred\n" +
"7. Nr to list\n" +
"8. Contains\n" +
"9. Even numbers\n" +
"10. List of Strings\n" +
"11. Second largest number\n" +
"12. Second lowest number\n" +
"13. Number \n" +
"14. Number 2 \n" +
"15. Number 3\n" +
"16. String\n" +
"17. String 2\n" +
"18. Amount of snow\n" +
"19. Eligible to vote test\n" +
"20. Odd or even\n" +
"21. Dog\n" +
"22. Cat\n" +
"23. Elev");
}
public void runProgram() {
showMenu();
int numberFromUser = citire.readNumbers();
do {
switch (numberFromUser) {
case 1:
addition();
break;
case 2:
subtraction();
break;
case 3:
multiply();
break;
case 4:
divide();
break;
case 5:
comparareNumere();
break;
case 6:
listhundred();
break;
case 7:
setnumbertolist();
break;
case 8:
contain();
break;
case 9:
limit();
break;
case 10:
list();
break;
case 11:
secmax();
break;
case 12:
secmin();
break;
case 13:
nr();
break;
case 14:
nr2();
break;
case 15:
nr3();
break;
case 16:
string();
break;
case 17:
string2();
break;
case 18:
weather();
break;
case 19:
eligible();
break;
case 20:
oddoreven();
break;
case 21:
dog();
break;
case 22:
cat();
break;
case 23:
elev();
break;
default:
break;
}
} while (numberFromUser != 0);
}
Add
numberFromUser = citire.readNumbers();
into the loop to take numbers in the loop.
public void runProgram() {
showMenu();
int numberFromUser;
do {
numberFromUser = citire.readNumbers();
switch (numberFromUser) {
case 1:
addition();
break;
case 2:
subtraction();
break;
case 3:
multiply();
break;
case 4:
divide();
break;
case 5:
comparareNumere();
break;
case 6:
listhundred();
break;
case 7:
setnumbertolist();
break;
case 8:
contain();
break;
case 9:
limit();
break;
case 10:
list();
break;
case 11:
secmax();
break;
case 12:
secmin();
break;
case 13:
nr();
break;
case 14:
nr2();
break;
case 15:
nr3();
break;
case 16:
string();
break;
case 17:
string2();
break;
case 18:
weather();
break;
case 19:
eligible();
break;
case 20:
oddoreven();
break;
case 21:
dog();
break;
case 22:
cat();
break;
case 23:
elev();
break;
default:
break;
}
} while (numberFromUser != 0);
}
Try changing runProgram to following :
public void runProgram()
{
int numberFromUser;
showMenu();
while(true)
{
System.out.print("Enter choice: ");
numberFromUser = citire.readNumbers();
switch(NumberFromUser)
{
// All your case statements
case 0: System.exit(0);
}
}
}
this is the code-
import java.util.Scanner;
public class HelloWorld{
public static void main(String []args){
Scanner kb=new Scanner(System.in);
System.out.println("enter a four digit number");
int number=kb.nextInt();
int digit2=number;
switch (digit1)
{
case 1: System.out.println("One");break;
case 2: System.out.println("Two");break;
case 3: System.out.println("Three");break;
case 4: System.out.println("Four");break;
case 5: System.out.println("Five");break;
case 6: System.out.println("Six");break;
case 7: System.out.println("Seven");break;
case 8: System.out.println("Eight");break;
case 9: System.out.println("Nine");break;
case 0: System.out.println("Zero");break;
default: System.out.println("");break;
}
switch (digit2)
{
case 1: System.out.println("One");break;
case 2: System.out.println("Two");break;
case 3: System.out.println("Three");break;
case 4: System.out.println("Four");break;
case 5: System.out.println("Five");break;
case 6: System.out.println("Six");break;
case 7: System.out.println("Seven");break;
case 8: System.out.println("Eight");break;
case 9: System.out.println("Nine");break;
case 0: System.out.println("Zero");break;
default: System.out.println("");break;
}
}
}
And this error is coming
2 errors
sh-4.3$ javac HelloWorld.java
HelloWorld.java:10: error: cannot find symbol
switch (digit1)
^
symbol: variable digit1
location: class HelloWorld
HelloWorld.java:10: error: illegal start of type
switch (digit1)
^
2 errors
pls help me
You have not defined a variable named digit1.
Here's some rough code to do what you want:
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
System.out.println("Enter a four digit number");
String numStr = kb.next();
int number = 0;
try {
number = Integer.parseInt(numStr);
} catch (Exception e) {
System.out.println("Input was not a number!");
System.exit(1);
}
if (number < 1000) {
System.out.println("Number must be at least four digits!");
System.exit(1);
}
for (int digitNdx = 0; digitNdx < 4; digitNdx++) {
switch (numStr.charAt(digitNdx)) {
case '1':
System.out.println("One");
break;
case '2':
System.out.println("Two");
break;
case '3':
System.out.println("Three");
break;
case '4':
System.out.println("Four");
break;
case '5':
System.out.println("Five");
break;
case '6':
System.out.println("Six");
break;
case '7':
System.out.println("Seven");
break;
case '8':
System.out.println("Eight");
break;
case '9':
System.out.println("Nine");
break;
case '0':
System.out.println("Zero");
break;
default:
System.out.println("");
break;
}
}
}
}
Your code
Scanner kb = new Scanner(System.in);
System.out.println("enter a four digit number");
int number = kb.nextInt();
int digit2 = number;
switch (digit1) {
Error occur due to
here is no any variable assigned for digit1
Example
int digit1 = 5;
int digit2 = number;
Modify the program to accept a telephone number with any number of letters. The output should display a hyphen after the first 3 digits and subsequently a hyphen (-) after every four digits. Also, modify the program to process as many telephone numbers as the user wants.
Below is what i have to edit.
Scanner keyboard = new Scanner(System.in);
String telephone_letter;
int index;
System.out.print("Enter Telephone letters : ");
telephone_letter = keyboard.nextLine();
char aChar[] = telephone_letter.toCharArray();
int[] number = new int[100];
for(index=0;index<telephone_letter.length();index++){
switch (aChar[index])
{
case 'A':
case 'a':
case 'B':
case 'b':
case 'C':
case 'c':
number[index] = 2;
break;
case 'D':
case 'd':
case 'E':
case 'e':
case 'F':
case 'f':
number[index] = 3;
break;
case 'G':
case 'g':
case 'H':
case 'h':
case 'I':
case 'i':
number[index] = 4;
break;
case 'J':
case 'j':
case 'K':
case 'k':
case 'L':
case 'l':
number[index] = 5;
break;
case 'M':
case 'm':
case 'N':
case 'n':
case 'O':
case 'o':
number[index] = 6;
break;
case 'P':
case 'p':
case 'Q':
case 'q':
case 'R':
case 'r':
case 'S':
case 's':
number[index] = 7;
break;
case 'T':
case 't':
case 'U':
case 'u':
case 'V':
case 'v':
number[index] = 8;
break;
case 'W':
case 'w':
case 'X':
case 'x':
case 'Y':
case 'y':
case 'Z':
case 'z':
number[index] = 9;
break;
default:
break;
}
}
System.out.println("=======================================");
System.out.println("The Telephone letter is : " + telephone_letter);
System.out.println("The Phone number is : " + number[0]+number[1]+number[2]+"-"+number[3]+number[4]+number[5]+number[6]);
System.out.println("=======================================");
}
This part is what i tried.
Scanner keyboard = new Scanner(System.in);
String telephone_letter;
int index;
System.out.print("Enter Telephone letters : ");
telephone_letter = keyboard.nextLine();
char aChar[] = telephone_letter.toCharArray();
int[] number = new int[100];
while(telephone_letter !="2")
{
for(index=0;index<telephone_letter.length();index++){
switch (aChar[index])
{
case 'A':
case 'a':
case 'B':
case 'b':
case 'C':
case 'c':
number[index] = 2;
break;
case 'D':
case 'd':
case 'E':
case 'e':
case 'F':
case 'f':
number[index] = 3;
break;
case 'G':
case 'g':
case 'H':
case 'h':
case 'I':
case 'i':
number[index] = 4;
break;
case 'J':
case 'j':
case 'K':
case 'k':
case 'L':
case 'l':
number[index] = 5;
break;
case 'M':
case 'm':
case 'N':
case 'n':
case 'O':
case 'o':
number[index] = 6;
break;
case 'P':
case 'p':
case 'Q':
case 'q':
case 'R':
case 'r':
case 'S':
case 's':
number[index] = 7;
break;
case 'T':
case 't':
case 'U':
case 'u':
case 'V':
case 'v':
number[index] = 8;
break;
case 'W':
case 'w':
case 'X':
case 'x':
case 'Y':
case 'y':
case 'Z':
case 'z':
number[index] = 9;
break;
default:
break;
}
}
System.out.println("=======================================");
System.out.println("The Telephone letter is : " + telephone_letter);
System.out.println("The Phone number is : " + number[0]+number[1]+number[2]+"-"+number[3]+number[4]+number[5]+number[6]);
System.out.println("=======================================");
System.out.print("Enter Telephone letters : ");
telephone_letter = keyboard.nextLine();
}
My output is wrong.
Enter Telephone letters : fewfwef
======================================
The Telephone letter is : fewfwef
The Phone number is : 339-3933
=======================================
Enter Telephone letters : wsqsq
=======================================
The Telephone letter is : wsqsq
The Phone number is : 339-3933
=======================================
And another Question is how do i do this The output should display a hyphen after the first 3 digits and subsequently a hyphen (-) after every four digits.
This seems like a homework problem so here are some hints to help you solve it yourself.
(1) Using a fixed number like 100 is rarely a good idea:
int[] number = new int[aChar.length];
(2) The scanner can tell you if there is more to be scanned(read):
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter Telephone letters : ");
while (keyboard.hasNextLine()) {
String telephone_letter = keyboard.nextLine();
...
System.out.println("");
System.out.print("Enter Telephone letters : ");
}
(3) A loop is good for counting. If the first count is different, then start different:
int todash = 3;
System.out.print("The Phone number is : ");
for (int i=0; i < number.length; ++i) {
if (todash == 0) {
System.out.print("-");
todash = 4;
}
System.out.print(number[i]);
--todash;
}
System.out.println("");
I've rewritten you're code. It may be easier to work with.
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Telephone {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter Telephone letters : ");
String input = keyboard.nextLine();
TelephoneProcessor telephoneProcessor = new TelephoneProcessor(input);
String result = telephoneProcessor.proccess();
System.out.println("=======================================");
System.out.println("The Telephone letter is : " + input);
System.out.println("The Phone number is : " + result);
System.out.println("=======================================");
}
}
class TelephoneProcessor {
Map<String, String> matches = new HashMap<>();
String toProcess;
public TelephoneProcessor(String toProcess) {
this.toProcess = toProcess;
matches.put("[a-c]", "2");
matches.put("[d-f]", "3");
matches.put("[g-i]", "4");
matches.put("[j-l]", "5");
matches.put("[m-o]", "6");
matches.put("[p-r]", "7");
matches.put("[s-w]", "8");
matches.put("[x-z]", "9");
}
public String proccess() {
if(toProcess != "2") {
matches.forEach((regex, replacement) -> {
toProcess = toProcess.replaceAll(regex, replacement);
});
}
return toProcess.substring(0,3) + "-" + toProcess.substring(3,7);
}
}
It only asks once but that's easy to solve by making a class that contains the main method in a method and loops it.
I hope this helps for further extensions of the program :)
I was doing some some Java homework with my friend. The instructor wants him to write a Java program to translate an integer input into a numeric grade. 100-90 = A and so on. The only catch is that he must use a switch statement. He must also:
Include a case in your switch statement that will display a polite
error message if the user enters any number less than 0 or greater
than 100."
Originally I thought of this...
import java.util.Scanner;
public class grade
{
public static void main(String[] args)
{
int ng;//number grade
String lg = "";//letter grade
System.out.println("enter grade");
Scanner in = new Scanner(System.in);
ng = in.nextInt();
switch (ng/10)
{
case 10:
case 9:
lg = "A";
break;
case 8:
lg = "B";
break;
case 7:
lg = "C";
break;
case 6:
lg = "D";
break;
default:
lg = "F";
break;
}
System.out.println("You got an " + lg);
}
}
This isn't perfect because it allows values over 100 and below 0, but I am trying to avoid typing out every integer from 100-0. This seems like a ridiculous use of a switch statement and I can't imagine why a college professor would teach it, other than to illustrate the DRY principle.
Is there a better way that still uses the switch statement, but doesn't type every int from 100-0?
You could always add some minor complexity to your switch expression to make the cases simpler; this will calculate 90-100 as 10, 80-89 as 9 and so on, 101 and above will become 11 and above, and every input below 0 will become 0 or negative so they'll fall under default;
switch ((ng-ng/100+10)/10)
{
case 10:
lg = "A";
break;
case 9:
lg = "B";
break;
case 8:
lg = "C";
break;
case 7:
lg = "D";
break;
case 6: case 5: case 4:
case 3: case 2: case 1:
lg = "F";
break;
default:
System.out.println("Polite Error");
lg = "";
}
Yeah, no way in hell you would want to actually use a switch statement for this lol. But the way you've suggested is about the best way I can think of to do it.
I would make the default be for the error scenario though, because that could any integer less than 0 or over 100. Between 0 and 100, at least you have a finite number of cases (though you'll have to repeat the "F" case several times).
Nice use of integer division ;)
Okay, well this code officially embarrasses me and makes me cry. But here, just using switch statements.
import java.util.Scanner;
public class grade
{
public static void main(String[] args)
{
int ng;//number grade
String lg = "";//letter grade
boolean error = false;
System.out.println("enter grade");
Scanner in = new Scanner(System.in);
ng = in.nextInt();
switch (ng/10)
{
case 10:
switch (ng)
{
case 100:
lg = "A";
break;
default:
error = false;
break;
}
break;
case 9:
lg = "A";
break;
case 8:
lg = "B";
break;
case 7:
lg = "C";
break;
case 6:
lg = "D";
break;
case 5:
case 4:
case 3:
case 2:
case 1:
case 0:
lg = "F";
break;
default:
error = true;
break;
}
if (error) {
System.out.println("Sorry, the grade must be between 0 and 100");
} else {
System.out.println("You got an " + lg);
}
}
}
Blech.
How about:
String lg = null;
switch (ng/10)
{
case 10:
if (ng > 100) {
// polite error
break;
}
case 9:
lg = "A"
break;
case 8:
lg = "B";
break;
case 7:
lg = "C";
break;
case 6:
lg = "D";
break;
default:
if (ng < 0) {
// polite error
break;
}
lg = "F";
break;
}
After the switch you'd have to check if a grade were set or not.
if (lg == null) {
System.out.println("The input score was > 100 or < 0");
} else {
System.out.println("You got an " + lg);
}