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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:30:49+00:00 2026-06-10T18:30:49+00:00

I am trying to determine if the user entered value already exists in the

  • 0

I am trying to determine if the user entered value already exists in the current array, how to do that?

User entered value to check the variable is
accno and the array to compare to is accnums

This is what i am currently working on

public class Randomtopic {

    static BigDecimal[] accbal = new BigDecimal[20];
    static Integer[] accnums = new Integer[20];

    public static void main(String[] args) {
        displayMenu();
    }

    public static void displayMenu() {
        int option, accno;
        double accbal;
        Scanner sc = new Scanner(System.in);
        System.out.println(" Add an account");
        System.out.println("Search an account with the given account number");
        System.out.print("Enter Your Choice: ");
        option = sc.nextInt();
        switch (option) {
            case 1:
                System.out.println("You have choosen to add account");
                addAccount();
                break;
            case 2:
                System.out.println("You have choosen to search for an account");
                System.out.print("Enter the Account Number: ");
                accno = sc.nextInt();
                System.out.println(search(accno, accnums));
                break;
            default:
                System.out.println("Please choose an appropriate option as displayed");
        }
        displayMenu();
    }

    public static void addAccount() {
        //String accno;
        int i = 0;
        int accno, input;
        BigDecimal accbala;
        DecimalFormat df = new DecimalFormat("0.00");
        //BigDecimal[] accbal= new BigDecimal[20];
        Scanner sc = new Scanner(System.in);
        //String[] accnums = new String[20];
        int j;
        System.out.print("Enter the account number: ");
        accno = sc.nextInt();
        if (String.valueOf(accno).matches("[0-9]{7}")) {
            System.out.print("Enter account balance: ");
            accbala = sc.nextBigDecimal();
            for (j = 0; j < accnums.length; j++) {
                if (accnums[j] == null )
             break;
         else if(accnums[j].equals(accno))
         {
              System.out.println("Account already exists");
         }
            }
            //System.out.print(j);
            if (j == accnums.length) {
                System.out.print("Account storage limit has reached.");

            } else {

                    accnums[j] = accno;
                    accbala = accbala.setScale(2, RoundingMode.HALF_UP);
                    accbal[j] = accbala;

            }
            input = accnums[0];
            System.out.println("The value: " + input + " witha a balance of " + accbal[0].toString());
        } else {
            System.out.println("Wrong NRIC");
        }
        displayMenu();
    }

    public static String search(int accnum, Integer[] numbers) {
        // Integer[] numbers;
        //int key;
        //numbers = accnums;
        // System.out.print("Enter the Account Number: ");        
        for (int index = 0; index < numbers.length; index++) {
            if (numbers[index].equals(accnum)) {
                return String.valueOf(index);
                // System.out.println("The account number exists in this array index :"+index);//We found it!!!
            }
            break;
        }
        return "-1";
    }
}

So my problem?
When i enter the accnum for the first time itself i am getting NullPointerException. Tks

  • 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-10T18:30:51+00:00Added an answer on June 10, 2026 at 6:30 pm

    When you instantiate or create an array, the values are by default set to null; whenever you iterate over an array of possibly null values, it is required you skip these. For example,

    for(loop conditions)
            if (numbers[index] != null && (numbers[index].equals(accnum))) {
                return String.valueOf(index);
                //your text goes here;
            }
            break;//I suggest you take this line out of your original code. Your for loop will end this once you have iterated through the entire array, or you will automatically break out if you find the value.
    }
    

    If you must use an array, then this is the best way to iterate through it. The parenthesis and the && in the if clause prevent you from performing the .equals check if the value of numbers[index] == null – in turn, preventing the null error from being thrown. your only alternative would be to set every value in the numbers[ ] to 0, or some other value, and than skip that value when you iterate. Such would be done with a static final int. However, this is not ideal coding.

    Ideally, you should use an ArrayList – you can then not only make this more efficient, but also, much more readable.

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

Sidebar

Related Questions

How to determine the url of the page that the user is trying to
I'm trying to get the current privacy status for a determine user's property. I'm
I'm trying to take a given URL entered by user and determine if the
I'm trying to determine the iPhone user's location using a CLLocationManager and CLGeocoder. My
Short version: I'm trying to determine the best way to track what the user
I'm trying to determine if the current system is 32 or 64 bit. And
I'm trying to determine if a user is a member of a group or
I am trying to determine if a user is a facebook fan. I load
I am trying to determine whether a string input by a user is valid
I am trying to figure out how to determine the user which triggered a

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.