The Nested While Loop Will Not Execute

May 21, 2014

//Main method
public static void main(String args[])throws IOException{
boolean runProgram = true;
Scanner keyboard = new Scanner(System.in);
//runs program while runProgram is true
while (runProgram){

[code].....

View Replies


ADVERTISEMENT

Nested If Statement - Execute If True

Feb 16, 2015

I have a problem with an if statement I've done. I'm not sure how to fix my problem. I'll post the code of the if statement and the output.

if (box = true &&(shippingSwitch ==1))

if (ounces < .5)
{
additionalPrice = ((ounces % .5)/(0.0625))*1.25;
shippingPrice = 15.75 + additionalPrice;//((weight / 8)+ (weight % 8));

[Code] ....

Here's the output I get using println to test it.

Please enter 1 if your package is a letter and 2 if your package is a box
2
Please enter weight in pounds (Example: 1.2):
1.2
Please enter your shipping service option:
Press 1 for Next Day Priority:
Press 2 for Next Day Standard:
Press 3 for Two Day Shipping:
1
7.00

If I comment out the else statement on the last line of code I get the following output:

Please enter 1 if your package is a letter and 2 if your package is a box
2
Please enter weight in pounds (Example: 1.2):
1.2
Please enter your shipping service option:
Press 1 for Next Day Priority:
Press 2 for Next Day Standard:
Press 3 for Two Day Shipping:
1
13.75

What I want to happen is the code under the first comment to to execute if true, or go to the code under the next comments and execute if that statement is true and if that is not true I want the last block of code to execute underneath the last comments.

View Replies View Related

How To Time While Loop To Execute Every 3 Seconds

Jul 11, 2014

How can I time my while loop to executeevery 3 seconds?

Here is my code:

class Info{
public String name;
public String version;
public String arch;
double CPUSpeed;
};
Info info = new Info();
Info[] queue = new Info[100];

[Code]...

I seem to have done it right, but it doesn't work... What is wrong?

View Replies View Related

Nested Do While Loop

Apr 5, 2014

just trying to learn it on my spare time and I'm doing do-while loops

public class help {
public static void main (String args[])
throws java.io.IOException {
char choice, ignore;
do{
System.out.println ("Choose one:");
System.out.println("1. if");
System.out.println("2.switch");
 
[code]....

It makes no difference in the program wither i delete this block or keep it..how while (choice < '1'| choice >'2'); translates? I would assume it would be while (choice >= '1'| choice =<'2');?

View Replies View Related

How To Use Nested For Loop

Dec 17, 2013

Question: you are only allowed to use numbers from 1-6. Write a program to find all the permutations when three numbers are multiplied together to give a result 8. one number cannot occur twice in any permutation.

public class number4
{ public static void main(String[] args)
{
for(int a=1; a<=4; a++)
{
for(int b=1; b<=4; b++)
{

[Code]...

my program also prints out 2 2 2. but i'm not allowed to do that. how can I stop it from printing 2 2 2 ?

View Replies View Related

Can For Loop Be Nested Inside If?

Mar 6, 2015

Here's what "Why doesn't this work?" question. It concerns a small method which is part of a card game.

I'm trying to check a condition from a section of an array, without a predetermined number of times any given call to this method will require a check. How much of the array I'm checking could vary greatly.

Is it at all possible to nest a for loop yielding a variable number of boolean && conditions inside an if? Am I just missing the right bracketing or is this nesting (if that's the word) even possible in the language?

To clarify, below is broken code. Compiler isn't letting me accomplish this goal as I envision it.

public boolean isFlanking() {
boolean f;
int reach = Math.abs(selectorX - targetX);
if(rival.getDeck()[selectorX].getPile().isEmpty() == true &&

[Code] ....

View Replies View Related

How To Determine Value After Nested For Loop

Oct 12, 2014

int count = 0;
for (int x = 0; x < 3; x++)
for (int y = x; y < 3; y++)
for (int z = y; z < 3; z++)count++;

What is the value of count after the code segment is executed?

The answer is 9, but I don't really understand how this answer was derived.

View Replies View Related

Reading A Nested For Loop

Nov 5, 2014

for (int i = 0; i < 4; i++)
{
for (int k = 0; k < i; k++)
{
System.out.print(" ");
}

for (int j = 4; j > i; j--)
{
System.out.print("$");
}
System.out.println();
}

I was told that the answer when this code segment is printed look's like this:
$$$$
$$$
$$
$

Here's what I did:Looking at the outer for loop, (i) 0 < 4 so I went into the first inner nested loop. (k) 0 is not less than (i) 0 so I went to the 2nd inner nested loop and found that it worked, and I was able to repeat this loop 3 more times and then I exited the loop and printed the line out (giving me the first line of four $). I then went back to the outer for loop, increased i by 1 and (i) 1 < 4 so I went to the 1st inner nested loop. I used the 0 for the k first and (k) 0 < 1 so I printed out a space (now here's where I get lost) I then incremented k by 1, so k = 1, but 1 is not less than 1 (i) and so I moved on to the next nested for loop. So when this line is printed, I'll only have one space when there should be 4.

View Replies View Related

How To Determine Value After Nested For Loop

Oct 12, 2014

int count = 0;
for (int x = 0; x < 3; x++)
for (int y = x; y < 3; y++)
for (int z = y; z < 3; z++)
count++;

What is the value of count after the code segment is executed?

The answer is 9, but I don't really understand how this answer was derived.

View Replies View Related

Big-O Notation Of A Nested For And While Loop

May 20, 2014

I'm having trouble figuring out the Big-O notation for this loop. For now I only have it in pseudocode. I hope you understand it anyway

for i = 1 to n do
j = 1
while j <= n/2 do
print "*"
j = j + 1
end while
end for

View Replies View Related

Nested For Loop - Pattern Printing

Feb 13, 2014

I don't know how to write a program based on for loop like pattern printing.etc

View Replies View Related

Java Enhanced For Loop Nested?

Sep 27, 2014

how does this nested enhanced for loop execute? I dont know how to indent the code.

for (String exam : exams)
for (String level : levels)
for (String grade : grades)
System.out.println(exam+":"+level+":"+grade);

Note:

exams,levels,grades are arraylist
exams contain Java,Oracle
levels contain Basic,Advanced
grades contain pass,fail

View Replies View Related

Printing Pattern Using Nested Loop

Jan 19, 2015

I built a java code to print the following pattern using nested loop:

1 2 3 4 5 6 7 8 7 6 5 4 3 2 1
1 2 3 4 5 6 7 6 5 4 3 2 1
1 2 3 4 5 6 5 4 3 2 1
1 2 3 4 5 4 3 2 1
1 2 3 4 3 2 1
1 2 3 2 1
1 2 1
1

And here is my code

public static void main(String[] args) {
for(int i=1; i<=8;i++)
{
for(int k=1;k<=i;k++)
{
System.out.print(" ");

[Code] .....

which gives the partially true output:

123456781
123456721
123456321
123454321
123454321
123654321
127654321
187654321

I couldn't figure out how to place the spaces.I have made some changes on the code, but it didn't work.

View Replies View Related

Nested For Loop Causing Program To Crash

Jan 23, 2015

Implementation of the nested for loop. I thought it was going to go smoothly but apparently not.Purpose: This program is meant to print out all the prime numbers up to a certain range using the algorithm Sieve of Eratosthenes.

Update output: Works as expected.

Java Code:

import java.util.Scanner;
import java.util.ArrayList;
public class PrimeFactors
{
static ArrayList<Double> primeList = new ArrayList<>();
static double range = 0;
static double maxPrime = 0;
static double primeTester = 0;

[code]....

View Replies View Related

Java - Going Through Value In Multiple ArrayList (Nested For Loop)

Jan 24, 2014

I just started playing around with Java for awhile, but got caught up in a problem when using ArrayList.

CinemaAppMain
public class CinemaAppMain {
public static void main(String[] args) {
new CinemaApp().start();

[Code]....

I am trying to get the movie name and theatre title from their ArrayList, and I know I need to go through the movie list & theatre list to find their name & title, and compare it with the user input and print out the result.

I use for(int i=0;i<movies.size();i++) to go through the the movie list, and I tried using for(int i=0;i<theatres.size();i++) to go through the theatre list. But when I printing multiple time with different user input value, it will show me strange result (screening have same movie name & theatre name, the else if statement is printed below the user input, user input is printed more than once after the first time).

Edit: I know its wrong for me to use nested for loop,if not, another solution is needed to get the item inside both of the list. (getting the "title" from movie list & "name" from theatre list)

View Replies View Related

Write Nested Loop For Pyramid Pattern

Sep 26, 2014

Write a nested loop for a pyramid pattern. Well here's an ugly description of what it should look like:

1
1 2 1
1 2 4 2 1
1 2 4 8 4 2 1
1 2 4 8 16 8 4 2 1
1 2 4 8 16 32 16 8 4 2 1
1 2 4 8 16 32 64 32 16 8 4 2 1
1 2 4 8 16 32 128 64 32 16 8 4 2 1

I managed to make the pyramid using addition. However, how to turn it into multiplication x 2.

import java.util.*;

public class NumbersPyramid
{
public static void main(String[] args) {
int odd = 1;
int numSpaces = 7;

[code]....

View Replies View Related

Nested For Loop - Print Out Table Of Angles (sin / Cos And Tan)

Oct 1, 2014

I am working on my java program it prints out a table of angles and the sin, cos, and tan. For extra credit he wanted us to use nest loops to create a space after every five lines. I have come real close to getting this to work but have a problem with the very end of the table. The table needs to stop at 180...

public static void printTable(double angle,double sin,double cos,double tan) { // Method to create a table for the values
for (double j = 0; j <= 180;) {
for (double i = 1; i <= 5; i++) {//loop to print five lines of code
angle = Math.toRadians(j);

[Code] .....

Here is the end of the output of the table:

175.00.0872-0.9962-0.0875
176.00.0698-0.9976-0.0699
177.00.0523-0.9986-0.0524
178.00.0349-0.9994-0.0349
179.00.0175-0.9998-0.0175
 
180.00.0-1.00.0
181.0-0.0175-0.99980.0175
182.0-0.0349-0.99940.0349
183.0-0.0523-0.99860.0524
184.0-0.0698-0.99760.0699

View Replies View Related

Making Custom Checkerboard With Nested Loop

Feb 7, 2014

I'm trying to make a custom checkboard with given user input, in the form of (# of rows, # of columns, size of each square, filler character).

So the input 2 3 5 * would look like

*****.........*****
*****.........*****
*****.........*****
*****.........*****
*****.........*****
.........*****
.........*****
.........*****
.........*****
.........*****

I've made my loop, but I am unable to get more then a 1 1 1 checkerboard properly. I am stuck on how to divide the filler characters to make the proper square size. As of now they are all one lined.

import java.util.*;
 public class Checker{
 public static void main(String[] args) {
int col, row, size; char filler;
System.out.println("Please enter 3 numbers and a character."); //output

[Code] ..........

View Replies View Related

Nested For Loop To Capture Structure Of The Figure

Jan 26, 2014

I am having problem with some problems like this nested for-loop for instance ....

-----1-----
----333----
---55555---
--7777777--
-999999999-

Now I have to write a method called printDesign that produces the following output but i am not even entirely sure how to start it out now of course i know how to make a for-loop but and i guess if i was to do something for this i would do this .....

Java Code:

for(int*j=0;j<N-k;j++){
System.out.print("-");*
for(int*j=0;j<i;j++){
System.out.print(i);*
for(int*j=0;j<N-k;j++){
System.out.print("-");*
System.out.println();*
}
}
} mh_sh_highlight_all('java');

View Replies View Related

Java Program Using Nested Loop To Compute / Display Average Of Test Results For Each Experiment

Apr 2, 2015

Four experiments are performed, each consisting of six tests. The number of test results for each experiment is entered by the user. Write a Java program using a nested loop to compute and display the average of the test results for each experiment.

You can run the program by entering the following test results:

Experiment 1 results:23.231.516.927.525.428.6
Experiment 2 results:34.845.227.936.833.439.4
Experiment 3 results:19.416.810.220.818.913.4
Experiment 4 results:36.939.549.245.142.750.6

View Replies View Related

How To Execute Jar File

Apr 3, 2007

How can I execute a jar file ?

how can I make such file ?

Will it be executed as an exe file in windows ?

View Replies View Related

If Else - How To Not Execute Both Statements

Oct 4, 2014

I am making a program, where the user answers 3 questions and then I add the number of correct answers in to the (int) numberofcorrect variable, then I want to print the results, and no matter how many correct, the program executes first the correct if statement, then the else statement.

Eg: I have 2 correct it will print:

"Grade B, 2 of 3 "

"You failed the test"

Why does it do that? How can I change my code so the else statement dosent print if one of the if statments is allready printed?I want to know how to not execute the else statement, if one of the if statements have allready been executed.Below is my current code for this problem:

if (numberofcorrect == 3){
System.out.println("Grade A, full score");}
if (numberofcorrect == 2){
System.out.println("Grade B, 2 of 3 ");}
if (numberofcorrect ==1) {
System.out.println("Grade C, 1 of 3");}
else {
System.out.println("You failed the test"); }

View Replies View Related

JSP :: Execute Code After Every 10 Seconds

Feb 25, 2014

I want to execute my jsp code to compare two dates after every 10 seconds if user enter date and current sys date are equal it will send the mail to the user automatically. Below is my jsp code

this if condition i want to check the date after every 10 seconds and when two dates are equal it will send the mail using below mail code

if(ExpcReDt.compareTo(dateStr)>0) {
out.println("Expected return date is greater than current date");
}
else if(ExpcReDt.compareTo(dateStr)==0)

[code]....

View Replies View Related

Program Compiled But Not Able To Execute

Apr 23, 2014

I am able to compile the below program but not able to execute.

public class String {
public static void main(String[] agrs){
System.out.println("Hello String");
}
}

Output Error:

Class String does not have a main method

View Replies View Related

Compiles But Won't Execute - JCreator

Oct 18, 2014

I was typing up this program and when I compile it, there aren't any errors, but it won't advance past the second conditional.

Here's the code:

import static java.lang.System.*;
import javax.swing.JOptionPane;
import java.util.*;
public class area51
{
public static void main(String args[])

[Code] ....

The first two answers are 41 and right.

View Replies View Related

How Compiler Picks What Method To Execute

Feb 20, 2014

public class Main {
private static void foo(Integer a) {
System.out.println("Integer");
}
private static void foo(long a) {
System.out.println("long");

[Code] ....

This code prints long. Why is that? How did compiler decided that it likes long version of foo() method the most. If I had to guess I'd pick int... or possibly Integer as they are most similiar to what was passed. But why long?

View Replies View Related







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