Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8606903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T03:12:40+00:00 2026-06-12T03:12:40+00:00

This program is suppose to accept input into a string, the password. The program

  • 0

This program is suppose to accept input into a string, the password. The program then checks to make sure that the password is at least 8 characters in length, contains at least 2 digits, and only contains letters and numbers. I can’t seem to get the correct way to count at least 2 digits, although from what i’ve seen this method should work.

This is the error I get
Exception in thread “main” java.lang.StringIndexOutOfBoundsException: String index out of range: 10
at java.lang.String.charAt(String.java:658)
at Password.main(Password.java:35)

import java.util.*;


public class Password{
    public static void main(String[] args){
    Scanner input = new Scanner(System.in);
    String s;
    int numbers = 0;

    System.out.println("Enter a password (Must contain only letters and numbers, ");
    System.out.print("minimum of 8 characters with atleast two numbers): ");    
    s = input.nextLine();

    while(1==1){
    if (s.length() < 8){
        System.out.println("Password is too short");
        System.out.println("Enter correctly formatted password");
        s = input.nextLine();
        continue;
        }

    if (s.matches("^[a-zA-Z0-9_]+$")){  
    }
    else{
        System.out.println("Password may only contain Letters and Digits");
        System.out.println("Enter correctly formatted password");
        s = input.nextLine();
        continue;
        }



    int i;
    for (i = 0; i <= s.length(); i++){
        if (Character.isDigit(s.charAt(i))){
            numbers++;
            }
            }

    if (numbers < 2){
        System.out.println("Password must contain atleast 2 digits");
        System.out.println("Enter correctly formatted password");
        s = input.nextLine();
        continue;
        }
        break;
        }


    while (0==0){   
    System.out.println("Reenter Password to see if it matches");
        String a = input.nextLine();

    if (s.equals(a)){
        System.out.println("Password matches!");
        break;
        }
    else{
        System.out.println("Password does not match");
        continue;
    }
    }
    }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T03:12:41+00:00Added an answer on June 12, 2026 at 3:12 am

    In your for loop, you should not allow i to equal s.length()

    for (i = 0; i <= s.length(); i++)
    

    ArrayIndexOutOfBoundsException is caused when you try to access s[s.length()].

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This program is suppose to accept a number from the user and print that
#include<stdlib.h> #include<stdio.h> #include<string.h> //This program is a sorting application that reads a sequence of
Say suppose I am running a java program through command line. And this program
I have a code at the top of my program that looks like this:
Suppose someone makes a program to play chess, or solve sudoku. In this kind
Suppose I have a text file that says: This file has plain text Now,
I am suppose to be creating a memory map of this program for my
I'm making a program that makes a fractal, and the user is suppose to
I need to write a program that gets a string from the user and
I'm reading a book to learn Objective-C and this program is suppose to show

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.