I need to rewrite Java "for" to "while" and "do" [closed] - java

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
i made a "for" a while a back ago now i need to change it to a "While" and change that to a "do", any help would be appriciated
for(int i = 3; i <= 10; i++) {
System.out.print(" " + i);
if(i % 10 == 0)
System.out.print("\n");
}
System.out.println();
}
}

A for statement:
for (ForInit; Expression; ForUpdate) Statement
is equivalent to the while statement:
{
ForInit;
while (Expression) {
Statement;
ForUpdate;
}
}
Converting a for to a while is simply a matter of cutting+pasting into this template.
for loops don't map onto to a do/while cleanly, because the Expression needs to be evaluated before Statement. You could do this:
{
ForInit;
do {
if (!Condition) break;
Statement;
ForUpdate;
} while (true);
}
but that's just horrible; you should be using a while statement instead.

Related

How to handle Number format Exception? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
My requirement is to check divisibility of the input by 7, for the various test cases and I have written this code but it is throwing me NumberFormat Exception
class Solution{
int isdivisible7(String num){
// code her
long i= Long.parseLong(num);
// to convert string into long
if(i%7==0)
return 1;
else
return 0;
}
}
How can I handle the exception and return the result for any (both valid and invalid) input ?
If the input num is not number, then it will throw NumberFormatException, so you just have to catch it. Also, function names should be in camel case. And finally, it's better to make the function return boolean rather then int of values 0 and 1.
boolean isDivisibleBy7(String num){
try {
long i = Long.parseLong(num);
return i % 7 == 0;
} catch (NumberFormatException e) {
// print some error message if you want
System.out.println("You haven't passed number");
return false;
}
}

What does return "" mean? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
for (int i = 0; i < alist.size(); i++) {
if ( alist.get(i).average() == d ) {
return alist.get(i).getCountry();
}
}
return "";
}
I need to return the name of the country which is in alist.get(i).getCountry(), but when I end the body with that it will say
error: missing {
When I put return "" it doesn't have an error.
What does return "" mean?
return ""; just returns an empty string.
If you put alist.get(i).getCountry() outside of the for loop it will not make sense. (I'm assuming that's what you mean by "end the body with that.") It depends on i, which only exists in the loop.
It's tough to see why you're getting that error without seeing more of your code.

Java scanners - skip all "non-int" and "less than 0" inputs [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I want to loop through and skip all inputs that are less or equal 0 or non integers.
I have written this code, but it doesn't work and I don't understand why.
while(!userInput.hasNextInt() || userInput.nextInt() <= 0) {
userInput.next();
}
return userInput.nextInt();
I think you need to change a logic a bit, for example:
while (userInput.hasNext()) {
if (userInput.hasNextInt()) {
int intValue = userInput.nextInt();
if (intValue > 0) {
return intValue;
}
}
userInput.next();
}
Because when you're trying to verify that int value is less or equal zero userInput.nextInt() <= 0, you're actually getting the value.
So, if it's not true, you will go to this line return userInput.nextInt();, but cursor will already be on the next value.
You can check whether there is any user input or not. If there is take the input and then process it else continue with the loop.
while(userInput.hasNextInt()){
int a=userInput.nextInt();
if(a>=0){
return a;
}else
continue;
}

Why is this while-loop not working? [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
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;
}
}

java break loop not working [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a break label in my java code but it doesn't jump to the label when I go to the break statement in my code:
OUTERMOST:for(String s :soso){
if(wasBreaked){
//code never enters this loop
Log.e("","WASBREAK = FALSE");
wasBreaked = false;
}
if(true){
Log.e("","WASBREAK = TRUE GOING TO OUTERMOST");
wasBreaked = true;
break OUTERMOST;
}
}
Break statement is not really a goto statement as in other programming languages like C.
What your code does instead is, break from the loop which has label OUTERMOST. I would have thought you need continue OUTERMOST; instead of break. But to me it really doesn't makes sense as you dont have further any statement post continue (now break in your code) and it's going to continue anyways irrespective of whether you say explicitly continue or not.
public class Test {
public static void main(String[] args) {
String soso[]={"1","2"};
boolean wasBreaked=false;
OUTERMOST:for(String s :soso){
if(wasBreaked){
Log.e("","WASBREAK = FALSE");
wasBreaked = false;
}
if(true){
Log.e("","WASBREAK = TRUE GOING TO OUTERMOST");
wasBreaked = true;
System.out.println("before break");
break OUTERMOST;//use continue in place of break here for going to label
}
System.out.println("Inside outermost loop");
}
System.out.println("outside outermost loop");
}
}
I tried this and it works
it gives output
before break
outside outermost loop
For going back to label you should use continue keyword rather than break in code. After using continue the output will be like
before break
before break
outside outermost loop

Categories