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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:34:11+00:00 2026-05-25T11:34:11+00:00

public int Remove(int i, Briefcase c[], String[] m) { int nChoice = 0; boolean

  • 0
public int Remove(int i, Briefcase c[], String[] m) {

        int nChoice = 0;
        boolean inputisok = false;

        while (inputisok == false) {
            System.out.print("\tPlease remove " + i + " cases: ");
            nChoice = input.nextInt();
            if (c[nChoice] == null || nChoice < 0 && nChoice >= c.length) {
                System.out.println();
                System.out.println("\tInvalid Input please Try again\n");
            } else {
                System.out.println("\tI'm " + m[nChoice]
                        + " You just removed case # " + nChoice);
                System.out.println("\t|" + nChoice + "| contains $"
                        + c[nChoice].getAmount() + "\n");
                inputisok = true;
            }
        }
        return nChoice;
    }

my problem here is that when I enter a letter and a -negative number, or a number that is higher than 27, I always get an exception error, how do I fix that?

  • 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-05-25T11:34:12+00:00Added an answer on May 25, 2026 at 11:34 am

    The following line is incorrect:

    if (c[nChoice] == null || nChoice < 0 && nChoice >= c.length) {
    

    You want to change it like so:

    if (nChoice < 0 || nChoice >= c.length || c[nChoice] == null) {
    

    There are two changes: (1) the && became a ||; (2) the clauses have been reordered.

    (1) The && is wrong as nChoice < 0 && nChoice >= c.length always evaluates to false, since nChoice can’t be simultaneously less than zero and greater than c.length (Thanks, @Aleks G!)

    (2) In your original version you try to access c[nChoice] before making sure nChoice is within the bounds of c. If it isn’t, this’ll result in an ArrayIndexOutOfBoundsException instead of printing out “Invalid Input”.

    Short-circuit evaluation is the reason the ordering of clauses matters.

    Lastly, before reading from input, you could call hasNextInt() to make sure that the next token can be interpreted as a valid integer.

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

Sidebar

Related Questions

Assuming public class MyClass { public int ID {get; set; } public string Name
Let's I have a method to remove duplicates in an integer Array public int[]
public class Request { public string ID { get; internal set; } public int
struct mydata { public int id; public string data; } class Program { static
public class MyClass { public int Age; public int ID; } public void MyMethod()
[DataMember] public int? NumberOfPages; //////////// Is this supported???? [DataMember] public bool? Color; //////////// Is
public class JsonCategoriesDisplay { public JsonCategoriesDisplay() { } public int CategoryID { set; get;
This compiles: class Ex1 { public int show() { try { int a=10/10; return
class Foo { public int A { get; set; } } class Program {
Given the following class public class Foo { public int FooId { get; set;

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.