Infinite For Loop

Oct 13, 2014

I wrote a program that asks the user for their letter code to tell them where they are supposed to go. I was then told to comment out asking the user and to use a loop to print each possible destination. I decided to use a for loop and am stuck in an infinite loop.

//Sam Kluender
//MajorDestination.java
import java.util.Scanner;
public class MajorDestination
{
public static void main(String[] args)
{
char major;
int countLang, countGifford;
Scanner reader = new Scanner(System.in);

[code]....

View Replies


ADVERTISEMENT

Infinite Loop Error

Apr 18, 2014

I just started my first programming class and I'm already playing catch up. It's a hybrid and the lectures are really short and fast paced that I don't entirely understand what is going on. I also got my textbook late (I was on the waitlist so didn't want to commit to buying early), so it's been a rough couple weeks.

Basically, I'm supposed to simulate a rental service transaction where the user will input numbers 1-4 to indicate which equipment they want to rent and input either H/h or F/f to indicate if it's for a (H)alf-day or (F)ull-day. I think I'm getting stuck in an infinite loop because every time I enter run the program to test the conditions in the loop, the console returns the "invalid choice" statement that I created in case the user inputs numbers less < 1 or > 4.

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Scanner;
public class Rental {

[code]....

View Replies View Related

How To Stop Infinite Loop Programmatically

Mar 24, 2015

I am developing application called java compiler... It takes java program as input and compiles and run it, gives output. but if input program has infinite loop then how can identify and stop process execution.

View Replies View Related

Infinite Loop On Double Linked List

Sep 11, 2014

I've been stuck for the last couple hours trying to understand why the "printInReverse" method is getting into an infinite loop. I was supposed to make a double linked list that you can insert numbers and it orders both ascending and descending; in this case, descending is the "printInReverse" method, which takes the already ordered lists and prints it reversed, like if it was descending, and that's where the problem lives. Here follows the whole code:

import java.util.Scanner;
public class MyList {
private Scanner userInput;
private Integer selectedOption;
private MyListElement firstElement = null;
private boolean exitRequested = false;
 
[Code] .....

View Replies View Related

Java-8 :: How To Stop Infinite Stream

May 26, 2014

I have a Stream instace which produces values using an infinite Supplier (it supplies values taken from an electronic sensor -- so unless the battery is low, the sensor will provide "for ever").

The stream is processed by a Collector using Stream.collect() (e. g. imagine that the values from the sensor should be averaged; in fact what it does is a bit more compliacted maths).

The problem is that the collector does not produce a result but hangs up, as the supplier does never stop providing more sensor values.

So what I need is a limitation rule that stops the stream. While there is a Stream.limit(long) method, it actually does not solve my problem as in my case it is not practical to stop after a particular count, while I actually want to stop streaming when the sensor value exceeds a particular limit etc. (hence, voids an arbitrary rule).
 
To sum up, what I need is Stream.limit(Predicate), i. e. the stream will stopped once the predicate becomes true.
 
Unfortunately I did not find anything like that in JRE 8.
 
Is that planned for JRE 8.1 or JRE 9.0? Or is there a known (and sophisticated) workaround?

View Replies View Related

Java Application For Infinite Series That Calculates PI

Sep 5, 2014

I am having trouble compiling my code for a java application that is supposed to print out the infinite series for Pi. Here is my java code:

//a java application that generates the infinite series for Pi, ie 3.14159...=4-4/3+4/5-4/7+4/9
 
public class Pi{
public static void main(String args[]){
//declare and initialize variables
 long counter=1;
double pi;
 
[Code] ....

Here is the error in my output that results when I attempt to compile my code:

C:UsersanonymousDesktopchapter five exercises for javaPi.java:21: error: variable pi might not have been initialized
total=total+pi;
^
1 error
Tool completed with exit code 1

Why do I need to initialized pi when I already initialized the total?

View Replies View Related

Graphical Program With Several Label Objects - How To Use Infinite Loops

Feb 22, 2015

I'm writing a graphical program with several Label objects. One of them is supposed to constantly change color. I tried to do it with while loop like this:

while (true) {label1.setColor(Color.blue); pause (80); label1.setColor(Color.red); pause (80);}

However, the rest of the code (after the loop) is never reached because the loop never ends. Is it possible to use infinite loop like this? And, is there any other way to handle permanent processes that are supposed to run as long as the program is running (like, in this case, blinking Label)?

View Replies View Related

Player One Type A Number And Player Two Suppose To Guess - Infinite Looping

Apr 2, 2014

Its a basic program that is played by 2 people. Player one is suppose to type a number and the second player is suppose to guess the number. However after I test it out, and if I guess too low or too high I get stuck in "Your guess is too low, try again." infinite loop, what is wrong.

import java.io.*;
class GuessingGame
{
public static void main (String[] args) throws IOException
{
BufferedReader stdin = new BufferedReader (new InputStreamReader (System.in));
String firstPlayer, secondPlayer;
int firstInput, secondInput;
int guessCount = 0;

[Code] .....

View Replies View Related

Unable To Print A Loop Inside Array Of Greater Size Than Loop Itself

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

While Loop Inside A For Loop To Determine Proper Length Of Variable

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

When Type Quit To Close Outer Loop / It Still Runs Inner Loop

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

Convert From While Loop To For Loop

Mar 8, 2014

How to convert this program from a while loop to a for loop.

import java.util.Scanner;
public class LongDivision {
public static void main(String arguments[]){
Scanner input = new Scanner(System.in);
System.out.println("Enter the dividend: ");

[Code] ....

View Replies View Related

Converting For Loop To While And Do While Loop

Feb 17, 2014

How do I convert my for loop below to a while and do while loop?

for(int a=1;a<=9;a++){
for(int b=1;b<=a;b++){
System.out.print(a);
}
System.out.println();

[Code] .....

View Replies View Related

How To Store Value From Loop To Add To New Value From Loop

Feb 9, 2015

I am trying to make a program add values from a loop. So what its supposed to do is search through tokens on an imported file. The file lists State, Capital, and then capital population. Then take the population string, turn it into numbers, and then do stuff with the numbers. First I'm supposed to find the Highest and lowest population of the places in the file (which I did without problem), but the finally thing is I'm supposed to add each found population to the last so I can find the average of the populations.

I just cannot seem to grasp how to do that. I THINK I'm supposed to some how store the given value into a variable, but how do I get that variable to add to the new value?

like...?
Get token -> a
b = a
c = a + b

or wait no.....

Java Code :

import java.io.*;
import java.util.Scanner;
public class CapPopS
{
public static void main(String[] args) throws IOException
{
File stateCAP = new File("state-capital-2004population.txt");
if (!stateCAP.exists())

[Code] ....

View Replies View Related

How To Add Within Loop

Feb 9, 2015

My teacher wants me to make a program that counts the amount of steps a student takes a day. The program asks for other info such as name, age, college. However I need to write a loop that will allow the user to enter how many ever steps they took and convert them to miles.how exactly to make the steps entered by the user within the loop be their own individual days like monday tuesday etc. Like the loop will ask how many steps did you take monday.. tuesday.. etc for each time it runs.

package StudentInfo;
import java.util.Scanner;
public class studentinfo {
public static void main (String [] args){
Scanner scan = new Scanner(System.in);

[code]....

View Replies View Related

How To Use While Loop

Apr 5, 2014

I need to write a program that measures how long it will take someone to make a million dollars if he is being paid $5.75 an hour, but the pay rate is increase by 0.2% each week after the third week.

View Replies View Related

Why Won't FOR Loop End

Feb 18, 2014

This is the first time I've ever gotten an infinite loop with a FOR loop. This program is supposed to let you enter five integer numbers and draw a bar chart based on those numbers. After the fifth number is entered, guess what? It wraps back around to zero again and starts over! Why the bleep doesn't it stop? The code is below:

Java Code: import java.awt.Graphics;
import javax.swing.JPanel;
import java.util.Scanner;

[Code]...

View Replies View Related

Inner If Loop Code

Mar 23, 2015

Can i write inner if loop without { and } if it has more lines of code as below

public class TEst111 {
/**
* @param args
*/
public static void main(String[] args) {
int num=11;
// TODO Auto-generated method stub
if (num < 10)

[code]....

I tested to see outer if which does not need { and } even though it has multiple lines in that

View Replies View Related

Conversion From While To For Loop

Aug 3, 2014

I just started to teach myself loops. I am trying to convert the while loop into for loops, however somehow I do not get the same printout.

While loop:
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
int i = 1;
int sum = 0;

[Code] ....

Print:
sum is 0 and i is 1
sum is 1 and i is 2
sum is 3 and i is 3
sum is 6 and i is 4

View Replies View Related

Loop To Add Integers From 1 To 50?

Mar 11, 2014

Create a loop where you add the integers from 1 to 50.

public class Sum50 {
public static void main(String[] args) {
int sum = 0;
int max = 50;
for(int i = 0;i <= max; i++){
sum=sum+i;
}
System.out.println("Sum is " + sum);
}
}

View Replies View Related

Semicolon After For Loop Is Not Necessary

Jun 15, 2014

I'm fairly new to programming, just started yesterday with a book I downloaded of the net. The book comes with a couple of examples on how to code and they all seem to work fine except for this loop.

class fordemo {
public static void main(String args[] ) {
int count;
for(count = 0; count < 5; count = count+1);
System.out.println(" This is count: " + count);
System.out.println("Your are all done! ");
}
}

I can't seem to find the problem. All it gives me is,
"This is count: 5
You are all done!"

When it's supposed to give me,

"This is count: 1
This is count: 2
This is count: 3
This is count: 4
You are all done!"

Found the error, semicolon after the for loop is not necessary. Should be...

class fordemo {
public static void main(String args[] ) {
int count;
for(count = 0; count < 5; count = count+1)
System.out.println(" This is count: " + count);
System.out.println("Your are all done! ");
}
}

View Replies View Related

Keylistener In A Loop?

Jun 16, 2014

I am trying to program a version of the "Worlds Hardest Game" using ready to program and applets for a final class project. Unfortunately I cannot figure out how to get my keylistener to work because I am using a loop for the enemies to go back and forth.

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
 public class HardestGame extends Applet
implements KeyListener, MouseListener {
final int WIDTH = 400;
final int HEIGHT = 123;
int myX = 400;
int myY = 400;

[code]...

View Replies View Related

How To Use Try Catch In Do While Loop

Jun 9, 2014

I wrote a program using switchcase.I used do while to show the menu to the user until the user decides to exit the menu.I used try catch to prevent ant exception and it worked properly.But i got one problem.When exception occurs,desired msg is printed but i am unable to display the menu to the user.So user wont be able to continue after an exception is caused.

View Replies View Related

Getting Right Answer In While-loop

Jan 29, 2015

If I for example choose 8 as "multiplikationstabell" and 4 as "faktor" the whole code works except that "svar" gets printed as 8 in every turn. Why? "Faktor" gets added with 1 every time but "svar" stays at 8.......

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

[Code] .....

View Replies View Related

For Loop - How To Get Rid Of Redundancy

Jan 21, 2015

PHP Code:

public static void drawFirstHalf() {
for (int line = 1; line <= SIZE; line++) {
System.out.print("|");
for (int dots1 = 1; dots1 <= SIZE - line; dots1++) {
printDot();

[Code] ....

How would I get rid of the redundancy of the dots1 and roof for loop in the nested for loop? The lines are just repeated and I'm not sure how to get rid of the redundancy.

View Replies View Related

Loop Not Stopping Where It Should

Sep 14, 2014

I am doing a simple program, just to find multiples of 7 all the way to 125. But the loop its not stopping at 125 and is going to 126.

public class Assingment2B {
public static void main(String[] args) {
int number = 0;
while (number <= 125) {
number += 7;
if (number % 7 == 0) {
System.out.println(number);

[code]....

View Replies View Related







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