Why is this while-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 7 years ago.
Improve this question
The following code compiles, but when I ran it, it didn't enter the while-loop. Why ?
while (Str1.equals(Str2)); {
count = 1;
while (count <= maxCount); {
System.out.print(something1);
count = count + 1; } }

Remove the semi colon, semicolon is added only for do-while.Here is your code:
while (Str1.equals(Str2))
{
count = 1;
while (count <= maxCount)
{
System.out.print(something1);
count = count + 1;
}
}

Related

java.lang.Error am i missing something in the code? [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 1 year ago.
Improve this question
This is just a simple query if anyone knows why I am facing this error I would appreciate the help.
The problem is simple java if else if else loop problem .
so I tried to make syntax as correct as possible.
The code here below is the one I am executing.
public class ifelseloop {
public static void main(String[] args){
// int a = 20;
// int b = 20;
String city = "Delhi";
if (city == "madras") {
System.out.println("the city is not delhi its madras");
}else if(city == "chennai") {
System.out.println("the city is not delhi its chennai");
}else if(city == "bartar") {
System.out.println("the city is bartar");
}else {
System.out.println(city);
}
}
}
and the error that I am getting is this.
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
at ifelseloop.main(ifelseloop.java:2)

While loop is broken, even though parameters are correct [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 2 years ago.
Improve this question
I am extremely confused about why my code is not running. I end up being in a loop that lasts forever that I assume is the while loop that happens after the print statement. The print statement that is listed prints true, so the while loop should go through at least one iteration... right?
This does not happen, my code ends up running forever, and does not end up printing anything else.
(Also just ignore the if statement with the %, I am testing on a small scale before going big.)
Scanner scan = new Scanner(file);
FileWriter fw = new FileWriter(modifiedCSV);
int counter = 0;
System.out.println(scan.hasNextLine() && counter < 10);
while(scan.hasNextLine() && counter < 10);
{
System.out.println("test");
if(counter%1000000==0)
{
System.out.println("Normalizing Line: " + counter);
}
String line = scan.nextLine();
String[] lineData = line.split(",");
System.out.println("!!!!!!");
double normalizedDistance = (Double.parseDouble(lineData[3])-minDistance)/(maxDistance-minDistance);
double normalizedAngle = (Double.parseDouble(lineData[7])-minAngle)/(maxAngle-minAngle);
double normalizedFlux = (Double.parseDouble(lineData[8])-minFlux)/(maxFlux-minFlux);
fw.write(line + "," + normalizedDistance + "," + normalizedAngle + "," + normalizedFlux + "\n");
counter++;
}
Your while loop is empty since there is a semicolon at the end of the line with the while statement.
while(scan.hasNextLine() && counter < 10);

Simple Java Recursion Method [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
What is the wrong of this code? need help.
public class TailRrecursion {
public static void tail(int i) {
if (i > 0) {
System.out.print(i + "");
tail(i - 1);
}
}
}
For a start, you are missing curly { braces
see
public class Tail-recursion{
public static void tail(int i) {
if(i>0){
System.out.print (i +"");
tail(i-1);
}
}
} // and here

If statement not running: any ideas? [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 8 years ago.
Improve this question
I am trying to compare the third number to the last number and the fourth number to the fifth number. The if statement isn't running at all any help would be appreciated.
import java.util.*;
class Problem2{
public static void main (String[] args){
Scanner s = new Scanner(System.in);
System.out.println("Enter a number between 100000 and 999996: ");
int m = s.nextInt();
if(m < 100000 || m > 999996){
System.out.println("Out of range!");
}
else{
String j = Integer.toString(m);
for(int i=2;i<6,i++){
System.out.println(j.charAt(i));
if(j.charAt(i) == (j.charAt(i)+3)&& (j.charAt(i)+1) == (j.charAt(i)+2)){
System.out.println("Works!");
}
}
}
}
}
You are incrementing the value, not the index of the character.
Change this:
if (j.charAt(i) == (j.charAt(i)+3)&& (j.charAt(i)+1) == (j.charAt(i)+2))
To this:
if (j.charAt(i) == j.charAt(i+3) && j.charAt(i+1) == j.charAt(i+2))
// increment indexes --^ --^ --^
Move the additions inside of the charAt() calls.
if(j.charAt(i) == (j.charAt(i+3))&& (j.charAt(i+1)) == (j.charAt(i+2)))

Cannot find i in the for loop [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 9 years ago.
Improve this question
I can't seem to get the last "i" in this code to fully function, it wont respond to the initial "i", just tells me that it "cannot find symbol". What am I doing wrong?
public void fillCB (JComboBox cb){
String sqlQueryCB = "select namn from ANSTALLD order by namn";
try {
ArrayList<String> listaAnstalld = idb.fetchColumn(sqlQueryCB);
for(int i = 0; i < listaAnstalld.size(); i++); {
cb.addItem(listaAnstalld.get(i));
}
}
catch(Exception e)
{
}
you have a semicolon at the end of your for loop, before the opening brace.
for(int i = 0; i < listaAnstalld.size(); i++); {
That semicolon closes the for loop. should remove it.
because of this, the contents of your block will not be in the scope of the for loop(and therefore not in the same scope that i is in).

Categories