How To Continue With Loop If User Decides To Not Quit
Mar 29, 2015
I have this program I have to write(attached). I am having problems with what the structure will look like. The following what I have so far. The questions I have are in bold.
>get userInput of how many observations
>for(int i = 1; i <= userInput; i++)
>for(int j = 1; j <= 1; j++)
>use a switch(case) method to ask user to select an option(1,2,3)
>example, user chooses option 1:
>ask to input time
>print displacement
>ask user to stop application(Y/N)
>IF "No" is selected --------->>
How do I continue with the loop if user decides to not quit? -- And do I need to put this in each 'case'?
View Replies
ADVERTISEMENT
Nov 2, 2014
I have everything else working. My problem is that when i type "quit" to close the outer loop. It still runs the inner loop. The National Bank manager wants you to code a program that reads each clients charges to their Credit Card account and outputs the average charge per client and the overall average for the total number of clients in the Bank.
Hint: The OUTER LOOP in your program should allow the user the name of the client and end the program when the name entered is QUIT.In addition to the outer loop, you need AN INNER LOOP that will allow the user to input the clients charge to his/her account by entering one charge at a time and end inputting the charges whenever she/he enters -1 for a value. This INNER LOOP will performed the Add for all the charges entered for a client and count the number of charges entered.
After INNER LOOP ends, the program calculates an average for this student. The average is calculated by dividing the Total into the number of charges entered. The program prints the average charge for that client, adds the Total to a GrandTotal, assigns zero to the Total and counter variables before it loops back to get the grade for another client.Use DecimalFormat or NumberFormat to format the numeric output to dollar amounts.
The output of the program should something like this:
John Smith average $850.00
Maria Gonzalez average $90.67
Terry Lucas average $959.00
Mickey Mouse course average $6,050.89
National Bank client average $1,987.67
Code:
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String name = "";
int charge = 0;
int count = -1;
int total = 1;
int grandtotal = 0;
int average = 0;
[code]....
View Replies
View Related
Feb 15, 2014
The while loop keeps running although I type quit. What is wrong with this code?
import java.util.*;
public class OddEvenGame {
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
Random r = new Random();
]Code] ....
View Replies
View Related
Feb 24, 2014
import java.util.Scanner;
public class Project_5
{
public static void main (String[] args)
{
Scanner input= new Scanner (System.in);
[code]....
So I'm attempting to have this program take the users input of integers, pressing enter in between each one. If they enter a negative number it lets them know that its invalid. At the end of the program it takes all of the valid integers entered and must add them and average them. I've put the final println in there as a placeholder. If the user types in "6" then presses enter and types in "3" , the output is:
There were 3 valid numbers entered.
The sum of the valid numbers was --- and the average was ---.
There were 0 invalid numbers entered.
It then continues on allowing the user to enter more values. Here is the example output for correct code"
Enter a positive value (EOF to quit): 4
Enter a positive value (EOF to quit): 7
Enter a positive value (EOF to quit): 8
Enter a positive value (EOF to quit): 2
Enter a positive value (EOF to quit): -1
The number "-1" is invalid.
Enter a positive value (EOF to quit): 8
Enter a positive value (EOF to quit): 0
Enter a positive value (EOF to quit): -4
The number "-4" is invalid.
Enter a positive value (EOF to quit): CTRL-D
There were 6 valid numbers entered.
The sum of the valid numbers was 29 and the average was 4.83.
There were 2 invalid numbers.
View Replies
View Related
Dec 7, 2014
So, i got the following code:
@Path("{bill-id}/request")
@Path("{user-id}")
and the following pattern
myhost/c15a856/request
somehow, the container always decides to match it to @Path("{user-id}"). How do I resolve this
View Replies
View Related
Dec 30, 2014
I had a question about data structures. For the insert method of a linked list, the 'node' object is declared in the insert method. So doesn't the 'node' get destroyed as soon as the closing brace of the insert method is encountered? How do all the nodes continue to occupy memory?
View Replies
View Related
Nov 13, 2014
I was told to write a method that adds up the sequence of the formula (n/2n+1) eg. 1/3 + 2/5 + 3/7 etc. simple enough i suppose. my method is below
public static double Series(int n){
if (n==0)return 0;
else return (n/(n*2+1)) + Series(n - 1);
}
However for some reason or another it returns 0 for any number that is put in. I've written it dozens of different ways with no change and i feel like something fairly obvious is being missed on my part. I am honestly intrigued and interested as to why this is happening. i assume it has something to do with the way i put the actual formula in cause if i put anything else in like simply n the recursion would work as expected.
View Replies
View Related
Nov 18, 2014
Here's my code so far
import java.util.Scanner;
public class Program08
{
public static void main(String []args)
{
char q;
int quit = 0;
[code].....
I'm trying to get the code to repeat after the do part and before the while statement. Then press 'q' to quit the program. I had it right earlier by getting the program to repeat if you did a binary number, but I keep messing it up more and more.
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
Mar 22, 2014
I read on how to use the continue statement, but I'm failing in how to use it properly, mostly because it's not working. I'm supposed to print out what numbers are showing up and how many times for each. Plus, I have to print out 'times' instead of 'time' if there's more than one of a certain number. Right now, it's printing out all the numbers including the ones that don't get inputted.
import java.util.Scanner;
public class occurrence {
public static void main(String[] args) {
//scanner/values
Scanner input = new Scanner(System.in);
int number = 101;
[Code] ....
View Replies
View Related
Jan 21, 2015
Here is my problem to solve : Find the smallest value Write an application that finds the smallest of several integers. Assume that the first value read specifies the number of values to input from the user.
I do not even know where to begin other than creating a scanner for the user input. But how do I take that user inputed number to create the loop?
View Replies
View Related
Apr 15, 2015
public static void getInput(int [] student, int []test1,int []test2,int []test3,
int [] test4, int [] test5) {
int stuId = 0;
int t1 = 0;
int t2 = 0;
int t3 = 0;
int t4 = 0;
int t5 = 0;
int h = 0;
char answer = '@';
[Code] ....
View Replies
View Related
Feb 12, 2015
I need to fix a program in which the user must get a three random numbers in a row. They have five chances. This is what i have so far:
public static void main(String[] args)throws IOException {
// TODO code application logic here
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int num1=(int)(Math.random()*(9-0));
int num2=(int)(Math.random()*(9-0));
int num3=(int)(Math.random()*(9-0));
[Code] ....
View Replies
View Related
Mar 3, 2014
I am having a problem with code that I've written, the problem occurs when I try to prompt for user input at the end of the first loop. What happens is the println fires but the prompt never does and it exits the loop.
Java Code:
import java.util.Scanner;
public class Lab3test
{
public static void main( String[] arguments )
{
//Variable declaration
[Code] ....
View Replies
View Related
Jul 2, 2014
Ask user to enter a name alphabet by alphabet, and print d whole word as output,,,,,, use while loop?Since i am new to JAVA..i have no clue on how to begin?
View Replies
View Related
Sep 10, 2014
I am still relatively new to java and am working on a lab program. I have already met the requirements and am trying to spice my code up with a loop that prompts the user to say if the program should run again. This is what I have so far:
package lab2;
import java.util.Scanner;
public class OrderedPairTest{
public static void main(String[] args){
boolean retry = true;
while(retry == true){
[code]....
However, it is giving me an error after the user inputs "y" in for the answer to run again. What did I do wrong?
Error code is: Exception in thread "main" java.util.NoSuchElementException: No line found at java.util.Scanner.nextLine(Unknown Source) at lab2.OrderedPairTest.main(OrderedPairTest.java:11)
View Replies
View Related
Nov 11, 2014
I'm writing a classic airplane seating program. The program runs fine except for two things:
1) I want the user to exit the program when 0 is inputted (program also quits when all seats are reserved - this works fine already). Now the program quits when user hits blank line with enter. How to fix this and make 0 the exit key?
2) I would like for there to be a line below the seating chart saying that there are XX number of seats available. When user makes reservations, the line should update every time under the seating chart saying that there are such and such number of seats available. How to implement this?
import java.util.*;
public class OmaAirplane {
public static void main(String[] args) {
System.out.println("Welcome to the seat reservation system!");
char[][] seats = new char [8][6];
ArrayList<String> reservedSeats = new ArrayList<>();
for (int i=0;i<8;i++){
[code]....
View Replies
View Related
Jun 28, 2014
I'm trying to learn Java and my current project is to write a short program to determine the factorial of a number entered by the user. I haven't looked. There may be a method that will do it, but I want to use a for loop specifically.
What I have compiles just fine. I'm actually pretty thrilled just with that. Here is what I have:
class factorial {
public static void main( String[] args) {
Scanner scan = new Scanner(System.in );
int num;
int product = 1;
[Code] ....
View Replies
View Related
Nov 11, 2014
I'm writing a classic airplane seating program. The program runs fine except for two things:
1) I want the user to exit the program when 0 is inputted (program also quits when all seats are reserved - this works fine already). Now the program quits when user hits blank line with enter. How to fix this and make 0 the exit key?
2) I would like for there to be a line below the seating chart saying that there are XX number of seats available. When user makes reservations, the line should update every time under the seating chart saying that there are such and such number of seats available. Any pointers on how to implement this?
import java.util.*;
public class OmaAirplane {
public static void main(String[] args) {
System.out.println("Welcome to the seat reservation system!");
char[][] seats = new char [8][6];
ArrayList<String> reservedSeats = new ArrayList<>();
[Code] .....
View Replies
View Related
Nov 17, 2014
I'm trying to do a user validation loop that runs until the user enters a valid binary, q, or Q. I think the rest of my program should work as intended, but here is the areas of concern:
public static boolean isBinary(String num) //Check if the entry is binary
{
for(int i = 0; i < num.length(); i++) {
if(num.charAt(i) != 0 || num.charAt(i) != 1){
return false;
[Code] .....
The program runs without errors otherwise.
View Replies
View Related
Feb 7, 2015
Write a program called RomanNumeralHelper that allows a user to enter a roman numeral and then outputs the integer number value. Use a Scanner to accept command line input from the user and continually ask for a roman numeral until the user enters Q (or q) to stop. Your solution should NOT use a switch statement.
Here is sample input / output:
Enter a roman numeral [Q | q to quit]: III
>> 3
Enter a roman numeral [Q | q to quit]: IV
>> 4
Enter a roman numeral [Q | q to quit]: V
>> 5
Enter a roman numeral [Q | q to quit]: Q
Good Bye!
This is what I have so far in my code, but I cant get what the user inputs when I want it to output the number.
import java.util.Scanner;
public class RomanNumber4
{
public static void main(String[] args) {
// obtain input from command window
Scanner input = new Scanner(System.in);
[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
Apr 9, 2014
Write a java program that will ask a user to input grades until the user inputs the sentinel value -1. The program should test each input to determine whether 0<=grade<=100. If the grade entered is within this range, the program should print "Grade accepted;" if not, the program should print "Invalid input".
View Replies
View Related
Oct 8, 2014
Basically I need to make a program prompts the user for an integer, check to make sure the length entered by the user is a power of 2 and at least 2. Then I need to convert from base e to base 2 then I need to print the tick marks of a ruler based of the value of the length.
I got the whole converting thing working and the check for a power of 2, that wasn't an issue because it didn't require any recursion. I just don't know how to print out the proper tick mark values.
The way it is supposed to work is that it needs to look like this. Say the user enters 8;
012131210
012345678
Or if the user enters 16;
01213121412131210
01234567890123456
The bottom row is pretty much just the index value, that I print fine.
The top row though is supposed to be the length of the ticks on a ruler, with the middle most value being the value of the conversion from base e to base 2 from above. I can get that printed and what I get just looks like this.
For 8;
000030000
012345678
For 16;
00000000400000000
01234567890123456
As you can see I can get the middle value and the index values below but I don't know how to use recursion to get the right numbers.
Here's my current code.
import java.util.*;
public class TickMarks {
public static void main (String args[]){
Scanner input = new Scanner(System.in);
boolean looping = true;
while(looping == true){
System.out.print("Please enter a length: ");
[Code]...
Now the methods isPowerOfTwo() and printLength() both work fine. However, I'm not sure how to get the left and right side working.
My thoughts were to split the length in half and get a left and right side. I gave both of them arrays so I can keep track of the values. Now as you've seen above I get all zeros and I know it's because that's the default value in an array, I just don't know how to get the proper values.
View Replies
View Related
Nov 4, 2014
I have been struggling with this program for weeks. This program is supposed to take a user's inputted odd number and then print out all prime numbers lower than that number.
public class PrimeNumber
{
Scanner scan = new Scanner(System.in);
int userNum;
String neg;
public void getUserNum()
[code]...
View Replies
View Related