So I'm trying to find whether array a is a subset of array b, and my code is as follows :`
public class subset {
public static boolean subset(int[]a, int[] b) {
for(int i=0;i<a.length;i++){
for(int j=0;i<a.length;j++){
if(a[i]==b[j]){
return true;
break;
}else{
return false;
}
}
}
}
public static void main(String[]args){
int[] a = {1,2,6};
int[] b = {1,2,4,3,7,4,8,5};
if (subset(a,b))
System.out.println("Array 1 is contained in Array 2");
else
System.out.println("Array 1 is not contained in Array 2");
}
}
And I am getting the following errors: unreachable statement and missing return statement.
What I want to do is that when the if condition is true it stops the inner for loop and carries on with the outer for loop.
Thanks in advance
2 things need to be fixed. You need to remove break, and add a return false at the end.
public static boolean subset(int[]a, int[] b) {
for(int i=0;i<a.length;i++){
for(int j=0;i<a.length;j++){
if(a[i]==b[j]){
return true;
//break; remove this
}else{
return false;
}
}
}
return false; //add this
}
Also, you could simplify it a bit, by doing this, instead of the if..else
return a[i]==b[j];
First problem is this:
return true;
break;}
the second line will never be executed because the first one terminates the method execution. Decide which line you want here: return a value or jump out of the inner loop (neither I presume, this branch should be empty, see below).
The second problem is that when you pass in an empty array the loops are not executed and the method does not have a value to return. Here simply add a "return true" (or false, quite arbitrary here) at the end of the method.
You probably can simplify the loop body in this way:
if(a[i]!=b[j]){
return false;
}
Related
trying to check if the letters of the word are sorted alphabetically.but i don't get any return from the method.
import java.util.Scanner;
public class A284 {
//Write a Java program to check
// if each letter of a given word (Abecadrian word) is less than the one before it.
public static boolean abecidarianWord(String word){
int index=word.length()-1;
for(int i=0;i<index;i++){
if (word.charAt(i)<=word.charAt(i+1)){
return true;
}else return false;
}
return true;
}
public static void main(String[] args) {
String entry;
System.out.println("input a word: ");
Scanner s1=new Scanner(System.in);
entry=s1.next();
abecidarianWord(entry);
}
}
You have two problems here.
First, you're not using the value returned from abecidarianWord, you're just calling it and ignoring the result, so you have no way of knowing what the method will return. So you should assign the return value to a variable and do something with it. For example, at the end of your main a naive implementation would do something like:
boolean isOrdered = abecidarianWord(entry);
if (isOrdered) {
System.out.println("String is ordered");
} else {
System.out.println("String is not ordered");
}
Second, in abecidarianWord you're returning immediately after the first iteration of the loop, which will only tell you if your condition holds true for the first two characters.
Instead you may want to return false as soon as you find a pair that doesn't respect the condition and return true if you reach the end of the loop without "accidents", so something like:
public static boolean abecidarianWord(String word) {
for (int i=0; i < word.length -1; i++) {
if (word.charAt(i) > word.charAt(i+1)) {
return false;
}
}
return true;
}
You have successfully returned the value.
import java.util.Scanner;
public class A284 {
public static boolean abecidarianWord(String word){
//you are getting length of "word" here
int index=word.length()-1;
for(int i=0;i<index;i++){
if (word.charAt(i)<=word.charAt(i+1)){
//If condition are correct return true.
return true;
}else{
//If condition are incorrect return false
return false;
}
}
return true;
}
public static void main(String[] args) {
String entry;
//Printing a text
System.out.println("input a word: ");
//getting values from user
Scanner s1=new Scanner(System.in);
entry=s1.next();
//calling a class
abecidarianWord(entry);
//You have get the value. But, you are actually trying to say that why it's not printing in output. When you return something you have to put them in another function to print-out
System.out.println(abecidarianWord(entry));
//If you don't wanna do it than you have to write SOUT instead of return. Than you can output the way you wrote
}
}
You are returning true on first comparison, so your loop only runs once. Instead, change your if condition inside for loop as below.
if (word.charAt(i)>word.charAt(i+1)){
return false;
}
#Istiak is perfectly right.
But just for optimizing your code to do what I think you would ideally want, I just wanted to say that the if statement -> if (word.charAt(i)<=word.charAt(i+1)) iterates for every two charecters in the word and you don't want to return true if just two letters are in order, Ideally replace return true; with just an empty ; otherwise your function will stop immediately after it finds a pair of two consecutive correctly placed letters.
I want only the while loop to start over if the if condition inside is true.
In my book this is achieved with breaking the loop, to let it start over.
Problem with this code is: The break does nothing but stopping the while loop. If the if condition is true, nothing gets repeated. The method returns the value of x and it is over.
How can I get the while loop repeat if the if condition is met, in this example?
Thank you :)
public int example() {
for() {
if() {
//code
}else{
z=0;
while (z==0) {
//code
if() {
//code
break;
}else{
z=1;
}
}
}
}
return x;
}
Here break; keyword is breaking the whole while loop if condition is true. And as the code has nothing else to execute after while loop has ended, the code returns 'X'.
I suppose that is not what you want.
You can either use continue keyword instead of break.
Or you can consider refactoring a method out of:
//code
if() {
//code
break;
}else{
z=1;
}
like below:
public boolean condtionalRepeat(){
//code
if() {
//code
return true;
}else{
return false;
}
}
and the code will look like:
public int example() {
for() {
if() {
//code
}else{
//code
while (condtionalRepeat()) {
//code
}
}
}
return x;
}
An additional comment on the original code: if this is accurate:
while (z==0) {
//code
if() {
//code
break;
}else{
z=1;
}
}
then it collapses to
//code
if() {
//code
}else{
z=1;
}
The loop never loops. The first time through (in the original code) either the 'if' condition is satisfied, or it is not satisfied.
If satisfied, we end with a 'break' out the loop.
If not satisfied, we set z to 1, then loop to evaluate the 'while' condition, which terminates the loop.
Either way the loop executes once only.
I wrote a piece of code which should be executed until the condition is satisfied. I have 2 classes using the same structure. In one of them while (true) loop executes as expected; in the other class the program exits the loop after the first recursion.
protected static boolean flag = true;
private static int value=0;
private static int limit=10;
.
.
.
public static int method(){
if (limit-value <=0)
{
...
}
else {
while(flag) {
if (limit-value > 0 ) {
*the action I want to perform until the condition is satisfied*
value++;
}
else if (limit==value)
{
flag = false;
}
return int_Value;
}
}
}
return int_Value;
}
I expect the while(true) loop to be executed until the condition is satisfied (which is more than once).
With some cleaned up indentation it becomes clear that the while loop contains an unconditional return.
if you look at your code the while loop goes like this
while(flag) {
if (limit-value > 0 ) {
*the action I want to perform until the condition is satisfied*
value++;
}
else if (limit==value)
{
flag = false;
}
return int_Value;
}
after executing either if or if else statement there is a return int_value statement which is causing the problem
Although I did debugging I couldn't see it in the first place. I performed another debug session after #user3437460 's suggestion, so I was able to find out:
It seems like an additional return statement was used!(return int_Value;),
the one after else if block. So, the program returns a value and never goes back into the loop.
After deleting the first return statement, program runs just fine.
I'm trying to make a boolean method, but it's not recognizing I have a return statement. What should I do?
public boolean isThreeKind( int rankHist[]) {
for (int i=0;i<=13;i++){
if (rankHist[i]>=3){
return true;
}else{
return false;
}
}
}
Your code does not make sense. There is no point in having a loop if you're always going to run the code inside the loop exactly once. I think you must have misunderstood. What are you trying to do?
Assuming the method only has to return true or false if it is greater than equal to 3,
would recommend to keep it simple.
Also, Please Note:
Loop through the i=0 to i< rankHist.length incase the array contains less than 13 elements you will encounter an ArrayOutOfBoundException.
If it contains more than 13 elements, the output might be incorrect.
.
public boolean isThreeKind( int rankHist[]) {
for (int i=0;i<rankHist.length;i++){
if (rankHist[i]>=3){
return true;
}
}
return false;
}
I think what he wants is if every element in the 13 are >=3, he should return a true, else ways he should return a false.
public boolean isThreeKind( int rankHist[])
{
for (int i=0;i<=13;i++)
{
if (rankHist[i]<3)
{
return false; // Will return false if either of the element have value <3
}
}
return true; // Will return true only if all the 13 elements have value >=3
}
This is literally what your code is doing right now:
public boolean isThreeKind( int rankHist[]) {
return rankHist[0] >= 3;
}
That is it, and I am assuming this is not what you are attempting to do. So if you tell us what you are actually trying to accomplish we can help you more.
In java, You always gotta keep track of returning something EVERYWHERE the method can exit. If it can exit without hitting a return statement, you'll see that error.
So, for your code, to modify it and make it see it, you would need to have it say:
public boolean isThreeKind( int rankHist[]) {
for (int i=0;i<=13;i++){
if (rankHist[i]>=3){
return true;
}else{
return false;
}
}
return false;
}
Or true, if you would rather that.
You should avoid multiple return statement.
You can use a flag. Declare the flag outside the for loop, then assign the value accordingly, and do not miss the break statement.
Do it like that:
public boolean isThreeKind( int rankHist[]) {
boolean value = false;
for (int i=0;i<=13;i++){
if (rankHist[i]>=3){
value = true;
break;
}
}
return value;
}
This is the question in Coding bat(the Java version)::
Given an array of ints, return true if the array contains a 2 next to a 2 somewhere.
has22({1, 2, 2}) → true
has22({1, 2, 1, 2}) → false
has22({2, 1, 2}) → false
This was my solution:
public boolean has22(int[] nums) {
for (int i=0; i<nums.length-1;i++) {
if (nums[i]==2 && nums[i+1]==2) {
return true;
}
else {
return false;
}
}
}
Doesn't compile while this does..
public boolean has22(int[] nums) {
for (int i=0; i<nums.length-1;i++) {
if (nums[i]==2 && nums[i+1]==2) {
return true;
}
else {
}
}
return false;
}
Sorry if this is a stupid question but I'm confused about the brackets at the end.
Imagine a case when your argument is empty or null. Your first method doesn't compile, because it doesn't return a boolean value for all cases.
Your second method compiles, because it will return a boolean value in any case, after the iteration is complete.
public boolean has22(int[] nums)
{
for (int i=0; i<nums.length-1;i++)
if (nums[i]==2 && nums[i+1]==2)
return true;
return false;
}
You can write your program like above no need of braces.
for each conditional statement one statement is always there if we want to associate more than one statement with any conditional statement then we provide {} braces for example
if(some condition)
stmt1;
no need of braces but if more than one statement then
if(some condition)
{
stmt1;
stmt2;
}
so always remember more than one statement braces compulsory and to avoid problem when ever open a bracket just close at the same time and write inside that one it will let you some relax.
Thanks
asif aftab