hello all. :)
my assignment is to write a program that accepts a charge account number and then determines whether the number is valid by comparing it to the numbers (listed in the array.)
most of my program is correct, except for the last part where it displays whether the number is valid or invalid.
i've figured out it's not going into the if statement, but i don't understand why it's doing that or how to change that.
any help would be greatly appreciated!
:D

import java.util.Scanner;
import java.util.ArrayList;

public class chargeaccount
{
    public static void main (String args[])
    {
        Scanner keyboard = new Scanner (System.in);
        int choice = 1;
        int choice1 = 1;
        int index = 0;
        int element = -1;
        int element1 = -1;
        int number = 0;
        int number1 = 0;
        boolean found = false;
        boolean found1 = false;
        int[] chargeaccounts = {5658845,4520125,7985122, 
                                8777541,8451277,1302850, 
                                8080152,4562555,5552012, 
                                5050552,7825877,1250255,
                                1005231,65452531,3852085,
                                7576651,7881200,4581002};
        
        
        do
        {
            
            
            System.out.println("Enter your charge account number:");
            number = keyboard.nextInt();
            
            while (!found1 && index < chargeaccounts.length)
            {
                if(number == chargeaccounts[index])
                {
                    found1 = true;
                    element1 = index;
                
                }
                index++; 
            }

            System.out.println(("Valid? ") + (found1));
            System.out.println("");
            System.out.println("Would you like to input another number?(Y(0)/N(1)");
            choice1 = keyboard.nextInt();
            number = 0;
            found1 = true;
        }
        while(choice1 == 0);
        
      
        
        
    }
    
}

Recommended Answers

All 7 Replies

hello all. :)
my assignment is to write a program that accepts a charge account number and then determines whether the number is valid by comparing it to the numbers (listed in the array.)
most of my program is correct, except for the last part where it displays whether the number is valid or invalid.
i've figured out it's not going into the if statement, but i don't understand why it's doing that or how to change that.
any help would be greatly appreciated!
:D

import java.util.Scanner;
import java.util.ArrayList;

public class chargeaccount
{
    public static void main (String args[])
    {
        Scanner keyboard = new Scanner (System.in);
        int choice = 1;
        int choice1 = 1;
        int index = 0;
        int element = -1;
        int element1 = -1;
        int number = 0;
        int number1 = 0;
        boolean found = false;
        boolean found1 = false;
        int[] chargeaccounts = {5658845,4520125,7985122, 
                                8777541,8451277,1302850, 
                                8080152,4562555,5552012, 
                                5050552,7825877,1250255,
                                1005231,65452531,3852085,
                                7576651,7881200,4581002};
        
        
        do
        {
            
            
            System.out.println("Enter your charge account number:");
            number = keyboard.nextInt();
            
            while (!found1 && index < chargeaccounts.length)
            {
                if(number == chargeaccounts[index])
                {
                    found1 = true;
                    element1 = index;
                
                }
                index++; 
            }

            System.out.println(("Valid? ") + (found1));
            System.out.println("");
            System.out.println("Would you like to input another number?(Y(0)/N(1)");
            choice1 = keyboard.nextInt();
            number = 0;
            found1 = true;
        }
        while(choice1 == 0);
        
      
        
        
    }
    
}

actually I don't see any problem with your code, tell me what's exactly the problem?

for example:
after the program displays "enter your charge account number", if i type in the number 5658845 as my first number, the value returns true.
(which is correct.)

then it asks "would you like to input another number (Y(0)/N(1))?"
i input 0, because i want to enter another number.
i then type in some random number not listed in the array, like 36.

the value returns true, while it is actually false because it is not one of the numbers in the array.

the only time the program works is with the FIRST number inputted.
:/

take a look at the found1 variable, at first it is false, then when you enter a number that can be located in the array it becomes true

found1 = true;

but then you do not set it to false again before checking for the second number. Then your while loop checks if found1 is false, sicne it is true !found1 returns false and the control would never enter the loop again.

This is what the first impression I get from your program where you might be going wrong, if thats isn't it, let me know.

take a look at the found1 variable, at first it is false, then when you enter a number that can be located in the array it becomes true

found1 = true;

but then you do not set it to false again before checking for the second number. Then your while loop checks if found1 is false, sicne it is true !found1 returns false and the control would never enter the loop again.

This is what the first impression I get from your program where you might be going wrong, if thats isn't it, let me know.

yes. verruckt2 is so correct, this is the error, check the condition in the if statement.

i tried resetting it by putting "found1 = false;" inside the do loop to reset it like you guys suggested, right after:
System.out.println("Enter your charge account number:");
number = keyboard.nextInt();

but i didn't get any difference.
i did a System.out.print to see if it was going in the if loop, and as usual it still went in the first time but not the second.
i'm not sure if i'm putting it in the correct place, but i would imagine so as that's where the whole loop starts over.

There's one more thing, that you have been forgetting and which I too skipped in my first post. Should you not reset the index too to zero so that the entire array is scanned once again. What say ?

it works!!!
thanks so much!
:icon_smile:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.