简体   繁体   中英

can't get a return from a boolean method in java

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM