I would like to add a name to a queue (linked), one name at a time from a text file. If the user selects choice 1 then it should take the next name in from the lists.
Case 1 is not letting me input another choice if I want to add another name.
int choice = console.nextInt();
FileReader names = new FileReader("Customer.txt");
Scanner lookup = new Scanner(names);
Queue a = new Queue();
String customerName;
// This loop was just to verify that
while(lookup.hasNextLine() ){ // It was actually reading
customerName = lookup.next();
System.out.println(customerName);
}
while(true){
switch(choice){
case 1:
customerName = lookup.next();//For some reason its not giving me
a.enqueue(customerName); // The choice to enter another number
break; //even though I made the case while(true)
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
case 6:
System.out.println(" Exiting......");
break;
default:
continue;
}
break;
}
The problem here is that there is a break after the switch statement. This is causing your code to jump out of the while loop after one pass of the switch statement.
The solution is to remove the break, as such:
while(true){
switch(choice){
case 1:
customerName = lookup.next();//For some reason its not giving me
a.enqueue(customerName); // The choice to enter another number
break; //even though I made the case while(true)
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
case 6:
System.out.println(" Exiting......");
break;
default:
continue;
}
}
Related
I am trying to write a program that receives the number of sides from the
user and determines the type of figure using switch structure and a while sentinel-controlled loop, but every time I get an infinite loop. How can that be fixed?
import java.util.Scanner;
public class P1 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Please enter the number of sides:");
int s = input.nextInt();
while ( s!=-1)
{
switch (s)
{
case 1: System.out.println("Line");
break;
case 2:System.out.println("Angle");
break;
case 3:System.out.println("Triangle");
break;
case 4:System.out.println("Quadrilateral");
break;
case 5:System.out.println("Pentagon ");
break;
case 6:System.out.println("Hexagon");
break;
case 7:System.out.println("Heptagon");
break;
case 8:System.out.println("Octagon");
break;
case 9:System.out.println("Nonagon");
break;
case 10:System.out.println("Decagon");
break;
default: System.out.println("Enter a valid value:");
}
}
}
}
The while loop is written to continue as long as s!=-1; so you need to change s so that this expression is no longer true.
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
}
I have set up a console menu like so:
int userOption = printMenu(sc);
while(userOption != 6){
switch(userOption) {
case 1: //function 1
break;
case 2: //function 2
break;
case 3: //function 3
break;
case 4: //function 4
break;
case 5: //function 5
break;
case 6: //
break
default: //statement asking for valid option
}
}
However when I run this, it only allows me to carry out the method I want to properly once and then rather than return to my menu and allow me to continue using other functions on top of the one I have just used, it just keeps repeating the original function I used.
Can anybody help me out and give me some advice?
From what is given here you seem to not get another userinput. To solve it you need to get another userinput after executing the loop.
int userOption = printMenu(sc);
while(userOption != 6) {
switch(userOption) {
case 1: //function 1
break;
case 2: //function 2
break;
case 3: //function 3
break;
case 4: //function 4
break;
case 5: //function 5
break;
case 6: //
break
default: //statement asking for valid option
}
userOption = printMenu(sc);
};
As a little additon:
To save yourself the double input you could simply rewrite the loop to a do while loop:
int userOption = 0;
do {
userOption = printMenu(sc);
switch(userOption) {
case 1: //function 1
break;
case 2: //function 2
break;
case 3: //function 3
break;
case 4: //function 4
break;
case 5: //function 5
break;
case 6: //
break
default: //statement asking for valid option
}
} while(userOption != 6);
adding to your comment, you might not store the value into userOption again, but just ask for the input.
I am getting this error "variable "st" might not have been initialized" in case 5 when i try to do a string count. I tried looking online for a solution, but couldn't find someone with the same problem using string tokenizer. Please could someone tell me why this is happening?
/**
*To change this license header,choose License Headers in Project Properties.
*To change this template file,choose Tools|Templates
*and open the template in the editor.
*/
package labone;
import java.util.Scanner;
import java.util.StringTokenizer;
public class LabOne {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
Scanner userInput = new Scanner(System.in);
System.out.println("Welcome To The String Editor!");
System.out.println("");
System.out.println("Please choose what you would like to do by choosing one of the options below:");
System.out.println("1. Input String");
System.out.println("2. Print Current String");
System.out.println("");
int userOption = 0;
String stringInput = new String();
while (userOption != 9) {
userOption = userInput.nextInt();
userInput.nextLine();
switch (userOption) {
case 1:
stringInput = userInput.nextLine();
System.out.println(stringInput);
break;
case 2:
System.out.println(stringInput);
break;
case 3:
stringInput = new StringBuilder(stringInput).reverse().toString();
System.out.println(stringInput);
break;
case 4:
StringTokenizer st = new StringTokenizer(stringInput);
System.out.println(stringInput);
break;
case 5:
System.out.println("Number of tokens:" + st.countTokens());
break;
default:
;
break;
}
}
// TODO code application logic here
}
}
Please could someone tell me why this is happening?
You initialize st locally in case 4. It is not visible in case 5.
Solution: initialize it outside the switch block.
Alternatively, as spotted by Thilo, you can initialize it in case 5, as it doesn't seem to be used anywhere else.
case 5:
StringTokenizer st = new StringTokenizer(stringInput);
System.out.println("Number of tokens:" + st.countTokens());
break;
You should be careful not to initialize so many variables locally though, as you can have the very same problem happen again.
Initialize the StringTokenizer outside the switch block. Dont do it in case 4.
You have initialize st in case 4 but not in case 5, You can corrected it in this way.
StringTokenizer st;
switch (userOption) {
case 1:
stringInput = userInput.nextLine();
System.out.println(stringInput);
break;
case 2:
System.out.println(stringInput);
break;
case 3:
stringInput = new StringBuilder(stringInput).reverse().toString();
System.out.println(stringInput);
break;
case 4:
st = new StringTokenizer(stringInput);
System.out.println(stringInput);
break;
case 5:
st = new StringTokenizer(stringInput);
System.out.println("Number of tokens:" + st.countTokens());
break;
default:
;
break;
}
declare it globally, declare it outside the switch block, where you declare your all variable.
Declare it here,
int userOption = 0;
String stringInput = new String();
StringTokenizer st; //add here
so your case 4 will be
case 4: st = new StringTokenizer(stringInput);
System.out.println(stringInput);
break;
you initialized st in case 4, just take the case when user enter option 5 before 4, in that case st is not initialized right. So the solution is to initialize st either in the main first and then use it in specific switch cases, or initialize it for case 5 as well.
case 4: StringTokenizer st = new StringTokenizer(stringInput);
System.out.println(stringInput);
break;
case 5: System.out.println("Number of tokens:" + st.countTokens());
break;
In the switch case, the variable, st declared in case 4 is available in locally for that case only. It will not be available outside the case block. If you need to access it, you need to declare it outside the switch block.
I was wondering if anyone can see what is wrong with my code. It works except that the program is not acknowledging my switch statement - I searched lots of questions but as I am a novice I am clearly missing something.
import java.util.Scanner;
class Calmlr1 {
public static void main(String[]args) {
Scanner input = new Scanner(System.in);
String anotherOption = "y", operatorOpt= "a";
int no1=0, no2=0;
double result= 0;
System.out.println ("Welcome to the online calculator! Let's begin...");
while (anotherOption.equalsIgnoreCase ("y")) {
System.out.println ("Please enter your 1st number: ");
no1 = input.nextInt();
System.out.println ("Please confirm your operator:\n1 = +\n2 = - \n3 = *\n4 = /");
operatorOpt = input.next ();
System.out.println ("Please enter your 2nd number: ");
no2 = input.nextInt();
switch(no1) {
case 1:
result=no1+no2;
break;
case 2:
result=no1-no2;
break;
case 3:
result=no1*no2;
break;
case 4:
result=no1/no2;
default:
result = 0 ;
break;
}
System.out.println("Your total calculation is: "+result);
System.out.println("Would you like to do another sum? y/n ");
anotherOption=input.next();
}
}
}
You should be using switch(operatorOpt). Right now you are switching on the first number.
You also need to change:
int operatorOpt= 0;
operatorOpt = input.nextInt();
That is, if you want to keep your switch statement the same. Please also see #Daniel Imms answer for an additional bug fix.
Try adding a break at the end of case 4
case 4:
result=no1/no2;
break;
EDIT J L's answer is the main issue, but this is another problem that will break division.
Your switch should be on the operatorOpt and not on no1.
Also, you're missing a break in the case 4. So, if you want to do a division, you'll get 0 as result.
The input from the user for operatorOpt should be done with input.nextLine(). Or, if you want to keep the same switch statement, with input.nextInt().
It should be like this:
switch(operatorOpt)
{
case "+":
result=no1+no2;
break;
case "-":
result=no1-no2;
break;
case "*":
result=no1*no2;
break;
case "/":
result=no1/no2;
break;
default:
result = 0 ;
break;
}
Your switch statement should be on "operatorOpt" and not on "no1" as you suppose to check the operator and based on that you want to do the calculation. However, you must use JDK1.7 to use String in Switch statement since previous versions of JDK do not support String Switch.
Also, you should use "break" in case 4.
Your switch should be on the operatorOpt and not on no1.
You can use like this
switch(operatorOpt)
{
case "+":
result=no1+no2;
break;
case "-":
result=no1-no2;
break;
case "*":
result=no1*no2;
break;
case "/":
result=no1/no2;
break;
default:
result = 0 ;
break;
}