Code:
public static void main(String args[]){
DBConnectionPool.ConnectionPool();
int scelta = 0;
Scanner sc = new Scanner(System.in);
for(;;) {
System.out.println("Selezionare il tipo di operazione da eseguire:");
System.out.println("1: Inserimento");
System.out.println("2: Modifica");
System.out.println("3: Cancellazione");
System.out.println("4: Altre");
System.out.println("5: Liste");
System.out.println("98: TEST");
System.out.println("99: Esci");
scelta = sc.nextInt();
sc.nextLine();
switch (scelta) {
case 1: opInserimento(sc);
break;
case 2: opModifica(sc);
break;
case 3: opCancella(sc);
break;
case 4: altreOp(sc);
break;
case 5: liste(sc);
break;
case 98: testSuite.testStart();
case 99:
System.out.println("Uscita");
sc.close();
return;
}
}
}
This cycle works for the first time, but after the selected method returns, once it gets to the nextInt() it throws NoSuchElementException. The inside of one of those methods (they're all the same pretty much)
static private void opInserimento(Scanner sc) {
int selezione = 0;
System.out.println("Selezionare l'operazione: ");
System.out.println("1: Inserisci allievo");
System.out.println("2: Inserisci corso");
System.out.println("3: Inserisci esame");
System.out.println("4: Inserisci gara");
System.out.println("5: Inserisci istruttore");
System.out.println("6: Inserisci lezione");
System.out.println("7: Inserisci sede");
System.out.println("8: Inserisci numero di telefono di un allievo");
System.out.println("9: Inserisci numero di telefono di un'istruttore");
System.out.println("99: Indietro");
selezione = sc.nextInt();
sc.nextLine();
switch(selezione) {
case 1: QueryInserimento.inserireAllievo();
break;
case 2: QueryInserimento.inserireCorso();
break;
case 3: QueryInserimento.inserireEsame();
break;
case 4: QueryInserimento.inserireGara();
break;
case 5: QueryInserimento.inserireIstruttore();
break;
case 6: QueryInserimento.inserireLezione();
break;
case 7: QueryInserimento.inserireSede();
break;
case 8: QueryUtenti.aggiungiTelefonoAllievo();
break;
case 9: QueryUtenti.aggiungiTelefonoIstruttore();
break;
case 99: return;
}
}
It's a sub-menu basically. If I skip the switch block the cycle works fine, so I'm assuming it must be a problem that happens in the sub-menu. Any ideas?
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 trying to validate switch case statement to make sure user do not enter any character or string types. How do i achieve that? I have been trying using while statement but i am unable to validate it.
public static boolean showMenu()
{
// Print welcome message
System.out.println("\nApplication Menu : ");
System.out.println("[1] Help");
System.out.println("[2] Add");
System.out.println("[3] Subtract");
System.out.println("[4] Multiply");
System.out.println("[5] Divide");
System.out.println("[6] Quit");
int SelectMenu = jin.nextInt();
switch(SelectMenu)
{
case 1:
showChoice();
break;
case 2:
add();
break;
case 3:
subtract();
break;
case 4:
multiply();
break;
case 5:
divide();
break;
case 6:
return false;
}
return true;
}
You can use a loop as follows :
String line = jin.nextLine();
while(!line.matches("\\d")){
System.out.println("Nope, please enter a digit");
line = jin.nextLine();
}
int selectMenu = Integer.parseInt(line);
Take a look at this simple example and adapt it to your code:
/**
*
* #author Momir Sarac
*/
public class SwitchExample {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("\nApplication Menu : ");
System.out.println("[1] Help");
System.out.println("[2] Add");
System.out.println("[3] Subtract");
System.out.println("[4] Multiply");
System.out.println("[5] Divide");
System.out.println("[6] Quit");
while (!scanner.hasNextInt()) scanner.next();
int number = scanner.nextInt();
switch (number) {
case 1:
clickedHelp();
break;
case 2:
clickedAdd();
break;
case 3:
clickedSubtract();
break;
case 4:
clickedMultiply();
break;
case 5:
clickedDivide();
break;
default:
clickedSomething();
}
}
public static void clickedHelp(){
System.out.println("Help!");
}
public static void clickedAdd(){
System.out.println("Add");
}
public static void clickedSubtract(){
System.out.println("Subtract");
}
public static void clickedMultiply(){
System.out.println("Multiply");
}
public static void clickedDivide(){
System.out.println("Divide");
}
public static void clickedSomething(){
System.out.println("Exit");
System.exit(0);
}
}
Hope you need to do something like this
public static boolean showMenu()
{
while(true) {
// Print welcome message
System.out.println("\nApplication Menu : ");
System.out.println("[1] Help");
System.out.println("[2] Add");
System.out.println("[3] Subtract");
System.out.println("[4] Multiply");
System.out.println("[5] Divide");
System.out.println("[6] Quit");
int SelectMenu = jin.nextInt();
switch(SelectMenu)
{
case 1:
showChoice();
break;
case 2:
add();
break;
case 3:
subtract();
break;
case 4:
multiply();
break;
case 5:
divide();
break;
case 6:
return false;
}
}
}
I'm not able to understand that why is the compiler just shows running(which is forever) when I change char to int in this program. By changing I mean using just int to take the option number and hence using int numbers itself in switch.
This one is normal char 1 which is working-
public static void main(String args[])
throws java.io.IOException{
char option; int i=0;
do{
if(i==1)
System.out.println("\nNotice: Wrong option chosen, pick again.");
i=1;
System.out.println("Help on:");
System.out.println("1. if");
System.out.println("2. switch");
System.out.println("3. while");
System.out.println("4. do-while");
System.out.println("5. for");
System.out.println("Pick any option for brief informatrion.");
option= (char)System.in.read();
}while(option<'1' || option>'5');
switch(option){
case '1':
System.out.println("The If:\n");
System.out.println("If(condition) statement;");
System.out.println("else statement;");
break;
case '2':
System.out.println("The Switch:\n");
System.out.println("switch(expression){");
System.out.println(" case constant:");
System.out.println(" statement sequence");
System.out.println(" break;");
System.out.println(" // ...");
System.out.println("}");
break;
case '3':
System.out.println("The While:\n");
System.out.println("while(condition statement;)");
break;
case '4':
System.out.println("The Do-While:\n");
System.out.println("do{");
System.out.println(" statement;");
System.out.println("}while(condition);");
break;
case '5':
System.out.println("The For:\n");
System.out.println("for(init; condition; iteration){");
System.out.println(" statement;");
System.out.println("}");
break;
}
}
}
This is int 1 which just keeps running forver
public static void main(String args[])
throws java.io.IOException{
int option; int i=0;
do{
if(i==1)
System.out.println("\nNotice: Wrong option chosen, pick again.");
i=1;
System.out.println("Help on:");
System.out.println("1. if");
System.out.println("2. switch");
System.out.println("3. while");
System.out.println("4. do-while");
System.out.println("5. for");
System.out.println("Pick any option for brief informatrion.");
option= System.in.read();
}while(option<1 || option>5);
switch(option){
case 1:
System.out.println("The If:\n");
System.out.println("If(condition) statement;");
System.out.println("else statement;");
break;
case 2:
System.out.println("The Switch:\n");
System.out.println("switch(expression){");
System.out.println(" case constant:");
System.out.println(" statement sequence");
System.out.println(" break;");
System.out.println(" // ...");
System.out.println("}");
break;
case 3:
System.out.println("The While:\n");
System.out.println("while(condition statement;)");
break;
case 4:
System.out.println("The Do-While:\n");
System.out.println("do{");
System.out.println(" statement;");
System.out.println("}while(condition);");
break;
case 5:
System.out.println("The For:\n");
System.out.println("for(init; condition; iteration){");
System.out.println(" statement;");
System.out.println("}");
break;
}
}
}
See https://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html#read%28%29
The System.in.read return an int represent for character.
Let's say you type 1, it will read as 49.
And if you use file to be stdin when it reach EOF it will return -1
Even if the char version, the same error happens.
See this demo: https://ideone.com/Q83qxn
here is the method class
import java.util.Scanner;
public class Methods extends Basic {
Scanner keyboard = new Scanner(System.in);
public void Nome(){
String name = keyboard .nextLine();
System.out.println("what is your name");
switch(name){
case "john":
if(name.length() < 5){
System.out.println("your name is average");
break;
}
case "jason":
if(name.length()>5){
System.out.println("your name is not average");
break;
}
}
}
public void Dates(){
System.out.println("Enter a number and we will tell you what month it is:");
int jay = keyboard.nextInt();
switch(jay){
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;
default:
System.out.println("that aint in the month dawg");
}
}
}
and here is the main class
public class Basic {
public static void main(String args[]) {
Methods dateo = new Methods();
Methods nomalo = new Methods();
nomalo.Nome();
dateo.Dates();
}
}
so yea, I am a complete noob. and I am really struggling when learning Java :( A lot of my friends who started learning java can now make their own 2 games and here I am struggling with a basic concept...
so what is the problem in this code?
Edit - Forget what I said here. Your code is working fine aparently. Just put your
println before you make the scan.
System.out.println("what is your name");
String name = keyboard.nextLine();
So problems is you are reading the data from keyboard before you printed 'what is your name'
The Methods class does not need to extend Basic. the name method does not need to have switch case.
So the Methods class would look like below,
public class Methods {
Scanner keyboard = new Scanner(System.in);
public void name(){
System.out.println("what is your name");
String name = keyboard.nextLine();
if(name.length() < 5){
System.out.println("your name is average");
} else {
System.out.println("your name is not average");
}
}
public void Dates(){
System.out.println("Enter a number and we will tell you what month it is:");
int jay = keyboard.nextInt();
switch(jay){
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;
default:
System.out.println("that aint in the month dawg");
}
}
}
I want the user to enter their grade (ex. A+) and I would give them the result. I need help adding the user input into the console because I am really confused as to how I would add it in nested cases.
My code:
public class SwitchCase {
public static void main(String[] args) {
String plusorminus ="+-";
char mark = plusorminus.charAt(0);
char modifier = 0;
if(plusorminus.length() > 1)
{
modifier = plusorminus.charAt(1);
}
java.util.Scanner input=new java.util.Scanner(System.in);
switch(mark)
{
case 'A':
switch(modifier)
{
case '+': System.out.println("Your grade is 90-99.99%"); break;
case '-': System.out.println("Your grade is 90-84.99%"); break;
default: System.out.println("Your grade is 85-89.99%"); break;
}
break;
case 'B':
switch(modifier)
{
case'+': System.out.println("Your grade is 77.00 - 79.99%"); break;
case'-': System.out.println("Your grade is 70.00 - 72.99%"); break;
default: System.out.println("Your grade is 73.00 - 76.99%"); break;
}
break;
case 'C':
switch(modifier)
{
case'+': System.out.println("Your grade range is 67.00 - 69.99%"); break;
case'-': System.out.println("Your grade range is 60.00 - 62.99%"); break;
default: System.out.println("Your grade range is 63.00 - 66.99%"); break;
}
break;
case 'D':
switch(modifier)
{
case'+': System.out.println("Your grade range is 55.00 - 59.99%"); break;
case'-': System.out.println("-"); break;
default: System.out.println("Your grade range is 50.00 - 54.99%"); break;
}
break;
case 'F':
switch(modifier)
{
default: System.out.println("Your grade range is 0.00-49.99%"); break;
}
break;
}
input.close();
}
}
input.nextLine() will return a String that you can assign to a variable. You are using char in your switch statement, so you will need to get the first letter of that String to allow this.
java.util.Scanner input = new java.util.Scanner(System.in);
String userInputString = input.nextLine();
char mark = userInputString.charAt(0);
Another solution, instead of steven35, is do in that way (this way, you can integrate from what you already had done)
public class SwitchCase {
public static void main(String[] args) {
Scanner scanIn = new Scanner(System.in);
System.out.println("Input a mark");
String line = scanIn.nextLine();
if (line.length()>2){
System.out.println("ex: +a / -a / a");
return;
}
char[] cA = line.substring(1, 2).toUpperCase().toCharArray();
char[] cB = line.substring(0, 1).toUpperCase().toCharArray();
char mark = cA[0];
char modifier = cB[0];
System.out.println(" INPUT: "+modifier+mark);
switch(mark)
{
case 'A':
switch(modifier)
{
case '+': System.out.println("Your grade is 90-99.99%"); break;
case '-': System.out.println("Your grade is 90-84.99%"); break;
default: System.out.println("Your grade is 85-89.99%"); break;
}
break;
case 'B':
switch(modifier)
{
case'+': System.out.println("Your grade is 77.00 - 79.99%"); break;
case'-': System.out.println("Your grade is 70.00 - 72.99%"); break;
default: System.out.println("Your grade is 73.00 - 76.99%"); break;
}
break;
case 'C':
switch(modifier)
{
case'+': System.out.println("Your grade range is 67.00 - 69.99%"); break;
case'-': System.out.println("Your grade range is 60.00 - 62.99%"); break;
default: System.out.println("Your grade range is 63.00 - 66.99%"); break;
}
break;
case 'D':
switch(modifier)
{
case'+': System.out.println("Your grade range is 55.00 - 59.99%"); break;
case'-': System.out.println("-"); break;
default: System.out.println("Your grade range is 50.00 - 54.99%"); break;
}
break;
case 'F':
switch(modifier)
{
default: System.out.println("Your grade range is 0.00-49.99%"); break;
}
break;
}
//input.close();
}
}