Switch Statement Inside While Loop
May 5, 2015
cannot break from while loop. Whenever I am trying to exit from startCustomerManagement-> backEnd() -> mainScreen()..It gets stuck between mainScreen and backEnd screen. However I can exit from backEnd()->startCustomerManagement() screen
/*
* 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 javaapplication19;
import java.io.BufferedReader;
[code]...
View Replies
ADVERTISEMENT
Nov 18, 2014
So I want to make a simple Java that ask the user to pick a powers and it has two options.If the user picks magic then execute the first if statement then ask the user again which type of magic the user wants.I can't make it work it keeps printing the else statement. Why is that?
import java.util.Scanner;
public class Variable {
static Scanner zcan = new Scanner(System.in);
public static void main(String[] args)
[code]....
View Replies
View Related
Oct 10, 2014
Basically the problem is I've been trying to loop my switch (if this is even possible) until the user enters a correct number between 1 and 4. But for some reason when the user enters a correct number (1,2,3,4) the loop runs again. Even when the user enters a wrong number the switch ignores the default and repeats what is in the do.
Am I just trying to loop something that cannot be looped or have I gone?
import java.util.Scanner;
class mammals {
int age;
int death;
String name;
[Code].....
View Replies
View Related
Sep 23, 2014
I am having a problem with looping a while loop that contains a switch statement. Here is my code:
package hw4jenningsd;
import javax.swing.*;
public class HW4JenningsD {
[Code].....
View Replies
View Related
Dec 8, 2014
I am trying to add a while loop into my switch statement. If you run the program, it will ask to enter the class grade (9,10,11, or 12). If you insert 5, it will say to try again. But, if you enter a wrong number twice, it will continue on to the next part of the program, which is asking how many students are in the class. Therefore, I believe a while loop would work, but it is not working at all. The program still runs, just doesn't fix the error. The program is below:
import java.util.Scanner;
public class stephProject {
public static void main(String[] args) {
//call method
welcomeMessage(); //method 1 of 3
[Code] ....
View Replies
View Related
Sep 28, 2014
I'm trying to create a switch statement inside of a counter controlled (while) loop that asks for an input of...
"How many characters would you like to convert?"
Then you type in a number > 0, and then it should convert letters into the "1337" equivalent.
This is an example on what it should do.
How many characters would you like to convert? 5
Enter character #1 to convert: !
!-
Enter character #2 to convert: $
$-
Enter character #3 to convert: #
#-
Enter character #4 to convert: *
*-
Enter character #5 to convert: ,
, -
Whenever i try to run the program, i only get the- How many characters would you like to convert- i input 5 but then nothing else prints..
My codes..
System.out.print ( "How many characters would you like to convert?: " );
int convertCounter = 1;
char ch;
ch = input.next().charAt(0);
while (convertCounter > 0)
switch ( ch ){
[Code] ....
View Replies
View Related
Dec 12, 2014
I made this calculator in C++ and it worked wonderful so I decided to make it in java. When I run the program it gives me no errors but my if statements inside my while loop don't work
import java.util.Scanner;
public class ohmlaw {
public static void main(String args[]) {
float current;
float resistance;
float voltage;
String calchoice = new String();
Scanner cc = new Scanner(System.in);
[code]....
View Replies
View Related
Jun 19, 2014
So from what iv learnt in Java and programming in general is that using a case statement is far more efficient that using multiple IF statements. I have an multiple IF statements contained within a method of my program, and would like to instead use a case statement.
public String checkPasswordStrength(String passw) {
int strengthCount=0;
String strengthWord = "";
String[] partialRegexChecks = { ".*[a-z]+.*", // lower
".*[A-Z]+.*", // upper
".*[d]+.*", // digits
".*[@#$%!]+.*" // symbols
[code].....
View Replies
View Related
Mar 2, 2014
I have create as short example which contains two classes Cat and Dog followed by a switch statement in the main method.
Class Cat
public class Cat {
private String name;
//Setter
public void setName(String pName)
{name = pName;}
//Getter
public String getName()
{return name;}
// Constructor
public Cat(String catName)
{name = catName;}
[code]....
My Issue is that after creating the objects within the switch statement, is there a way to return the objects back to the main method once created within the switch ?
View Replies
View Related
Jul 10, 2014
can give me a sample program which solves with switch case statement with nested if inside?
View Replies
View Related
Mar 30, 2014
This is what I'm trying to do;
Java Code:
import java.util.Scanner;
class test{
public static void main ( String[] args ) {
Scanner scan = new Scanner ( System.in );
int choice = scan.nextInt();
[Code] ....
How is this doable?
View Replies
View Related
Apr 10, 2015
this is what code i have so far i want to try and get the computer to output a String type result rather than a number and i wonder if there is a way to output the score after the user is done with the game. I couldn't really see if there was a way to incorporate a switch statement.
import java.util.Scanner;
import java.util.Random;
public class RPSLS {
public static void main(String[] args) {
[code]...
View Replies
View Related
Mar 1, 2015
If I have an integer variable like int a=9 then in the switch case If i write
switch(a) {
case 4+a: System.out.println("hii");
}
Then why is this statement a compile-time error that variables cannot be used inside a case statement why does the compiler not subtitutes the values in place of the variables.
So basically what problem it creates for which the language developers did not include it as a proper syntax,is there any reason behimd this because of jump table?
View Replies
View Related
Oct 4, 2014
Write a program to calculate sales and visually display. An online Cowboy retailer sells five products whose retail prices are as follows:
Product 1: Cowboy Boots, $74.99
Product 2: Wranglers, $25.99
Product 3: Cowboy Hats, $24.95
Product 4: Chaps, $ 34.89
Product 5: Spurs, $12.50
Write an application that reads the quantity for each product until user has completed their order. Your program should use switch, if, for, while and do while statements to read, calculate and display the total retail value of all pro ducts sold for each user transaction. The user should be able to start a new transaction after the first transaction is completed. The user can make no more than 3 total orders
public class Merchandise {
private int bootsQTY;
private int wranglersQTY;
private int hatsQTY;
private int chapsQTY;
[Code] ....
When I run my program all of my quantities and the total are 0, I have been stuck for a while trying to figure out how to get values assigned to them...
View Replies
View Related
Sep 6, 2014
What I want to happen is to add 2+ contacts by entering "a" from the menu. Then I want to enter "c" and have it display those contacts. But it doesn't seem to remember that I changed the arrayList masterList.
Java Code:
public class ContactListApp2 {
public static void main(String[] args) {
boolean quit = false;
while (quit == false) {
System.out.println("Main Menu:");
[Code] ....
View Replies
View Related
May 19, 2014
I am stuck with two switch statements that I feel I am close to achieving. My first switch statement is to generate random mathematical operators for the math questions in the game (generated randomly) My second switch statement is to determines which action the system will take upon the correct or incorrect answer. For the first switch statement I have a variable called num. Num is a random integer. I have an operator mapped to each outcome of num. In my second switch statement I am trying to use the variable answer as a variable in the first case but do not know how.
package pkgnew;
import java.util.Scanner;
import java.util.Random;
public class New {
public static void main(String args[]) {
//Declare and construct variables
[Code] ....
View Replies
View Related
Sep 10, 2014
In switch case statement string expression whether it will work or not ???
View Replies
View Related
Feb 21, 2015
I'm using jsoup to parse a calendar page.
I want to use a switch or if/else statement to filter out the data I don't need for the app. This is by no means anything more than a draft because java is easier to work with than android.
Element table = doc.getElementById("launch_calendar");
Iterator<Element> iterate = table.select("td").iterator();
if(iterate.hasNext()){
for(iterate.hasNext();;){
counter++;
[Code] ....
View Replies
View Related
Feb 23, 2014
I am trying to write out a program that takes numerical input from the user and converts it to a date using the English month name. I am experimenting with the method of a "switch" statement without using the "break" clause. However, I seem to be missing something, as Eclipse is telling me I have a syntax error with my block. My curly braces seem properly placed. Also, I made sure to follow guidelines to make my code fit on the screen and remain easy to read.
import acm.program.*;
public class MethodsThatReturnNonNumericValues extends ConsoleProgram {
public void run() {
int month=readInt("Enter month number");
int day=readInt("Enter day");
int year=readInt("Enter year");
[Code] ....
View Replies
View Related
Nov 4, 2014
// GradeBook class uses switch statement to count A, B, C, D and F grades.
import java.util.Scanner;
public class GradeBook
{
private String courseName;
private int total;
private int gradeCounter;
private int aCount;
[Code] .....
Then eclipse told me that:
"Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at chapter5_control_statements_II.GradeBook.inputGrades(GradeBook.java:52)
at chapter5_control_statements_II.GradeBookTest.main(GradeBookTest.java:11)"
View Replies
View Related
Nov 10, 2014
//Use a Switch statement to determine outcomes for letter grades
switch (Letter)
{
case 'A': outFile.println("
Good Work! You have met the pre-req for CIS 220.");
break;
case 'B': outFile.println("
[Code] ....
is not printing the information on the next line in my output file.
View Replies
View Related
Dec 3, 2014
We're told to make a cash register program for 5 products using Java. It loops until "-1" is entered on the "Continue?" input dialog box.There should be a:
1. Main Menu - where the user will pick what to buy (one product at a time) like the sample code below and prompt the user if he will buy another product or not. If yes, the program will go back to the main menu to buy another product and if not, continue to the program. And it looks something like this: LEvFOwQ.png
2. Receipt - at the end of every transaction.
3. Daily Sales Report where it shows all receipts then grand total like:
Transaction 1
-subtotal
Transaction 2
-subtotal
and so on then,
GRAND TOTAL
4. Inventory Report where it shows the Items Available before the Selling, Items Sold, and Items Available after Selling in table format.
Below is the code for a simple cash register for only one product that I made but I need to upgrade it for it to work for 5 products. That code will serve as the basis for this program.make the switch or if-else statement for the main menu first and if I got it right I try doing the rest.
int userQty1 = Integer.parseInt(JOptionPane.showInputDialog(
"Product ID Product Name Product Quantity Product Price"
+prodID1 + " " +prodName1 + " " + prodQty1 + " " +prodPrice1 "
"
+ and so on until prod5));
[code].....
View Replies
View Related
Jan 27, 2015
I am trying to print a loop inside an array of a greater size than the loop itself. Like for example I have an array of size 7 but it has only 3 elements.
int[] array=new int[7]
array[0]=2;
array[1]=3;
array[2]=4;
now what I want to do is print these three numbers in a loop so that my array[3]=2;array[4]=3;array[5]=4 ...... till the last one. Also the array could be any size and not just 7.
View Replies
View Related
Feb 25, 2014
Here's the code: it's while loop inside a for loop to determine the proper length of a variable:
for (int i = 0; i < num; i++) {
horse[i]=new thoroughbred();
boolean propernamelength = false;
while (propernamelength==false){
String name = entry.getUserInput("Enter the name of horse "
[code]....
I was just wondering what was going on here -- I've initialized the variable, so why do I get this message? (actually the carat was under the variable name inside the parentheses.
View Replies
View Related
Oct 5, 2014
Write a program using switch statement to allow users to choose an item of a menu. For example, the menu looks like the following.
File menu
1. Open
2. Close
3. Save
4. Exit
Enter your choice:
If you type 1, then your program should print out "Open is selected" (double quotes not included).
Notice:
(1) Using Scanner to get user choice;
(2) If the number user input is not in {1,2,3,4}, your program should let user know that.
Here's what I have:
public static void main (String[] args) {
int user = 1
switch (1) {
case value: 1
System.out.println ("Open is selected");
[Code] .....
View Replies
View Related
Nov 1, 2014
Alright so I wrote a switch statement that decides what range to print based on the letter grade input.
import java.util.Scanner;
public class SwitchPractice
{
public static void main(String [] args)
{
Scanner scan = new Scanner(System.in);
[code]...
It works fine, but once it the user enters a letter grade and then displays the value, it does not prompt the user for another letter grade so that it can perform the output again. Also if I wanted to display an error message to the user if they enter an invalid letter grade how would I do that. I tried using a while loop and if statement in the switch statement but that didn't work.
View Replies
View Related