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 8644751
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:22:36+00:00 2026-06-12T12:22:36+00:00

I have some logic (java.lang.ArrayIndexOutOfBoundsException) error in this part of my code for some

  • 0

I have some logic (java.lang.ArrayIndexOutOfBoundsException) error in this part of my code for some reason. I want the user to input anything and have it be split and parsed into an integer. If the user fails to do so ask them again. And if they enter something like “g5 3 76h 23” have the program accept it as 5 3. Or if i can have the program deny this until the user only enters two numbers between 0 and 9 separated by a space, that would be fine aswell. The user also has the option of enetering a “q” to quit.
However, everytime i run it, it appears as if nothing was split into a new array. and I get the error.

/** 
 * Prompts the user for input
 */
public void promptUser() {

// a Scanner object that uses System.in for input.

    Scanner scan = new Scanner(System.in);

// a prompt for the user, asking them for input.

    System.out.print("Pick a coordinate [row col] or press [q] to quit: ");

//         Get input from the user, checking for errors. If the input is
//         correct (e.g., two numbers that in bounds and have not 
//         already been clicked), then call the click method for desired 
//         coordinates. If the user wants to quit the game then make sure 
//         to update the boolean state variable and print out a message.

    String input = scan.next();
    String del = "[\\s,;\\n\\t]+"; // these are my delimiters
    String[] token = input.split(del); // here i will save tokens

    int val0 = 11, val1 = 11;
    boolean tf = true;
    while(tf)
    {
    if(token[0] == "q")
        {
        isRunning = false;
                    System.out.println("Thank you for playing");
        }
    else
        {
        try
            {
            val0 = Integer.parseInt(token[0], 10);
            }
        catch (NumberFormatException nfe)
            {
            // invalid data - set to impossible
            val0 = 11;
            }
        try
            {
            val1 = Integer.parseInt(token[1], 10);
            }
        catch (NumberFormatException nfe)
            {
            // invalid data - set to impossible
            val1 = 11;
            }
        }
    if( !(((val0 >= 0) && (val0 < rows)) && ((val1 >= 0) && (val1 < cols))) )
        {
        System.out.println("Input Invalid, pick a coordinate [row col] or press [q] to quit: ");
        input = scan.next();
        for(int i=0;i<2;i++)
            {
            token = input.split(del);
            }
                }
            else if(false) //atm
                {

                }
            else
        {
                    tf = false;


                }
    click(val0, val1);
    } //while loop
} // promptUser
  • 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-12T12:22:37+00:00Added an answer on June 12, 2026 at 12:22 pm

    From the Scanner documentation:

    A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace.

    You could change it to:

    scan.useDelimiter(del); // If you want to split on more than just whitespace
    while(scan.hasNext()) {
        String input = scan.next();
    
        if("q".equals(input)) {
            System.out.println("Thank you for playing");
            return;
        }
        // etc. Put in a list or array for use later.
    }
    

    Remember that Strings are objects so == only returns true if both strings are the same object, not if they have the same value. Use .equals for value comparison.

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

Sidebar

Related Questions

In XCode 4.2, I have some logic tests that I want to exercise against
I have a list view and depending on some logic I want to temporary
I have a java application which doing some logic... ... But the java application
I have some code that's currently giving me an error, because recur can only
I have realized some logic using integers import java.util.concurrent.locks.*; public class MassageSalon implements Salon{
I have some events in my model and some handling logic. I want organize
I have some logic in a method that operates on a specified type and
I have some logic that depends upon two properties being set, as it executes
I have some logic in before_save whereby (only) when some conditions are met I
So here is the problem: I have some logic in my application that will

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.