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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:16:12+00:00 2026-06-18T06:16:12+00:00

Wrote a program to validate the amount of seats adjacent in a row. The

  • 0

Wrote a program to validate the amount of seats adjacent in a row. The seats are either booked or available, represented by 0 or 1. The program works for the most part. If the desired amount of seats in a row is available, it will output a message saying it. What is wrong is when the desired amount of seats is unavailable, or over 6. How do i fix this?

package javaapplication2;
import java.util.*;

public class JavaApplication2 {


    public static void main(String[] args) {
       Scanner input = new Scanner(System.in);
       System.out.println("Enter the amount of people in your group, up to 6");
       int num = input.nextInt();

       int highest = num - 1;

         String available = "";
         String booking = " ";       

       int[] RowA = {0,0,1,0,0,0,1,0,0,1};

        for (int i = 0; i < RowA.length; i++) {
             if (RowA[i] == 0) {
                 available = available + (i + 1);

             }

             if (available.length() > booking.length()) {
                 booking = available;

             }else if (RowA[i] == 1) {
                 available = "";

             }
         }

         char low = booking.charAt(0);
         char high = booking.charAt(highest);


        if (num <= booking.length()) {
            System.out.println("There are seats from " + low + " - " + high + ".");
            System.out.println(booking);
        }
        else {
            System.out.println("Sorry, the desired seat amount is not available. The maximum amount on Row is " + booking.length());

        }
    }
}
  • 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-18T06:16:13+00:00Added an answer on June 18, 2026 at 6:16 am

    First of all – add stacktrace to your question.
    Second – read stacktrace: it gives you a clue about what’s wrong with your code.
    Third – debugger is your best friend 🙂

    Actual exception is:

    Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 5
        at java.lang.String.charAt(String.java:686)
        at JavaApplication2.main(JavaApplication2.java:35)
    

    Line 35: char high = booking.charAt(highest);

    So the problem is that you’re trying to calculate high even if booking string is smaller than you need. You should move calculation of high and low inside if statement. This way you can be sure that booking is not shorter than you need:

    if (num <= booking.length()) {
        char low = booking.charAt(0);
        char high = booking.charAt(highest);
        System.out.println("There are seats from " + low + " - " + high + ".");
        System.out.println(booking);
    } else {
        System.out.println("Sorry, the desired seat amount is not available. The maximum amount on Row is " + booking.length());
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote this program: #include <stdio.h> /*Part B Write a program that: defines an
Yesterday I wrote a simple Python program (really simple as shown below) to validate
I wrote a program to determine the game tree for tic-tac-toe. I believe most
i wrote program to connect oracle database 11g for my android application to store
I wrote a program for downloading an image from web using AsyncTask in service
I wrote a program called Hello.py that looks like this: import pygame, sys from
I wrote a program to multiply, divide, add, and subtract fractions..I just can't figure
I wrote a program that forks some processes with fork(). I want to kill
I wrote a program to click on an application automatically at scheduled time using
I wrote a program in C++/OpenGL (using Dev-C++ compiler) for my calculus 2 class.

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.