Osman
Osman

Reputation: 1771

Java issue with scanner

I have a method that should scan for one of 3 letters in caps or lowercase and return the lower case version of the letter. If an improper letter is entered the user is warned and reasked for a letter. I have two issues, 1: as soon as the method is run I get the outputted line with the error message telling the user invalid entry without waiting for an entry! (so the second the method is run I see High, low or sevens (H/L/S):Invalid entry. Please try again using H/L/S! before entering anything then the method is recalled again and all works fine form there except for my next issue) 2: the entry that is gotten from the scanner never passes any of my if statements even though it should.

my code:

private static char getHighLow(Scanner inScanner) {
    System.out.print("High, low or sevens (H/L/S):");
    String entered = inScanner.nextLine();
    System.out.print(entered);
    if(entered.equals("H") || entered.equals("h")){
        return 'h';
    }
    else if (entered.equals("L") || entered.equals("l")){
        return 'l';
    }
    else if(entered.equals("S") || entered.equals("s")){
        return 's';
    }

        char result = 0;
        while(result != 'l' || result != 'h' || result != 's'){
        System.out.println("Invalid entry. Please try again using H/L/S!");
        result=getHighLow(inScanner);
    }
        return result;
}

Upvotes: 1

Views: 308

Answers (5)

Gunjan Shah
Gunjan Shah

Reputation: 5168

This is what you want. Here is the program to iterate over characters in a String. And convert them in lower case letter if they are H,L OR S.

package testproj;

import java.util.Scanner;

public class TestProj {

    public static void main(String[] args) {
        Scanner scanner = new Scanner("HLs");
        String result = getHighLow(scanner);
        System.out.println("Result :"+result);
    }

    private static String getHighLow(Scanner inScanner) {
        System.out.println("High, low or sevens (H/L/S):");

        String entered;
        String result = "";
        boolean isCharFound = false;
        String temp = "";
        while (inScanner.hasNext()) {
            temp = inScanner.next();

            System.out.println(temp);

            for (int index = 0; index < temp.length(); index++) {
                entered =new Character(temp.charAt(index)).toString() ;


                if (entered.equals("H") || entered.equals("h")) {
                    result = result + 'h';
                    isCharFound = true;
                } else if (entered.equals("L") || entered.equals("l")) {
                    result = result + 'l';
                    isCharFound = true;
                } else if (entered.equals("S") || entered.equals("s")) {
                    result = result + 's';
                    isCharFound = true;
                }

                if (!isCharFound) {
                    System.out.println("Invalid entry. Please try again using H/L/S!");
                }

                isCharFound = false;
            }


        }

        return result;
    }
}

Upvotes: 0

Amit Deshpande
Amit Deshpande

Reputation: 19185

You can simply use equalsIgnoreCase and trim the entered string. And add a while loop util your condition is satisfied.

    Scanner scanner = new Scanner(System.in);
    boolean loop = true;
    String choice = null;
    while (loop) {
        System.out.print("High, low or sevens (H/L/S):");
        choice = scanner.nextLine();
        if ("H".equalsIgnoreCase(choice.trim())
                || "L".equalsIgnoreCase(choice.trim())
                || "S".equalsIgnoreCase(choice.trim())) {
            System.out.println("Correct Choice");
            loop = false;
        }
        else
        {
            System.out.println("Wrong Choice");
        }
    }
    System.out.print(choice);

Upvotes: 2

k_ssup
k_ssup

Reputation: 348

Instead of using while(), you can use 'else' like this-

private static char getHighLow(Scanner inScanner) {
    System.out.print("High, low or sevens (H/L/S):");
    String entered = inScanner.nextLine();
    System.out.print(entered);
    if(entered.equals("H") || entered.equals("h")){
        return 'h';
    }
    else if (entered.equals("L") || entered.equals("l")){
        return 'l';
    }
    else if(entered.equals("S") || entered.equals("s")){
        return 's';
    }
    else {
        System.out.println("Invalid entry. Please try again using H/L/S!");
        return getHighLow(inScanner);
    }   
}

Upvotes: 2

sakthisundar
sakthisundar

Reputation: 3288

Hey you are not breaking out of the while loop at all. Did you see that ?

Upvotes: 0

Subhrajyoti Majumder
Subhrajyoti Majumder

Reputation: 41200

char result; 
while(true){
    System.out.print("High, low or sevens (H/L/S):");
    String entered = inScanner.nextLine();
    System.out.print(entered);
    if(entered.equals("H") || entered.equals("h")){
        result = 'h';break;
    }
    else if (entered.equals("L") || entered.equals("l")){
        result = 'l';break;
    }
    else if(entered.equals("S") || entered.equals("s")){
        result = 's';break;
    }else{
        System.out.println("Invalid entry. Please try again using H/L/S!");
    }
}

Upvotes: 1

Related Questions