This Method tests if the accountnumber is in the given array
public boolean containsAccount(int accountNumber) {
int i;
boolean ausgabe = false;
for (i = 0; i < allAccounts.length; i++) {
if (allAccounts[i].getAccountNumber() == accountNumber) {
ausgabe = true;
}
else if (i == length() - 1) {
ausgabe = false;
}
}
return ausgabe;
}
Expected to return true or false but it seems to be a neverending loop which retirns nothing.
Try this
public boolean containsAccount(int accountNumber) {
for (int i = 0; i < allAccounts.length; i++) {
if (allAccounts[i].getAccountNumber() == accountNumber) {
return true;
}
}
// If the execution flow reaches this line, then that
// means that the account 'id' does not exist in the array
return false;
}
Much cleaner, less variables & makes it easy to detect the real problem in your code. (Could be something with the allAccounts array, or the accountNumber)
Related
I do not understand what I should return. My method returns false if the last time it goes
through the for-loop it is false. If the last time is true than it returns true. But I want it to return false regardless of where the false occurred.
public class test {
public static void main(String[] args) {
int number = 4;
int[] intArray = {4, 8, 12, 16};
System.out.println(allMultipleOf(intArray, number));
}
public static boolean allMultipleOf(int[] ary, int n){
boolean a = true;
for(int i = 0; i < ary.length; i++){
if(ary[i] % n == 0){
a = true;
//System.out.println(a);
break;
} else {
a = false;
}
}
}
return a; //what should I return
}
You can return false early from the method, if we reached the end without returning false, then return true:
public static boolean allMultipleOf(int[] ary, int n) {
for (int i = 0; i < ary.length; i++) {
if (ary[i] % n != 0) {
return false;
}
}
return true;
}
It should return false, the default of the boolean is print at this place.
You can make the return statement within the method. it return true.
public static boolean allMultipleOf(int[] ary, int n) {
boolean a = true;
for(int i = 0; i < ary.length; i++) {
if (ary[i] % n == 0) {
a = true;
//System.out.println(a);
break;
} else {
a = false;
}
}
return a;
}
So here's my code, I want the output to be like this:
Given two numbers, is the second input a multiple of the first?
For Example:
Input:
3
6
Output:
true
public boolean multiple(int m, int n){
int i = 0;
int j = 0;
boolean check = true;
if(n%m == 0){
i++;
return check;
}
else{
j++;
return false;
}
}
When I try it I get an error, I think it's because the return statement is within the if and else statements.
The code is perfectly fine .. Error must be Somewhere else
public class Test1 {
public static void main(String[] args) {
System.out.println(multiple(3, 9));
}
public static boolean multiple(int m, int n){
int i = 0;
int j = 0;
boolean check = true;
if(n%m == 0){
i++;
return check;
}
else{
j++;
return false;
}
}
}
Output
true
here is output see IDEONE
The easiest way is to return the result of your if statement.
return n % m == 0;
I'm not sure what i/j are doing. You don't use them except to increment, but they are local to the function and get GC'd after the return. What you have now is basically this:
boolean bool = some_calculation();
if (bool == true)
{
return true;
}
else
{
return false;
}
Heres my code:
public int getPieceCoords(int entityId) {
for(int x = 0;x<8;x++) {
for(int y=0;y<8;y++) {
if(position[x][y] == entityId){
return position[x][y];
}else{
return (int)null;
}
}
}
//Need a return here, but it overrides the returns above
}
So, I am having trouble getting this to return the actual output I want, help is appreciated!
public int getPieceCoords(int entityId) {
int temp = 0;
for(int x = 0;x<8;x++) {
for(int y=0;y<8;y++) {
if(position[x][y] == entityId){
temp = position[x][y];
}
}
}
return temp;
}
Try this ::
public int getPieceCoords(int entityId)
{
int result = -1;
for(int x = 0;x<8;x++)
{
for(int y=0;y<8;y++)
{
if(position[x][y] == entityId)
{
result = position[x][y];
break;
}
}
}
return result;
}
You have specified to return int[][] type in your method but,
return position[x][y];
is returning an int , you might want to check it.
Edit: According to your new(edited) code, you are returning the same value as of parameter
entityId
in case of match or 0 if there is no match.
This doesn't make much sense, why not to use Boolean as return type? i.e return true on match or false otherwise?
public boolean isNumber(String t) {
for (int i = 0, i<= 9, i++) {
if t.equals(i) {
return true;
}
}
return false;
}
Copypastad the wrong method originally -_-
I have this inside a class compiling with this error:
data_structures/ExpressionEvaluator.java:40: illegal start of type for (int i = 0, i< 10, i++) {
Use semi-colons instead of commas.
for(int i = 0; i < 10; i++) {
//do stuff
}
You should use semi-colon and your if should be surrounded with brackets.
public boolean isNumber(String t) {
for (int i = 0; i <= 9; i++) {
if (t.equals(i)) {
return true;
}
}
return false;
}
I would suggest reading Language Basics
Semicolons separate the qualities of a for loop. Also, the condition of your if block must be surrounded by parentheses.
public boolean isNumber(String t) {
for (int i = 0; i <= 9; i++) {
if (t.equals(i)) {
return true;
}
}
return false;
}
public boolean isNumber(String t) {
for (int i = 0; i<= 9; i++) {
if( t.equals(i) ){
return true;
}
}
return false;
}
1 . use ";" replace of ","
2 .
if(boolean) {
//do stuff
}
Your method only verifies if the String that you passed is a digit, not a number (a number can have more than one digit). You could verifiy it only using a char and calling the ,Character.isDigit
char c = '1';
boolean isDigit = Character.isDigit(c);
If you really want to create your own method, passing a String param, I suggest you to modify like this:
public boolean isDigit(String t) {
return t.length() == 1 && Character.isDigit(t.charAt(0));
}
This is my below mehod for Validation .
public boolean validateData(Bagform[] bagdata) {
boolean flag = false;
int length = bagdata.length;
if (length == 2) {
for (int i = 0; i < bagdata.length; i++) {
if (bagdata[i].getCallType() == null) {
flag = true;
}
}
}
else {
flag = true;
}
return flag;
}
In this basically i am checking that if the getCallType() is not null for any of the values in the array .
If its not null , then its a valid data , so i am setting the flag to true .
(Please see the code above )
But for me , i am getting the flag value still as false ( Even though the getCallype() is not null )
Please help me .
You're setting the flag to true if the call type is null. I suspect you want:
public boolean validateData(Bagform[] bagdata) {
boolean flag = true;
int length = bagdata.length;
if (length == 2) {
for (int i = 0; i < bagdata.length; i++) {
if (bagdata[i].getCallType() == null) {
flag = false;
}
}
}
return flag;
}
It's not clear why you're only performing this validation when there are exactly two entries in the array though. Unless that's really deliberate, I'd have written this has:
public boolean validateData(Bagform[] bagdata) {
for (Bagform form : bagdata) {
if (form.getCallType() == null) {
return false;
}
}
return true;
}