shrawberry
shrawberry

Reputation: 3

How do I fix my java method to correctly iterate through string?

I need some major help.

My assignment is to recognize a lowercase string and return a true or false statement despite the presence of other characters or words. So far, I'm able to recognize the string in all lowercase, but my code still returns a TRUE value if the word is all uppercase; I only want it to recognize the lowercase values.

The assignment: Write a program that takes in a string line and prints true if letters of my name (“john”) have appeared in the string in the same order, all in lowercase. Please note that there might be other characters between the letters of my name.

Here's my code:

import java.util.Scanner;

class Main {

    public static void main(String[] args) {
        boolean output = Check();
        if (output == true) {
            System.out.println("true");
        }

        else if (output == false) {
            System.out.println("false");
        }
    }

    public static boolean Check() {
        String random;
        Scanner sc = new Scanner(System.in);
        random = sc.nextLine();
        String word = "john";

        for (int i = 0; i < word.length(); i++) {
            if (random.contains(word.substring((i)))) {
                return true;
            }
        }
        if (random.contains("JOHN")) {
            return false;
        }
        return false;
    }
}

Any help would be great thanks. Some sample outputs:

Sample Input 1: hello John Sample Output 1: false

Sample Input 2: j123o23h56n Sample Output 2: true

Sample Input 3: joh'n Sample Output 3: true

Sample Input 4: ho0jn Sample Output 4: false

Sample Input 5: J2j#@oh123$NNNnn Sample Output 5: true

Upvotes: 0

Views: 57

Answers (2)

Pushpesh Kumar Rajwanshi
Pushpesh Kumar Rajwanshi

Reputation: 18357

You need to write a logic where you pick each char from "john" string and compare it in order whether all of them occurred in that order or not. The moment it finds all the characters are found in the input string, it immediately return true without the need to further scan the input string. You may write something like this,

public static boolean Check() {
    String random;
    Scanner sc = new Scanner(System.in);
    random = sc.nextLine();
    String word = "john";
    sc.close();

    int findIndex = 0;
    char findChar = word.charAt(findIndex);
    for (char c : random.toCharArray()) {
        if (findChar == c) {
            findIndex++;
            if (findIndex == word.length()) {
                return true;
            }
            findChar = word.charAt(findIndex);
        }
    }
    return false;
}

Upvotes: 2

mettleap
mettleap

Reputation: 1410

You can simply do this (I didn't understand your condition for uppercase though; this Check method can find whether j,o,h,n appear in the string in the proper order),

  public static boolean Check(){
     String random;
     Scanner sc = new Scanner(System.in);
     random = sc.nextLine();


     HashMap<String, Boolean> map  =new HashMap<String, Boolean>();
     for(int i=0;i<random.length();i++){
         if(map.size() == 0){
             //find j
             if(random.charAt(i) == 'j'){
                 map.put("j", true);
             }
         }else if(map.containsKey("j") && map.size() == 1){
             //find o
             if(random.charAt(i) == 'o'){
                 map.put("o", true);
             }
         }else if(map.containsKey("o")&& map.size() == 2){
             //find h
             if(random.charAt(i) == 'h'){
                 map.put("h", true);
             }
         }else if(map.containsKey("h")&& map.size() == 3){
             //find n
             if(random.charAt(i) == 'n'){
                 map.put("n", true);
             }
         }
     }
   return map.size() == 4;
}

Alternatively, you can use a stack to solve this too,

public static boolean Check(){
     String random;
     Scanner sc = new Scanner(System.in);
     random = sc.nextLine();


     ArrayDeque<Character> stack = new ArrayDeque<Character>();
     stack.push('n');
     stack.push('h');
     stack.push('o');
     stack.push('j');
     for(int i=0;i<random.length();i++){
       if(random.charAt(i) == stack.peek()){
         stack.pop();
       }

       if(stack.size() == 0){
         return true;
       }

     }

     return false;
}

Upvotes: 0

Related Questions