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;
}
Related
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;
}
}
```
`
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;
I'm pretty new to Java, and coding in general. In my class we need the user to input the month (1-12) and a day (1-30) and we determine on a set date (june 15 to sept 30th) if it is or is not monsoon season. I'm trying to use a if else statement in a switch to say that anytime before june 15 is not monsoon season but my code keeps showing both if and else statement. Any help is welcomed, thank you!
import java.util.*;
public class Monsoon
{ public static void main (String[]args)
{
Scanner kb = new Scanner (System.in);
// Prompt the user to enter month & Day
System.out.print("Please enter a month(1-12) and day (1-31): " );
int month = kb.nextInt();
int day = kb.nextInt();
// using a switch statement to show months
switch (month){
case 1:
System.out.println("is NOT monsoon season");
break;
case 2:
System.out.println("is NOT monsoon season");
break;
case 3:
System.out.println("is NOT monsoon season");
break;
case 4:
System.out.println("is NOT monsoon season");
break;
case 5:
System.out.println("is NOT monsoon season");
break;
case 6:
System.out.println("“is monsoon season");
// use if else statement so user knows that before june 15 is not monsoon season
if (day>=15)
System.out.print("it is monsoon season");
else
System.out.print("it is not monsoon season");
break;
case 7:
System.out.println("“is monsoon season");
break;
case 8:
System.out.println("“is monsoon season");
break;
case 9:
System.out.println("“is monsoon season");
break;
case 10:
System.out.println("is NOT monsoon season");
break;
case 11:
System.out.println("is NOT monsoon season");
break;
case 12:
System.out.println("is NOT monsoon season");
break;
default: System.out.println("not valid");
break;
}
}
}
Repetition is bad. You can do it like this, utilizing the fall-through feature of the switch-case:
boolean isMonsoon;
switch (month) {
case 7:
case 8:
case 9:
isMonsoon = true;
break;
case 1:
case 2:
case 3:
case 4:
case 5:
case 10:
case 11:
case 12:
isMonsoon = false;
break;
case 6:
// use if else statement so user knows that before june 15 is not monsoon season
if (day >= 15)
isMonsoon = true;
else
isMonsoon = false;
break;
default:
System.out.println("not valid");
break;
}
if (isMonsoon)
System.out.println("is monsoon season");
else
System.out.println("is NOT monsoon season");
Or, since the monsoon season is a range, using comparison operators is probably more suitable than a switch-case:
if ((month >= 7 && month < 10) || (month == 6 && day >= 15))
System.out.println("is monsoon season");
else
System.out.println("is NOT monsoon season");
I only need help with the input part. If the user inputs a number I need the program to read and output a case that equals the number that was input.
//This program will display the months of the year
public class MonthsOfTheYear {
public static void main(String[] args)
throws java.io.IOException{
int month;
System.out.println("Please enter a Month Number: ");
month = (int) System.in.read(); //Get an integer
switch (month) {
case 1: System.out.println("January");
break;
case 2: System.out.println("February");
break;
case 3: System.out.println("March");
break;
case 4: System.out.println("April");
break;
case 5: System.out.println("May");
break;
case 6: System.out.println("June");
break;
case 7: System.out.println("July");
break;
case 8: System.out.println("August");
break;
case 9: System.out.println("September");
break;
case 10: System.out.println("October");
break;
case 11: System.out.println("November");
break;
case 12: System.out.println("December");
break;
default: System.out.println("Invalid Month");
break;
}
System.out.println();
}
}
Try using the Console class instead: http://docs.oracle.com/javase/6/docs/api/java/io/Console.html to obtain user input, then convert the String to a number with Integer.parseInt(String)
Also, be aware that the next version of Java (8) will support Strings in case blocks.
class vehicle
{
int passengers;
int fuelcap;
int mpg;
}
import java.io.*;
class Vehicle_Demo
{
public static void main (String args[]) throws java.io.IOException
{
vehicle obj1 = new vehicle();
obj1.passengers=12;
obj1.fuelcap=9;
obj1.mpg=78;
System.out.println(obj1.passengers, obj1.fuelcap, obj1.mpg);
}
}
You can use Scanner to read your System.in
Scanner input = new Scanner(System.in);
month = input.nextInt();
Output:
Please enter a Month Number:
1
January
See related
Edit:
as noahz pointed out there is a Console class that covers the same functionality. For an idea of the difference between the two, read this.
Try to use TextIO Input Functions
This is better than scanner input
You need to compile TextIO.java fist as a prerequisite so that TextIO.class must be found in the same folder.
use this input function instead:
month = TextIO.getChar();
Please let me know if you have problems on this method.
Thanks!
Correct me if I'm wrong, but I'm pretty sure it will still work with "system.in.read". You're switch cases do not have single quotes around the input you want to be processed. If you want case one to be run when you type '1' your case should be case '1', not case 1.
There problem here was Java have cast a character to an integer, therefore you get an ASCII value.
For instance, when you type 5 you've got 53 in ASCII. Check it taking the variable value in System.out.println();
I've tried to solve that changing month to a char type and then passing a char value to the switch control. But the problem is, getting System.in.read(); method you just can get a single character. At last you just can select between 1 to 9.
I suspect the solution is using a buffer reader. Until then, I leave you here my code:
public class MonthsOfTheYear {
public static void main(String[] args)
throws java.io.IOException{
char month;
System.out.println("Please enter a Month Number: ");
month = (char) System.in.read(); //Get an integer
//System.out.println("Actual value of :" + month);
switch (month) {
case '1': System.out.println("January");
break;
case '2': System.out.println("February");
break;
case '3': System.out.println("March");
break;
case '4': System.out.println("April");
break;
case '5': System.out.println("May");
break;
case '6': System.out.println("June");
break;
case '7': System.out.println("July");
break;
case '8': System.out.println("August");
break;
case '9': System.out.println("September");
break;
/*case '10': System.out.println("October");
break;
case '11': System.out.println("November");
break;
case '12': System.out.println("December");
break;*/
default: System.out.println("Invalid Month");
break;
}
System.out.println();
}
}