Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
EDIT: If I am asking this wrong, I am sorry. My expected results would be to end up with this for loop running twice total. I would expect only a:
System.out.println(inventory[0][1]);
System.out.println(inventory[1][1]);
I have what I thought was a simple for loop to pull from a database, and add each result to a String array.
while(rs3.next()) {
for(int c = 0; c < iCount; c++) {
inventory[c][0] = rs3.getString(10);
inventory[c][1] = rs3.getString(9);
System.out.println(inventory[c][1]); //#&(*& WHY IS THIS HAPPENING TWICE
inventory[c][3] = Integer.toString(rs3.getInt(3));
inventory[c][6] = rs3.getString(6);
inventory[c][10] = Integer.toString(rs3.getInt(12));
inventory[c][12] = Integer.toString(rs3.getInt(13));
inventory[c][31] = Integer.toString(rs3.getInt(14));
inventory[c][32] = Integer.toString(rs3.getInt(15));
inventory[c][34] = Integer.toString(rs3.getInt(16));
inventory[c][52] = rs3.getString(4);
inventory[c][53] = Integer.toString(rs3.getInt(5));
inventory[c][18] = Integer.toString(rs3.getInt(7));
inventory[c][29] = Integer.toString(rs3.getInt(8));
inventory[c][78] = Integer.toString(rs3.getInt(1));
}
}
int iCount is set at 2 in this example, which i checked using System.out.println().
The problem is, it is recording each value twice.(per loop, so a total of 4 times in this example) This throws off other methods using this data.
I added the println(inventory[c][1]); to verify to myself that it was indeed doing this action twice PER LOOP of the for loop.
I expect you want:
int c = 0;
while(rs3.next()) {
inventory[c][0] = rs3.getString(10);
inventory[c][1] = rs3.getString(9);
System.out.println(inventory[c][1]);
// ... rest omitted for brevity
c++;
}
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
Why here i'm getting not concatenated string
String zero = "0";
for (int i = 0; i <= 5; i++)
System.out.println(zero);
zero += "0";
and here i'm able to concatenate?
String test = "Hello";
System.out.println(test);
test += "world!";
System.out.println(test);
When a for-loop has no curly braces then only the very next statement is repeated:
String zero = "0";
for (int i = 0; i <= 5; i++) {
System.out.println(zero);
zero += "0";
}
Note that indentation has no syntactic value in Java, so the fact that both the println and concatenation statements are indented in your example misleads the reader to think that both statements are repeated when only the println statement is.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
Every time I try and run this code currentCost returns as 0.
I am trying to calculate depreciation # 20% using loops with the value and age of the asset (here a car) from the user. Thank you.
float cost = Integer.parseInt(jTextField2.getText());
int vehicalAge = Integer.parseInt(jTextField3.getText());
float currentCost = 0;
for (int a = 1; a == vehicalAge; a++) {
cost = (float) (cost - (cost * 0.2));
currentCost = cost;
}
JOptionPane.showMessageDialog(this, Float.toString(currentCost));
Problem is in your for loop:
for (int a=1; a==vehicalAge;a++)
it should be
for (int a=1; a<=vehicalAge;a++)
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
Why I try to use the loop to display the numbers I had put into the ArrayList, it says Unreachable code what am I doing wrong.
private void nOther1() {
ArrayList<Integer> multiples = new ArrayList<Integer>();
int n = 1;
while (1 <= 100) {
multiples.add(n);
n++;
}
// UNREACHABLE CODE
for (int num : multiples) {
System.out.println(num);
}
}
Because
while (1 <= 100)
will never terminate since 1 is always <= 100, and the compiler knows that.
See Chapter 14.21. Unreachable Statements for detailed explanation.
This condition while(1 <= 100){ is always returning true
it would be the same as you do while(true){ so all behind that point is code that NEVER will be executed..
I think you wanted trying instead:
while(n <= 100){
multiples.add(n);
n++;
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
So my problem is that it keeps saying I already defined String spaces up top but if I take the definition that's outside of the loop away and try to define spaces only in the loop, it tells me it hasn't been instantiated...
public class NestedLoop {
public static void main (String [] args) {
int userNum = 0;
int i = 0;
int j = 1;
String spaces = "";
Scanner scnr = new Scanner(System.in);
System.out.print("Enter userNum:");
userNum = scnr.nextInt();
for (i = 0; i <= userNum; i++) {
while (j == i){
spaces = spaces.concat(" ");
System.out.print(spaces);
j++;}
System.out.println(i);}
Error: main.java:244: spaces is already defined in main(java.lang.String[])
I just ran this code in my IDE and got no errors, compile time or run time. Not sure I can replicate the errors you're reporting. Only possibility I could think of is an error with your JDK, but at the same time I dunno how or why that would occur.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
I've just started working with some programming in java. I'm experimenting with loops and I have a problem with a for-loop where I'm having a hard time finding the mistake. Its saying that "i" is not a variable, even though i made it one in just above. Hope you guys can help!
import java.util.Scanner;
public class Loops {
public static void main(String [] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Skriv et tal");
int a = sc.nextInt();
for(int i = 0; i <= 9; i++);
{
System.out.println(a + i);
}
}
}
for(int i=0; i<=9;i++);
// ^ get rid of this
should be
for(int i=0; i<=9;i++)
Because of that the for statement ends there and the new block had been started there.
Beware of the ; behind your loop definition. It is an empty statement.