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


ADVERTISEMENT

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

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

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

If Statement Won't Work Inside A While Loop

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

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

Declare String Inside Of Loop - Use It Outside

Apr 26, 2015

I'm trying to code a little text RPG. I've made a little "personality test", with 4 questions (answers a b c), where every letter stands for a type of personality. The analysis of the result is simple counting of the answers, if you have 3 answers a (4 questions), then a has won. If 2 on a, and 2 on b, a simple Random method shows weather result a or b.

Now... the test should give you a "partner" (imagine the pokemon game with 3 different starter pokemon). I have now 3 string variables, like 3 different partners. They are all declared somewhere outside, but i only need one later.

Like...the test is completed, you have your partner and how can i make now that the program is just showing me my partner? Like...when i type " System.out.println(partner); " (outside of the test loop! ) that i only get the one i got through the test?

I was trying to declare in every loop the partner String with every result. But outside the loop java isn't recognising that String, bcs...ofc...it was declared for the loop. So i had in every if or else if clause a " String partner = anwsA;" and answB and answC, thats why i cant declare them outside.

Short: i need a partner String variable that could have 3 possible results... i jut need one

I'm using Eclipse Luna 4.4

View Replies View Related

Unable To Use Double Data Type In For Loop

Aug 12, 2014

I am trying to use double data type in a for loop for precise operations and just to see if there could be any problem doing that I tested a small code :

public class doubleLimit {
public static void main(String[] args){
for(double i=-0.1;i<=0;i+=0.01)
System.out.println(i); }}

The output I was expecting is :
-0.1
-0.09
-0.08
-0.07
-0.06
-0.05
-0.04
-0.03
-0.02
-0.01
0.00

But the output of the code is :
-0.1
-0.09000000000000001
-0.08000000000000002
-0.07000000000000002
-0.06000000000000002
-0.05000000000000002
-0.040000000000000015
-0.030000000000000013
-0.02000000000000001
-0.01000000000000001
-1.0408340855860843E-17

Why is the code not working the way I expected, I think it has something to do with any property of double but I am not sure.

View Replies View Related

Swing/AWT/SWT :: Update JTable Cell Values Inside A Loop

Jan 5, 2015

I have a program where i want to indicate the user when i have completed a task which i am running inside a for loop. Below is my code.

for(Map.Entry<Double,SimplestCopyInstance> entry : myList.entrySet()){
double key = entry.getKey();
SimplestCopyInstance scp = entry.getValue();
Copy cp = new Copy();
cp.putLocations(scp.getSrc(), scp.getDes());
scp.setStatus(cp.baseCopy()+"");

[Code] ....

I have used netbeans to build my app. So there creating jTable is out of my control as that part was inside auto-generated code. I have just used the jTable.setValue().

My problem is, above method is inside a button click event. Updated values not displaying until the loops ends.

View Replies View Related

How To Print Big Integer Using For Loop

Mar 6, 2015

I amtrying to iterate a value of L,R both are range of BigInteger but its not working

BigInteger L=in.nextBigInteger();
BigInteger R=in.nextBigInteger();
for (BigInteger bi =L;
bi<=bi.compareTo(R);
bi = bi.add(BigInteger.ONE)) {

//Task to do with Numbers
}

I am trying to iterate a for loop in range of L and R but its not working

View Replies View Related

Print A Diamond Using For-loop?

Mar 26, 2014

I am currently trying to print a Diamond using for-loops but I seem to be confusing my self / over-complicating it.

This is what I have so far [URL]). When I run the code I get [URL].

I want to duplicate it on to the other side but I can't seem to be able to figure out a working for-loop to print it out.

Also, is there a faster/efficient method of doing something like this?

View Replies View Related

While Loop To Print All Odd Numbers Between 1 And 100

Apr 3, 2015

I want to write a while loop to print all odd numbers between 1 and 100.

But it gives error. Where is the mistake?
 
