Java: Loop not working [closed] - java

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++)

Related

ArrayIndexOutOfBoundsException in INTELIJ [closed]

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 3 years ago.
Improve this question
I just started learning java, and I created a support code (called HELP) to help me track some variables in another code im writing. But when I try to run HELP I get this exception in return, can someone help me?
Im using INTELIJ
public static void main(String [] args){
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
int T = Integer.parseInt(args[2]);
for (int i = 0; i < T; i++) {
//bob vĂȘ a carta
int see;
int unseen;
if (Math.random() > .5) {
see = a;
} else see = b;
System.out.println(see);
}
}
the output is:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0
at HELP.main(HELP.java:4)
You aren't specifying any arguments when you run the program so args[0], args[1], args[2] isn't a valid index.
In one old post founded this
// to use 10 when there aren't args...
int trials = (args.length > 0) ? Integer.parseInt(args[0]) : 10;

Why integer value gets 0 when the limit is exceeded? [closed]

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 3 years ago.
Improve this question
Why an int becomes zero when it exceed the int limit in java?
Eg:
import java.util.Scanner
public class MyClass{
public static void main(Stings [] args){
Scanner sc = new Scanner(System.in);
int x = sc.nextInt(); // enter 50 or higher.
int answer = 1;
while(x>0){
answer = answer*x;
x--;
}
System.out.println(answer);
}
}
At some point answer becomes 0 (integer overflow) and stays 0.

For Loop seeming to be running twice [closed]

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++;
}

Why do I get Unreachable Code in this situation? [closed]

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++;
}

Error in retrieving the lenght of a string [closed]

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
What's wrong with my code?
import java.util.Scanner;
class Pali
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.println("\nInserisci una stringa: ");
String p = in.next();
int n = p.lenght();
}
}
I get a "cannot find symbol" error in p variable. Why? Many thanks (sorry,if I did something wrong, it's my first post).
It's a typo:
int n = p.lenght();
Should be p.length();
It is a typo.
int n = p.lenght();
Correction: int n = p.length();
It happens to the best of us.
Use: int n = p.length(); instead of p.lenght();

Categories

Resources