Try to search your question here, if you can't find : Ask Any Question Now ?

How to search string in one dimensional Array with lower and upper case is same/equivalent in Java?

HomeCategory: stackoverflowHow to search string in one dimensional Array with lower and upper case is same/equivalent in Java?
Avatarkundan asked 5 months ago

I have one dimensional array, i want user to input a name to search. the array contain names of Leon, Helena, Chris, Piers, Jake, Sherry, and Ada. i want to use if statement, that if user type leon or LEON instead of Leon and other, it will be true. im using uppercase and lowercase too.

i used if statement combined with || attribute

public class CariNama {
public static void main(String[] args) {
    String[] nama = {"Leon", "Helena", "Chris", "Piers", "Jake", "Sherry", "Ada"};
    String temp, tempchange, tempchange1;
    int i, proximator=0;
    Scanner keyboard = new Scanner(System.in);
    System.out.println("Insert RE 6 Characters name : ");
    temp=keyboard.nextLine();
    tempchange=temp.toLowerCase();
    tempchange1=temp.toUpperCase();
    for(i=0;i<nama.length;i++){
        if(nama[i].equals(temp)||nama[i].equals(tempchange)||nama[i].equals(tempchange1)){
            proximator = 1;
        }
    }
    if(proximator==1){
        System.out.println("Character "+temp+" Found!");
    }
    else{
        System.out.println("There is no "+temp);
    }
    System.out.println(tempchange); //to make sure if tempchange is lowered
    System.out.println(tempchange1); //to make sure if tempchange1 is upper-ed
}

}

but when i type LEON or leon it still said there is no LEON or leon
if i type Leon then it said Character Leon Found, how do i fix this help

1 Answers
Best Answer
AvatarFernando answered 5 months ago
Your Answer

1 + 14 =

Popular Tags

WP Facebook Auto Publish Powered By : XYZScripts.com