public class pro {
public static void main(String[] args) {
// TODO Auto-generated method stub 
int c;
int x=1;
 
[Code] ....

View Replies View Related

Symbol Won't Print From For Loop

Oct 20, 2014

On line 45 i am trying to add "-" symbol to my output phone number. As of now the out prints like "1800*45*3569377. I want it to print 1800-3569377, or even more ideal: 1-800-356-9377. When printing normally (system.out.println) I can print symbols, but when I try to print from a for-loop it says "unclosed character literal." import java.util.Scanner;

public class Phone_010473030 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a phone number string: ");
String Phone_num = input.nextLine();
Phone_num = Phone_num.toUpperCase();

[code]....

View Replies View Related

Print Variable Which Only Exists Within For Loop?

Jul 18, 2014

There's loads of problems with this. What I'm trying to do.

1) get a program to add the contents of an array together, preferably with a for loop and not the heavy handed version I've tried to use here.

2) get the for loop's output just once, since it won't compile or recognise the variable outside of the loop. How do I make the loop's 'counter' variable available everywhere?

public class retint {
 public static void main(String[] args){
int[] onetoTen = {1,2,3,4,5,6,7,8,9,10};
for (int i=0; i<10; i++) {
int counter = (onetoTen[0] + onetoTen[1] + onetoTen[2] + onetoTen[3] + onetoTen[4] + onetoTen[5] +
onetoTen[6] + onetoTen[7] + onetoTen[8] + onetoTen[9]);
 System.out.println(counter); 
}
}

Terrible code, I know. There has to be a more efficient way.

View Replies View Related

Print Triangle - Loop Is Not Working

Jul 7, 2014

I am trying to make a program that prints triangle... and I did various test on each method to realise that the problem lies with this segment.When I call this method, nothing prints out, I figure there is something with the loop that I am not realizing.the loop is backwards because it's supposed to have the right side edge parralel (when I try to print it out the spaces do not appear, imagine the x are space...), so as each line is looped the # of spaces diminishes

xxxx*
xxx*x*
xx*xx*
x*xxx*
*****

public class test {
public static void main(String[] args){  
for (int countdown = 5; countdown <= 1; countdown = countdown--){
showNTimes(countdown, ' ');
showNTimes(5- countdown, '*');
System.out.println("");
}
}
public static void showNTimes ( int nbTimes, char carac ) {
for ( int i = 1 ; i <= nbTimes ; i = i + 1 ) {
System.out.print( carac );
}
}
}

View Replies View Related

Program Ask For Five Names Using Loop Statement And Print All

Jan 24, 2014

write a program that would ask for the five names using loop statement and print all names .

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

For Loop - Print All Odd Numbers Between FirstNum And SecondNum

Feb 4, 2015

Prompt the user to enter two ints, firstNum and secondNum. Then print all odd numbers between firstNum and secondNum.

Here's my code-- my error is in this line: for(firstNum;firstNum<=secondNum;firstNum++)

The error is "not a statement."

import java.io.*;
import java.util.*;
public class ListOddNumbers
{
public static void main(String[] args) {
Scanner keyboard= new Scanner(System.in);
System.out.println("Enter your first number.");
int firstNum= keyboard.nextInt();

[Code] .....

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

Print Route Method / Stuck In A Loop Of Printing Same Two Nodes

Nov 13, 2014

My issue is that when I run my search, it does find a goal. However, when I try and print the route using my print route method, it just gets stuck in a loop of printing the same two nodes. What is wrong with My A* implementation?

package search;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
 
[code]....

View Replies View Related

For Loop To Count Values Of Stack Of Quarters From 1.00 To 3.00 And Print Value Of Each Iteration

Nov 2, 2014

I had to write a foor loop to count the values of a stack of quarters from 1.00 to 3.00 and I had to print the values, that I understood and got it working so I taught the next assignment was going to be easier but I am having a hard time with it. For this one I have to write a for loop to print all the positive integer factors of 144 and I am supposed to print of factor per line but I tried doing that but it doesn't work it just prints out 144.

This is my code. The quarter assignment is also in there because it is part of a lab so just ignore that part since it is working correctly.

public class ForLoopPractice
{
public static void main(String [] args)
{
// Write a for loop to count out the values of a stack of quarters from $1.00 to $3.00
// Print the value of each iteration. Print this all on one line, rounded to the nearest cent.
// To print rounded, use printf, with a placeholder of %.2f
// (%f is the floating-point placeholder; the .2 indicates the number of decimal places)
/* YOUR CODE HERE */
for (double q = 1.00; q <= 3.00; q += .25)

[Code] ....

View Replies View Related

Invoke Some Methods In For Loop In Order To Print Some Info Stored In A List

Apr 28, 2014

I am trying to invoke some methods in a for loop in order to print some info stored in a List. But for some reason, compiler pops a message saying "cannot find symbol - method getEmpID(). You are using a symbol here (a name for a variable, method or class) that has not been declared in any visible scope." But I am pretty sure that method getEmpID (as also getName(), getAfm(), and payment() ) have been declared as public.

Note: My List contains objects of different type (SalariedEmployee, HourlyEmployee). I hope this is not the factor causing this problem.

Java Code:

import java.util.*;
abstract class Employee{
private String name = "";
private String afm = "";
private long EmpID;
static long count=0;

[code]....

View Replies View Related

Unable To Print Out Randomly Generated Array

Nov 19, 2014

i am trying to print out a randomly generated array, but i only get

[I@7852e922

i did some research and the "[" is for arrays, "I" is for the int and "@" is the hash. and the rest is some sort of hex. I need to override this in a way, but i can't seem to find out how.
this is my current code:

import java.util.Random;

public class Oppgave {
public static void main(String[] args){
int myint[] = fyll();
System.out.println(myint);
}
public static int[] fyll() {

[Code]...

View Replies View Related

Unable To Print Repeating Integers In Array

Aug 5, 2012

I have a problem where i have to write some code to read through an int array and print out the different integers and how many times they occur. For example:

if the array contained the numbers 1,4,2,3,5,4,4,7,5,4,3,6,8,6,4,

i would need the print to appear something like this

integer: 1, times: 1
integer: 4, times: 5
integer: 2, times: 1

etc

as the array is read sequentialy from element 0 to the end. the program should find the integer value in the first element (increment a counter by 1) and then search all other elements to see if the integer reoccurs (counter++ for each time it reappears). obviously the counter would be the 'times' value in the second column.

My problem is i can get this accomplished yet i cant get the program to recognise when it has already registered an integer in a previous element and skip to the next element looking for a new integer. SO for each element in the array it prints the integer and then searches the entire array for the integer again and increments the counter accordingly. so in essence there is a line printed for each integer the number of times it actually occurs.

I simply wish to have my code to find an integer and the number of times it occurs. and then disregard the elements it has registered the previous integer in.

Also, keep in mind that i am NOT permitted to use any new data structures/arrays for storing/remembering values. I can only use int and double variables throughout.

what i have so far is this: with the array being customerID[] :

Java Code:

int num;
int counter;
for(int count = 0; count < customerID.length; count++)
{
counter = 0;
num = customerID[count];
for(int count2 = 0; count2 < customerID.length; count2++)

[Code]...

The output for the int array 1,1,3,0,3,2,0,4,1,3, looks something like this:

Integer: 1, Times: 3
Integer: 1, Times: 3
Integer: 3, Times: 3
Integer: 0, Times: 2
Integer: 3, Times: 3
Integer: 2, Times: 1

[Code]...

and as you can see lines are repeated for each time the same integer occurs in a new element. i just need my code to skip an element if the integer has been previously encounterd and recorded. i.e i need it to look more like this :

Integer: 1, Times: 3
Integer: 3, Times: 3
Integer: 0, Times: 2
Integer: 2, Times: 1
Integer: 4, Times: 1

with the first encountered integer being checked throughout the array and then moving to the next integer/element which is not the same as any previous.

View Replies View Related

For Loop Involving Array

Jan 29, 2015

As a result I need to get 6 66 666 6666 66666, but I'm getting a mistake. Can anb explain what's wrong?

Java Code:

public static void main(String [] args)
{

int[] sixes = new int [6];
for (int i = 0; i < sixes.length; i++)
{
sixes[i] = i * 10 + 6;
System.out.print(sixes[i]);
}

} mh_sh_highlight_all('java');

View Replies View Related

How To Do For Loop With Char Array

Apr 15, 2015

How can I display this result.aJohny

I have array contain numberi create new char array and i want to check if a number is >= 90 in array then input A at the same index in char arraythen second part i want to check if char array contain "A" then a = a + 1. 

The result is like this:

A: 5
B: 29
C: 38
D: 24
E: 12
F: 17
 
public class Test {
public static char[] grades(int[] getMarks) {
                        char[] grade = new char[getMarks.length];
                        for (int i = 0; i < getMarks.length; i++) {
                                    if (getMarks[i] >= 90) {

[Code] .....

View Replies View Related







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