I try to make a menu system, and I need to know how I can restart JTextField, so that by entering "x" number it shows me another menu, and you can continue browsing between the menus
JTextField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String Cap = JTextField.getText();
switch (Cap) {
case "1":
paintConsole("Menu1");
JTextField.setText("");
// In this site is my problem, I don't know how to reset a JTextField. PLEASE HELP ME
Cap = JTextField.getText();
if (Cap.equals("1")) {
paintConsole("Menu9");
}
break;
case "2":
paintConsole("Menu2");
break;
case "3":
paintConsole("Menu3");
break;
case "4":
paintConsole("Menu4");
case "5":
paintConsole("Menu5");
break;
case "6":
paintConsole("Menu6");
break;
}
}
});
}
Related
Hi i have a txt file that have a number in bunch of rows and i get this error
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
what can i add in my code to make it work it seems to be right to me but idont know why i get error
public static void main(String[] args) {
String input;
try {
Scanner scan = new Scanner(new File("andy.txt"));
while (scan.hasNextLine()) {
lineCounters++;
input = scan.nextLine();
putArray(sigFig(input));
}
calcPercentage();
makeGraph();
scan.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
here is my putArray
public static void putArray(char input) {
switch (input) {
case '1':
++digitCounters[0];
break;
case '2':
++digitCounters[1];
break;
case '3':
++digitCounters[2];
break;
case '4':
++digitCounters[3];
break;
case '5':
++digitCounters[4];
break;
case '6':
++digitCounters[5];
break;
case '7':
++digitCounters[6];
break;
case '8':
++digitCounters[7];
break;
case '9':
++digitCounters[8];
break;
}
}
It might be your putArray function which launches that exception. Can I have a look of it?
I'm stuck at a question that requests me if the user insert something else instead "e" "x" nums between 1 to 10, then it's returning "incorrect"
here's the code:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
try{
int Num = 0;
Num = sc.nextInt();
String str = sc.nextLine();
switch (Num) {
case 1: case 2: case 3: case 4:
case 5: case 6: case 7: case 8:
case 9: case 10:
System.out.println("this is a volume");
break;
case : if((Num>10)||(Num <0))
System.out.println("this is incorrect");
break;
}
switch (str) {
case "e": case "E":
System.out.println("Shutting Down");
break;
case "x" : case "X":
System.out.println("Mute");
break;
case "a":case "b":case "c":case "d":case "f":case "g":case "h":case "i":case "j":
case "k":case "l":case "m":case "n":case "o":case "p":case "q":case "r":case "s":
case "t":case "u":case "v":case "w":case "y":case "z":
System.out.println("this is incorrect");
break;
default:
break;
}
}
catch(Exception e) {
System.out.println("stopped");
}
}
}
thank you
If I understand your question properly, as you did not specify how many time the user should enter the input. However, you can have a look at this example, take the idea of the approach and then create your own one.
import java.util.Scanner;
public class ParseInput {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Insert E for Shutdown, X for Mute and Numbers between 1 to 10 for Volume");
String input = in.nextLine().trim(); // read the entire line after removing spaces if any, then parse it
if(input.length()>0){ // that means the user entered something
try{
int volumeValue = Integer.parseInt(input); // if it's not a number, it will throw exception that will be handled in the catch block
if(volumeValue>=1 && volumeValue<=10){// then evaluate the value
System.out.println("This is a Volume");
}
else{
System.out.println("Incorrect Volume Value");
}
}catch(NumberFormatException e){ // if you reach this block that means it's not a number
if(input.equalsIgnoreCase("e")){ // to accept both uppercase and lowercase
System.out.println("Shutting Down");
}
else if(input.equalsIgnoreCase("x")){
System.out.println("Mute");
}
else{
System.out.println("Incorrect Input");
}
}
}
else{
System.out.println("You have NOT entered anything!");
}
}
}
Test
Insert E for Shutdown, X for Mute and Numbers between 1 to 10 for Volume
e -> Shutting Down
x -> Mute
5 -> This is a Volume
12 -> Incorrect Volume Value
ZzZ -> Incorrect Input
-> You have NOT entered anything!
Please try the below code
switch (Num)
{
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
case 10:
System.out.println("this is a volume");
break;
default:
{
if ((Num > 10) || (Num < 0))
{
System.out.println("this is incorrect");
}
break;
}
}
I am trying to make a proper marking system for my multiple choice quiz, but when I answer the quiz, only the default in my switch statement will appear, How can I put in the input?
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
int mark = 0;
switch (mark) {
case 5:
JOptionPane.showMessageDialog(null, "D");
break;
case 6:
JOptionPane.showMessageDialog(null, "C");
break;
case 7:
JOptionPane.showMessageDialog(null, "B");
break;
case 8:
JOptionPane.showMessageDialog(null, "A-");
break;
case 9:
JOptionPane.showMessageDialog(null, "A");
break;
case 10:
JOptionPane.showMessageDialog(null, "A+");
break;
default:
JOptionPane.showMessageDialog(null, "F");
break;
}
Please check the value of mark. It is 0 hence default is showing.
Please check.
Please put break; statement in all case like.
case 6:
JOptionPane.showMessageDialog(null, "C");
break;
You must use a break; after each statement, otherwise the rest will be evaluated and the last will count.
Here's an example taken from here:
switch(expression) {
case value :
// Statements
break;
case value :
// Statements
break;
// You can have any number of case statements.
default : // Optional
// Statements
}
Here is a do..while loop with switch menu..
I am trying to make the user be able to return to the menu which is
System.out.println("Options: (C)reate, (R)ead, (W)rite, (L)ist, (S)ave or (E)xit. (*****)");
But after much trial and errors, I still cant get back to the main menu after choosing C, R, W, L or S.
Below is a short snippet of the codes..
do {
System.out.println("Options: (C)reate, (R)ead, (W)rite, (L)ist, (S)ave or (E)xit.
(*****)");
Scanner switchC = new Scanner(System.in);
System.out.println("Please enter your choice: ");
choice = switchC.next().charAt(0);
switch (Character.toUpperCase(choice)) {
case 'C':
System.out.println("C");
break;
case 'R':
break;
case 'W':
break;
case 'L':
break;
case 'S':
break;
case 'E':
System.exit(1);
default:
System.out.println("Options: (C)reate, (R)ead, (W)rite, (L)ist, (S)ave or (E)xit. (*****)");
}
} while(choice!='E');
try
do{
}
while(Character.toUpperCase(choice) !='E')
For class I am attempting to design a menu driven program which has sub-menu's. I'm having some logic issues.
Originally I made it so that the switch driven menu's just called each other and the sub-menus would call back to the main menu and so on. I was told this would eventually blow the stack by going forward with calls and not going backwards. I would like to confirm this and perhaps sort out my loop logic.
My desire is that when a user hits r or x in the submenu week1 it bounces back and reprints the main menu. Currently the submenu just reprints with the switch statement not working. My guess is that the value of main menu is still set to 1, but if i call it and pass a new value aren't I taking the stack 1 deeper?
The first menu.
//method to print and navigate the main menu
public void mainMenu(String choice){
toOutput.printMenu("src/datastructures/menus/main.txt");
choice = input.myScanner.nextLine();
do {
switch (choice){
case "1": toBaseNumberSystems.week1();
break;
case "2": toOutput.printMenu("src/datastructures/menus/week2.txt");
break;
case "3": toOutput.printMenu("src/datastructures/menus/week3.txt");
break;
case "4": toOutput.printMenu("src/datastructures/menus/week4.txt");
break;
case "5": toOutput.printMenu("src/datastructures/menus/week5.txt");
break;
case "6": toOutput.printMenu("src/datastructures/menus/week6.txt");
break;
case "7": toOutput.printMenu("src/datastructures/menus/week7.txt");
break;
case "x": choice="x";
System.out.println("Goodbye");
break;
default: System.out.println("The character entered was not understood"
+ ", please try again");
mainMenu(choice);
break;
}
} while (!"x".equals(choice));
Calls only week1 menu, which in turn calls other menus.
public void week1(){
toOutput.printMenu("src/datastructures/menus/baseNumberSystems.txt");
choice = input.myScanner.nextLine();
do{
switch (choice){
case "1": toOutput.printMenu("src/datastructures/menus/week1Base8.txt");
week1Base8();
break;
case "2": toOutput.printMenu("src/datastructures/menus/week1Base5.txt");
week1Base5();
break;
case "3": toOutput.printMenu("src/datastructures/menus/week1Base4.txt");
week1Base4();
break;
case "4": toOutput.printMenu("src/datastructures/menus/week1Base2.txt");
week1Base2();
break;
case "r": choice="r";
break;
case "x": choice="x";
System.out.println("Goodbye");
break;
default: System.out.println("The character entered wass not understood"
+ ", please try again");
break;
}
}while (!"x".equals(choice)&&!"r".equals(choice));
}
//method to print and control the base8 menu
public void week1Base8(){
int base;
toOutput.printMenu("src/datastructures/menus/week1Base8.txt");
choice = input.myScanner.nextLine();
switch (choice){
case "1": base=8;
toBase(base);
break;
case "r": week1();
break;
case "x": choice="x";
System.out.println("Goodbye");
break;
default: System.out.println("The character entered was not understood"
+ ", please try again");
break;
}
}
// method to print and control the base5 menu
public void week1Base5(){
toOutput.printMenu("src/datastructures/menus/week1Base5.txt");
int base;
choice = input.myScanner.nextLine();
switch (choice){
case "1": base=5;
toBase(base);
break;
case "r": week1();
break;
case "x": choice="x";
System.out.println("Goodbye");
break;
default: System.out.println("The character entered was not understood"
+ ", please try again");
break;
}
}
//method to print and control the base4 menu
public void week1Base4(){
toOutput.printMenu("src/datastructures/menus/week1Base4.txt");
int base;
choice = input.myScanner.nextLine();
switch (choice){
case "1": base=4;
toBase(base);
case "r": week1();
break;
case "x": choice="x";
System.out.println("Goodbye");
break;
default: System.out.println("The character entered was not understood"
+ ", please try again");
break;
}
}
//method to print and control the base2 menu
public void week1Base2(){
toOutput.printMenu("src/datastructures/menus/week1Base2.txt");
int base;
choice = input.myScanner.nextLine();
switch (choice){
case "1": base=2;
toBase(base);
case "r": week1();
break;
case "x": choice="x";
System.out.println("Goodbye");
break;
default: System.out.println("The character entered was not understood"
+ ", please try again");
break;
}
}