Way To Incorporate Switch Statement

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


ADVERTISEMENT

How To Turn If Statement Into A Case / Switch Statement

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

Using Switch Statement Inside If Statement

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

Looping Through While Loop With Switch Statement?

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

Can Insert While Loop In Switch Statement?

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

Can't Get Values Found In Switch Statement

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

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 View Related

Modifying ArrayList With Switch Statement

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

Using Variables In Switch Statement Case Clauses

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

Counter Controlled Loop With A Switch Statement

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

Switch Case Statement - String Expression

Sep 10, 2014

In switch case statement string expression whether it will work or not ???

View Replies View Related

Use Switch Or If / Else Statement To Filter Out Data Not Required For App

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

Method That Returns Non-Numeric Values - Switch Statement

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

Simple GradeBook - Switch Statement To Count A B C D And F Grades

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

Switch Statement - Determine Outcomes For Letter Grades

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

Cash Register Program For 5 Products Using Switch Or If Else Statement In Java

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

Writing A Program Using Switch Statement - Allow Users To Choose Item Of A Menu

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

Switch Statement That Decides What Range To Print Based On Letter Grade Input

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

Electricity Bill Project - How To Incorporate Rate Per Month And Three Rate Zones

Jan 30, 2014

I am not sure how to incorporate the rate per month and the three rate zones.

Problem: Electricity billing charges vary with the season and amount used.

Southwest Power and Light provides electricity at the basic rate of $ 0.10 per kilowatt for the months December through February, 0.12 per kilowatt for the months of March through May, and 0.15 per kilowatt for the remaining months.

Customer billing is divided into three rate zones: 0-350 kilowatts, 351 – 500 kilowatts, and above 500 kilowatts. Usage up to 350 kilowatts is billed at the basic rate. Usage between 350 and 500 kilowatts is billed at 10% above the basic rate. Above 500, billing is 25% above the basic rate. These surcharges apply only to the usage falling within the respective rate zone.

View Replies View Related

How To Switch Sides

Oct 21, 2014

I had to make a chart where it took miles 1-10 and convert it to kilometers and then on the other side it needs to show 25-65 (in 5's) kilometers to miles and I dont know how to switch the sides...

import java.util.Scanner;
public class MilesAndKilos {
public static void main(String[] args) {
int miles;
double kilometers=1.60934;
System.out.println(" Miles Kilometers Kilometers Miles");
for(miles=1;miles<10;miles++){
kilometers = miles * 1.60934;
System.out.printf(" %2d %8.3f |
", miles,kilometers);
}
}//end of main method
}

View Replies View Related

Nested Switch Case

Oct 6, 2014

write a program using nested switch case to show the different shops in a shopping mall.

View Replies View Related

Switch Inside A Do While Loop?

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

Modify Hub Code To Switch

May 26, 2014

I have a code for a Hub and I wanted to modify it to a Switch.. See the code below:

--- Update ---

package module.Hub;

import framework.Port;
import module.ModuleUI;
import java.lang.String;
import framework.Packet;

[code]....

View Replies View Related

Make A Switch With A String

Jan 9, 2015

I needed to make a switch with a string ,but eclipse told met there was an error and that the fix was to switch to 1.7 compliant. I did so and now when i run my program errors come back.

Here are the errors :

Exception in thread "main" java.lang.UnsupportedClassVersionerror: start : Unsupported major.minor version 51.0
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:637)
at java.lang.ClassLoader.defineClass(ClassLoader.java:621)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
at java.net.URLClassLoader.access$000(URLClassLoader.java:58)

[code]....

View Replies View Related

Replace Four Loops Through Switch Box?

Sep 11, 2014

I will like replace this code:

// boucle pour les fractions aleatoires
for (i=0; i<2; i++) {

// Creation des fractions aleatoires
Frac first = new Frac(r,5);
Frac second = new Frac(r,5);

[Code] .....

But when I use a switch case I do not have the same result as my four loops.

View Replies View Related

If Else / Switch Not Outputting Correctly

Jan 27, 2014

Trying to learn switch statements. Can't figure out what I am doing wrong.

java Code:

import java.util.Scanner;
public class Ok1 {
public static void main(String args[]) {
Scanner input = new Scanner(System.in);

[code]...
When I run, it makes me enter letter = null and it wont output correctly.

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved