javaloopswhile-loopsubroutine

The user must enter a string that is greater than 6 characters long


How can I code this using subroutines in java with a loop so if they don't get it right it keeps repeating till they do. This is all i have so far. The user must enter a string that is greater than 6 characters long.

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package containsmethod;

import java.util.*;  
import java.lang.String;  
import java.lang.Character;
import java.util.Scanner;
public class ContainsMethod {

    
   public static boolean isValid(String password) {

    if (password.length() < 6) {
    return false;
    } else {
    char c;
    for (int i = 0; i < password.length() -1; i ++) {
    c = password.charAt(i);
    if (!Character.isLetterOrDigit(c)) {
    return false;
    } else if (Character.isDigit(c)) { 
        return false;
            }
    }
    }
    return false;
   }

 
   
    
            
    public static void main (String [] args) {
    Scanner input = new Scanner (System.in);
    {
    System.out.print ("Please enter a string that is greater than 6 characters long. ");
    String password = input.next();
    if (isValid(password)) {
    System.out.println ("That is a valid string onto stage 2.");
    } 
    else 
    {
    System.out.println ("That is a invalid string. Try again.");
    }
    }

Solution

  • First of all, your isValid function never returns true, even the case that user enters a password that is greater than 6 characters. So you should update your method like that:

    public static boolean isValid(String password) {
            if (password.length() < 6) {
                return false;
            } else {
                char c;
                for (int i = 0; i < password.length() - 1; i++) {
                    c = password.charAt(i);
                    if (!Character.isLetterOrDigit(c)) {
                        return false;
                    } else if (Character.isDigit(c)) {
                        return false;
                    }
                }
            }
            return true;
        }
    

    Then, you can take the user input in a do-while loop. So if the password is invalid it will keep asking. Here is your updated main method:

    public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
            {
                String password;
                do {
                    System.out.print("Please enter a string that is greater than 6 characters long. ");
                    password = input.next();
                    if (isValid(password)) {
                        System.out.println("That is a valid string onto stage 2.");
                    } else {
                        System.out.println("That is a invalid string. Try again.");
                    }
                }while (!isValid(password));
            }
        }