Can I give a condition to boolean primitive as initialize?
I'm not sure it is possible, because I haven't found exact answer, but NetBeans not gives error. But if it's possible, why my code doesn't work?
public static void main(String args) {
Scanner scan = new Scanner(System.in);
int fiveMatrix = {
{1, 4, 7, 5, 3}, {3, 7, 9, 10, 1}, {4, -3, 2, -4, 1}, {5, 9, 6, 4, 3}, {1, 2, 3, 4, 5},};
System.out.print("Which line do you want to write out (0-4)? ");
int lineNumber = scan.nextInt();
boolean goodLine = lineNumber < 0 || lineNumber > 4;
if (goodLine) {
while (goodLine) {
System.out.println("Bad index.");
System.out.print("Which line do you want to write out (0-4)? ");
lineNumber = scan.nextInt();
}
}
}
}
java while-loop boolean
|
show 1 more comment
I'm not sure it is possible, because I haven't found exact answer, but NetBeans not gives error. But if it's possible, why my code doesn't work?
public static void main(String args) {
Scanner scan = new Scanner(System.in);
int fiveMatrix = {
{1, 4, 7, 5, 3}, {3, 7, 9, 10, 1}, {4, -3, 2, -4, 1}, {5, 9, 6, 4, 3}, {1, 2, 3, 4, 5},};
System.out.print("Which line do you want to write out (0-4)? ");
int lineNumber = scan.nextInt();
boolean goodLine = lineNumber < 0 || lineNumber > 4;
if (goodLine) {
while (goodLine) {
System.out.println("Bad index.");
System.out.print("Which line do you want to write out (0-4)? ");
lineNumber = scan.nextInt();
}
}
}
}
java while-loop boolean
1
Can you explain exactly what "doesn't work"? It's hard to tell without an error message or stacktrace.
– dave
Nov 15 '18 at 8:16
AddgoodLine = lineNumber < 0 || lineNumber > 4;
insidewhile
loop to exit.
– secret super star
Nov 15 '18 at 8:20
@secretsuperstar That looks like a simple solution, but it is conceptually wrong: you do not duplicate code, even when it is just a single line. Because you would then have to remember that you have to update each duplicate of your check when the condition needs to adapted. Duplicating code is always the first step to introduce future bugs.
– GhostCat
Nov 15 '18 at 8:28
@dave you're right. I didn't mentioned the error message. It went into an infinite loop. And later I removed the if condition but that also didn't help.
– Sirsemy
Nov 16 '18 at 11:26
1
@GhostCat thank you for your notes. I'm working on understand the answers. :) I'am totally a newbie. Of course I'm going to accept one of the solutions.
– Sirsemy
Nov 16 '18 at 11:31
|
show 1 more comment
I'm not sure it is possible, because I haven't found exact answer, but NetBeans not gives error. But if it's possible, why my code doesn't work?
public static void main(String args) {
Scanner scan = new Scanner(System.in);
int fiveMatrix = {
{1, 4, 7, 5, 3}, {3, 7, 9, 10, 1}, {4, -3, 2, -4, 1}, {5, 9, 6, 4, 3}, {1, 2, 3, 4, 5},};
System.out.print("Which line do you want to write out (0-4)? ");
int lineNumber = scan.nextInt();
boolean goodLine = lineNumber < 0 || lineNumber > 4;
if (goodLine) {
while (goodLine) {
System.out.println("Bad index.");
System.out.print("Which line do you want to write out (0-4)? ");
lineNumber = scan.nextInt();
}
}
}
}
java while-loop boolean
I'm not sure it is possible, because I haven't found exact answer, but NetBeans not gives error. But if it's possible, why my code doesn't work?
public static void main(String args) {
Scanner scan = new Scanner(System.in);
int fiveMatrix = {
{1, 4, 7, 5, 3}, {3, 7, 9, 10, 1}, {4, -3, 2, -4, 1}, {5, 9, 6, 4, 3}, {1, 2, 3, 4, 5},};
System.out.print("Which line do you want to write out (0-4)? ");
int lineNumber = scan.nextInt();
boolean goodLine = lineNumber < 0 || lineNumber > 4;
if (goodLine) {
while (goodLine) {
System.out.println("Bad index.");
System.out.print("Which line do you want to write out (0-4)? ");
lineNumber = scan.nextInt();
}
}
}
}
java while-loop boolean
java while-loop boolean
asked Nov 15 '18 at 8:14
SirsemySirsemy
528
528
1
Can you explain exactly what "doesn't work"? It's hard to tell without an error message or stacktrace.
– dave
Nov 15 '18 at 8:16
AddgoodLine = lineNumber < 0 || lineNumber > 4;
insidewhile
loop to exit.
– secret super star
Nov 15 '18 at 8:20
@secretsuperstar That looks like a simple solution, but it is conceptually wrong: you do not duplicate code, even when it is just a single line. Because you would then have to remember that you have to update each duplicate of your check when the condition needs to adapted. Duplicating code is always the first step to introduce future bugs.
– GhostCat
Nov 15 '18 at 8:28
@dave you're right. I didn't mentioned the error message. It went into an infinite loop. And later I removed the if condition but that also didn't help.
– Sirsemy
Nov 16 '18 at 11:26
1
@GhostCat thank you for your notes. I'm working on understand the answers. :) I'am totally a newbie. Of course I'm going to accept one of the solutions.
– Sirsemy
Nov 16 '18 at 11:31
|
show 1 more comment
1
Can you explain exactly what "doesn't work"? It's hard to tell without an error message or stacktrace.
– dave
Nov 15 '18 at 8:16
AddgoodLine = lineNumber < 0 || lineNumber > 4;
insidewhile
loop to exit.
– secret super star
Nov 15 '18 at 8:20
@secretsuperstar That looks like a simple solution, but it is conceptually wrong: you do not duplicate code, even when it is just a single line. Because you would then have to remember that you have to update each duplicate of your check when the condition needs to adapted. Duplicating code is always the first step to introduce future bugs.
– GhostCat
Nov 15 '18 at 8:28
@dave you're right. I didn't mentioned the error message. It went into an infinite loop. And later I removed the if condition but that also didn't help.
– Sirsemy
Nov 16 '18 at 11:26
1
@GhostCat thank you for your notes. I'm working on understand the answers. :) I'am totally a newbie. Of course I'm going to accept one of the solutions.
– Sirsemy
Nov 16 '18 at 11:31
1
1
Can you explain exactly what "doesn't work"? It's hard to tell without an error message or stacktrace.
– dave
Nov 15 '18 at 8:16
Can you explain exactly what "doesn't work"? It's hard to tell without an error message or stacktrace.
– dave
Nov 15 '18 at 8:16
Add
goodLine = lineNumber < 0 || lineNumber > 4;
inside while
loop to exit.– secret super star
Nov 15 '18 at 8:20
Add
goodLine = lineNumber < 0 || lineNumber > 4;
inside while
loop to exit.– secret super star
Nov 15 '18 at 8:20
@secretsuperstar That looks like a simple solution, but it is conceptually wrong: you do not duplicate code, even when it is just a single line. Because you would then have to remember that you have to update each duplicate of your check when the condition needs to adapted. Duplicating code is always the first step to introduce future bugs.
– GhostCat
Nov 15 '18 at 8:28
@secretsuperstar That looks like a simple solution, but it is conceptually wrong: you do not duplicate code, even when it is just a single line. Because you would then have to remember that you have to update each duplicate of your check when the condition needs to adapted. Duplicating code is always the first step to introduce future bugs.
– GhostCat
Nov 15 '18 at 8:28
@dave you're right. I didn't mentioned the error message. It went into an infinite loop. And later I removed the if condition but that also didn't help.
– Sirsemy
Nov 16 '18 at 11:26
@dave you're right. I didn't mentioned the error message. It went into an infinite loop. And later I removed the if condition but that also didn't help.
– Sirsemy
Nov 16 '18 at 11:26
1
1
@GhostCat thank you for your notes. I'm working on understand the answers. :) I'am totally a newbie. Of course I'm going to accept one of the solutions.
– Sirsemy
Nov 16 '18 at 11:31
@GhostCat thank you for your notes. I'm working on understand the answers. :) I'am totally a newbie. Of course I'm going to accept one of the solutions.
– Sirsemy
Nov 16 '18 at 11:31
|
show 1 more comment
4 Answers
4
active
oldest
votes
This here:
boolean goodLine = lineNumber < 0 || lineNumber > 4;
is evaluated once, and the result is assigned to that variable.
Later changes to lineNumber = scan.nextInt();
do not change that boolean variable!
The "correct" solution: you have to recompute the boolean property. But ideally not by copying code, but by creating a small helper method:
boolean isGoodLine(int lineNumber) { return lineNumber < 0 || lineNumber > 4; }
And now, instead of having a boolean variable in your other code, you simply invoke that method whenever the lineNumber changes!
add a comment |
You probably want to update the boolean inside the loop, to avoid infinite loop.
boolean badLine = lineNumber < 0 || lineNumber > 4;
while (badLine) {
System.out.println("Bad index.");
System.out.print("Which line do you want to write out (0-4)? ");
lineNumber = scan.nextInt();
badLine = lineNumber < 0 || lineNumber > 4;
}
I renamed the boolean
, since its original name was confusing. I also eliminated the if
condition, since it's redundant.
Thank you. The rename suggestion is relly justifiable. And I really forget to say to the while loop to examine the condition every time.
– Sirsemy
Nov 16 '18 at 13:39
add a comment |
You are missing a line inside while loop
boolean goodLine = lineNumber < 0 || lineNumber > 4;
Consider refactor to a method to avoid duplicate code:
public boolean goodLine(Scanner scan) {
System.out.print("Which line do you want to write out (0-4)? ");
int lineNumber = scan.nextInt();
return lineNumber < 0 || lineNumber > 4;
}
And call it:
while(goodLine());
Consider also call it a badLine
because user input is wrong (not 0-4 values)
Thank you. Perfect solution. I choosen the other one because it's shorter.
– Sirsemy
Nov 16 '18 at 13:40
add a comment |
Simplified code and easy to understand
import java.util.Scanner;
public class Post3 {
public static void main(String args) {
Scanner scan = new Scanner(System.in);
int fiveMatrix = {
{1, 4, 7, 5, 3}, {3, 7, 9, 10, 1}, {4, -3, 2, -4, 1}, {5, 9, 6, 4, 3}, {1, 2, 3, 4, 5},};
System.out.println("Which line do you want to write out (0-4)? ");
int input = -1;
while(true) {
System.out.println("input valid number between 0 to 4");
input = scan.nextInt();
if(input >= 0 && input <= 4) {
break;
}
}
System.out.println("input is "+input);
scan.close();
}
}
Close good. Thank you. Your solution brings a new idea. On the other hand doesn't handle the first data request. And if would handle, it'll be a code duplication what I wanted to avoid.
– Sirsemy
Nov 16 '18 at 13:14
It does handle. it will continue until user inputs a valid input! do you want to take two inputs?
– secret super star
Nov 16 '18 at 13:15
I wasn't precise in my wording. I need a 'System.out.println("Bad index.");' error code in the case of bad number (as it has inside the question) . And if I use inside the while loop, it'll been read out, whether the answer is good or not. That's why I need the data request before the loop.
– Sirsemy
Nov 16 '18 at 14:01
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53314987%2fcan-i-give-a-condition-to-boolean-primitive-as-initialize%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
This here:
boolean goodLine = lineNumber < 0 || lineNumber > 4;
is evaluated once, and the result is assigned to that variable.
Later changes to lineNumber = scan.nextInt();
do not change that boolean variable!
The "correct" solution: you have to recompute the boolean property. But ideally not by copying code, but by creating a small helper method:
boolean isGoodLine(int lineNumber) { return lineNumber < 0 || lineNumber > 4; }
And now, instead of having a boolean variable in your other code, you simply invoke that method whenever the lineNumber changes!
add a comment |
This here:
boolean goodLine = lineNumber < 0 || lineNumber > 4;
is evaluated once, and the result is assigned to that variable.
Later changes to lineNumber = scan.nextInt();
do not change that boolean variable!
The "correct" solution: you have to recompute the boolean property. But ideally not by copying code, but by creating a small helper method:
boolean isGoodLine(int lineNumber) { return lineNumber < 0 || lineNumber > 4; }
And now, instead of having a boolean variable in your other code, you simply invoke that method whenever the lineNumber changes!
add a comment |
This here:
boolean goodLine = lineNumber < 0 || lineNumber > 4;
is evaluated once, and the result is assigned to that variable.
Later changes to lineNumber = scan.nextInt();
do not change that boolean variable!
The "correct" solution: you have to recompute the boolean property. But ideally not by copying code, but by creating a small helper method:
boolean isGoodLine(int lineNumber) { return lineNumber < 0 || lineNumber > 4; }
And now, instead of having a boolean variable in your other code, you simply invoke that method whenever the lineNumber changes!
This here:
boolean goodLine = lineNumber < 0 || lineNumber > 4;
is evaluated once, and the result is assigned to that variable.
Later changes to lineNumber = scan.nextInt();
do not change that boolean variable!
The "correct" solution: you have to recompute the boolean property. But ideally not by copying code, but by creating a small helper method:
boolean isGoodLine(int lineNumber) { return lineNumber < 0 || lineNumber > 4; }
And now, instead of having a boolean variable in your other code, you simply invoke that method whenever the lineNumber changes!
answered Nov 15 '18 at 8:17
GhostCatGhostCat
93.3k1689151
93.3k1689151
add a comment |
add a comment |
You probably want to update the boolean inside the loop, to avoid infinite loop.
boolean badLine = lineNumber < 0 || lineNumber > 4;
while (badLine) {
System.out.println("Bad index.");
System.out.print("Which line do you want to write out (0-4)? ");
lineNumber = scan.nextInt();
badLine = lineNumber < 0 || lineNumber > 4;
}
I renamed the boolean
, since its original name was confusing. I also eliminated the if
condition, since it's redundant.
Thank you. The rename suggestion is relly justifiable. And I really forget to say to the while loop to examine the condition every time.
– Sirsemy
Nov 16 '18 at 13:39
add a comment |
You probably want to update the boolean inside the loop, to avoid infinite loop.
boolean badLine = lineNumber < 0 || lineNumber > 4;
while (badLine) {
System.out.println("Bad index.");
System.out.print("Which line do you want to write out (0-4)? ");
lineNumber = scan.nextInt();
badLine = lineNumber < 0 || lineNumber > 4;
}
I renamed the boolean
, since its original name was confusing. I also eliminated the if
condition, since it's redundant.
Thank you. The rename suggestion is relly justifiable. And I really forget to say to the while loop to examine the condition every time.
– Sirsemy
Nov 16 '18 at 13:39
add a comment |
You probably want to update the boolean inside the loop, to avoid infinite loop.
boolean badLine = lineNumber < 0 || lineNumber > 4;
while (badLine) {
System.out.println("Bad index.");
System.out.print("Which line do you want to write out (0-4)? ");
lineNumber = scan.nextInt();
badLine = lineNumber < 0 || lineNumber > 4;
}
I renamed the boolean
, since its original name was confusing. I also eliminated the if
condition, since it's redundant.
You probably want to update the boolean inside the loop, to avoid infinite loop.
boolean badLine = lineNumber < 0 || lineNumber > 4;
while (badLine) {
System.out.println("Bad index.");
System.out.print("Which line do you want to write out (0-4)? ");
lineNumber = scan.nextInt();
badLine = lineNumber < 0 || lineNumber > 4;
}
I renamed the boolean
, since its original name was confusing. I also eliminated the if
condition, since it's redundant.
answered Nov 15 '18 at 8:17
EranEran
287k37467557
287k37467557
Thank you. The rename suggestion is relly justifiable. And I really forget to say to the while loop to examine the condition every time.
– Sirsemy
Nov 16 '18 at 13:39
add a comment |
Thank you. The rename suggestion is relly justifiable. And I really forget to say to the while loop to examine the condition every time.
– Sirsemy
Nov 16 '18 at 13:39
Thank you. The rename suggestion is relly justifiable. And I really forget to say to the while loop to examine the condition every time.
– Sirsemy
Nov 16 '18 at 13:39
Thank you. The rename suggestion is relly justifiable. And I really forget to say to the while loop to examine the condition every time.
– Sirsemy
Nov 16 '18 at 13:39
add a comment |
You are missing a line inside while loop
boolean goodLine = lineNumber < 0 || lineNumber > 4;
Consider refactor to a method to avoid duplicate code:
public boolean goodLine(Scanner scan) {
System.out.print("Which line do you want to write out (0-4)? ");
int lineNumber = scan.nextInt();
return lineNumber < 0 || lineNumber > 4;
}
And call it:
while(goodLine());
Consider also call it a badLine
because user input is wrong (not 0-4 values)
Thank you. Perfect solution. I choosen the other one because it's shorter.
– Sirsemy
Nov 16 '18 at 13:40
add a comment |
You are missing a line inside while loop
boolean goodLine = lineNumber < 0 || lineNumber > 4;
Consider refactor to a method to avoid duplicate code:
public boolean goodLine(Scanner scan) {
System.out.print("Which line do you want to write out (0-4)? ");
int lineNumber = scan.nextInt();
return lineNumber < 0 || lineNumber > 4;
}
And call it:
while(goodLine());
Consider also call it a badLine
because user input is wrong (not 0-4 values)
Thank you. Perfect solution. I choosen the other one because it's shorter.
– Sirsemy
Nov 16 '18 at 13:40
add a comment |
You are missing a line inside while loop
boolean goodLine = lineNumber < 0 || lineNumber > 4;
Consider refactor to a method to avoid duplicate code:
public boolean goodLine(Scanner scan) {
System.out.print("Which line do you want to write out (0-4)? ");
int lineNumber = scan.nextInt();
return lineNumber < 0 || lineNumber > 4;
}
And call it:
while(goodLine());
Consider also call it a badLine
because user input is wrong (not 0-4 values)
You are missing a line inside while loop
boolean goodLine = lineNumber < 0 || lineNumber > 4;
Consider refactor to a method to avoid duplicate code:
public boolean goodLine(Scanner scan) {
System.out.print("Which line do you want to write out (0-4)? ");
int lineNumber = scan.nextInt();
return lineNumber < 0 || lineNumber > 4;
}
And call it:
while(goodLine());
Consider also call it a badLine
because user input is wrong (not 0-4 values)
edited Nov 15 '18 at 9:04
answered Nov 15 '18 at 8:18
user7294900user7294900
22.9k113362
22.9k113362
Thank you. Perfect solution. I choosen the other one because it's shorter.
– Sirsemy
Nov 16 '18 at 13:40
add a comment |
Thank you. Perfect solution. I choosen the other one because it's shorter.
– Sirsemy
Nov 16 '18 at 13:40
Thank you. Perfect solution. I choosen the other one because it's shorter.
– Sirsemy
Nov 16 '18 at 13:40
Thank you. Perfect solution. I choosen the other one because it's shorter.
– Sirsemy
Nov 16 '18 at 13:40
add a comment |
Simplified code and easy to understand
import java.util.Scanner;
public class Post3 {
public static void main(String args) {
Scanner scan = new Scanner(System.in);
int fiveMatrix = {
{1, 4, 7, 5, 3}, {3, 7, 9, 10, 1}, {4, -3, 2, -4, 1}, {5, 9, 6, 4, 3}, {1, 2, 3, 4, 5},};
System.out.println("Which line do you want to write out (0-4)? ");
int input = -1;
while(true) {
System.out.println("input valid number between 0 to 4");
input = scan.nextInt();
if(input >= 0 && input <= 4) {
break;
}
}
System.out.println("input is "+input);
scan.close();
}
}
Close good. Thank you. Your solution brings a new idea. On the other hand doesn't handle the first data request. And if would handle, it'll be a code duplication what I wanted to avoid.
– Sirsemy
Nov 16 '18 at 13:14
It does handle. it will continue until user inputs a valid input! do you want to take two inputs?
– secret super star
Nov 16 '18 at 13:15
I wasn't precise in my wording. I need a 'System.out.println("Bad index.");' error code in the case of bad number (as it has inside the question) . And if I use inside the while loop, it'll been read out, whether the answer is good or not. That's why I need the data request before the loop.
– Sirsemy
Nov 16 '18 at 14:01
add a comment |
Simplified code and easy to understand
import java.util.Scanner;
public class Post3 {
public static void main(String args) {
Scanner scan = new Scanner(System.in);
int fiveMatrix = {
{1, 4, 7, 5, 3}, {3, 7, 9, 10, 1}, {4, -3, 2, -4, 1}, {5, 9, 6, 4, 3}, {1, 2, 3, 4, 5},};
System.out.println("Which line do you want to write out (0-4)? ");
int input = -1;
while(true) {
System.out.println("input valid number between 0 to 4");
input = scan.nextInt();
if(input >= 0 && input <= 4) {
break;
}
}
System.out.println("input is "+input);
scan.close();
}
}
Close good. Thank you. Your solution brings a new idea. On the other hand doesn't handle the first data request. And if would handle, it'll be a code duplication what I wanted to avoid.
– Sirsemy
Nov 16 '18 at 13:14
It does handle. it will continue until user inputs a valid input! do you want to take two inputs?
– secret super star
Nov 16 '18 at 13:15
I wasn't precise in my wording. I need a 'System.out.println("Bad index.");' error code in the case of bad number (as it has inside the question) . And if I use inside the while loop, it'll been read out, whether the answer is good or not. That's why I need the data request before the loop.
– Sirsemy
Nov 16 '18 at 14:01
add a comment |
Simplified code and easy to understand
import java.util.Scanner;
public class Post3 {
public static void main(String args) {
Scanner scan = new Scanner(System.in);
int fiveMatrix = {
{1, 4, 7, 5, 3}, {3, 7, 9, 10, 1}, {4, -3, 2, -4, 1}, {5, 9, 6, 4, 3}, {1, 2, 3, 4, 5},};
System.out.println("Which line do you want to write out (0-4)? ");
int input = -1;
while(true) {
System.out.println("input valid number between 0 to 4");
input = scan.nextInt();
if(input >= 0 && input <= 4) {
break;
}
}
System.out.println("input is "+input);
scan.close();
}
}
Simplified code and easy to understand
import java.util.Scanner;
public class Post3 {
public static void main(String args) {
Scanner scan = new Scanner(System.in);
int fiveMatrix = {
{1, 4, 7, 5, 3}, {3, 7, 9, 10, 1}, {4, -3, 2, -4, 1}, {5, 9, 6, 4, 3}, {1, 2, 3, 4, 5},};
System.out.println("Which line do you want to write out (0-4)? ");
int input = -1;
while(true) {
System.out.println("input valid number between 0 to 4");
input = scan.nextInt();
if(input >= 0 && input <= 4) {
break;
}
}
System.out.println("input is "+input);
scan.close();
}
}
answered Nov 15 '18 at 8:30
secret super starsecret super star
1,025115
1,025115
Close good. Thank you. Your solution brings a new idea. On the other hand doesn't handle the first data request. And if would handle, it'll be a code duplication what I wanted to avoid.
– Sirsemy
Nov 16 '18 at 13:14
It does handle. it will continue until user inputs a valid input! do you want to take two inputs?
– secret super star
Nov 16 '18 at 13:15
I wasn't precise in my wording. I need a 'System.out.println("Bad index.");' error code in the case of bad number (as it has inside the question) . And if I use inside the while loop, it'll been read out, whether the answer is good or not. That's why I need the data request before the loop.
– Sirsemy
Nov 16 '18 at 14:01
add a comment |
Close good. Thank you. Your solution brings a new idea. On the other hand doesn't handle the first data request. And if would handle, it'll be a code duplication what I wanted to avoid.
– Sirsemy
Nov 16 '18 at 13:14
It does handle. it will continue until user inputs a valid input! do you want to take two inputs?
– secret super star
Nov 16 '18 at 13:15
I wasn't precise in my wording. I need a 'System.out.println("Bad index.");' error code in the case of bad number (as it has inside the question) . And if I use inside the while loop, it'll been read out, whether the answer is good or not. That's why I need the data request before the loop.
– Sirsemy
Nov 16 '18 at 14:01
Close good. Thank you. Your solution brings a new idea. On the other hand doesn't handle the first data request. And if would handle, it'll be a code duplication what I wanted to avoid.
– Sirsemy
Nov 16 '18 at 13:14
Close good. Thank you. Your solution brings a new idea. On the other hand doesn't handle the first data request. And if would handle, it'll be a code duplication what I wanted to avoid.
– Sirsemy
Nov 16 '18 at 13:14
It does handle. it will continue until user inputs a valid input! do you want to take two inputs?
– secret super star
Nov 16 '18 at 13:15
It does handle. it will continue until user inputs a valid input! do you want to take two inputs?
– secret super star
Nov 16 '18 at 13:15
I wasn't precise in my wording. I need a 'System.out.println("Bad index.");' error code in the case of bad number (as it has inside the question) . And if I use inside the while loop, it'll been read out, whether the answer is good or not. That's why I need the data request before the loop.
– Sirsemy
Nov 16 '18 at 14:01
I wasn't precise in my wording. I need a 'System.out.println("Bad index.");' error code in the case of bad number (as it has inside the question) . And if I use inside the while loop, it'll been read out, whether the answer is good or not. That's why I need the data request before the loop.
– Sirsemy
Nov 16 '18 at 14:01
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53314987%2fcan-i-give-a-condition-to-boolean-primitive-as-initialize%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Can you explain exactly what "doesn't work"? It's hard to tell without an error message or stacktrace.
– dave
Nov 15 '18 at 8:16
Add
goodLine = lineNumber < 0 || lineNumber > 4;
insidewhile
loop to exit.– secret super star
Nov 15 '18 at 8:20
@secretsuperstar That looks like a simple solution, but it is conceptually wrong: you do not duplicate code, even when it is just a single line. Because you would then have to remember that you have to update each duplicate of your check when the condition needs to adapted. Duplicating code is always the first step to introduce future bugs.
– GhostCat
Nov 15 '18 at 8:28
@dave you're right. I didn't mentioned the error message. It went into an infinite loop. And later I removed the if condition but that also didn't help.
– Sirsemy
Nov 16 '18 at 11:26
1
@GhostCat thank you for your notes. I'm working on understand the answers. :) I'am totally a newbie. Of course I'm going to accept one of the solutions.
– Sirsemy
Nov 16 '18 at 11:31