I\'m in an intro Java class I am getting errors upon compiling my program. The p
ID: 3823081 • Letter: I
Question
I'm in an intro Java class I am getting errors upon compiling my program. The problem is as follows. Create a class with a method that accepts a charge ccount number as its argument. The method should determine whether the number is valid by comparing it to list of numbers (provided in code below). These numbers should be stored in an array or an ArrayList object. Use a sequential search to locate the number passed as an argument. If the number is in the array, the method should return true, indicating the number is valid. If the number is not in the array, the mehtod should return false, indicating the number is invalid.
Please correct my error(s) (and forgive me if it is obvious, I am just learning lol)
My sample code:
import javax.swing.JOptionPane;
class Validator
{
//array of valid numbers
private int [] valid=
{
5658845, 4520125, 7895122, 8777541, 8451277, 1302850,
8080152, 4562555, 5552012, 5050552, 7825877, 1250255,
1005231, 6545231, 3852085, 7576651, 7881200, 4581002};
/*the is valid method below uses squential ssearch to
determine if number appears in valid number list*/
public boolean isValid(int number)
{
boolean found =false; //search flag
int index=0; //array index
//search the valid array
while(!found && index< valid.length)
{
if(valid[index] ==number)
found=true;
else
index++;
}
//return the status of the search
return found;
}
}
public class validatorTest
{
public static void main(String []arg)
{
String input;
int accountNumber; //account number to validate
Validator val=new Validator();
//get a charge account number
input=JOptionPane.showInputDialog("Enter your charge account Number:");
accountNumber=Integer.parseInt(input);
//determine whether it is valid
if(val.isValid(accountNumber))
JOptionPane.showMessageDialog(null, "That's a valid account number.");
else
JOptionPane.showMessageDialog(null, "That's an invalid account number.");
System exit(0);
}
}
Explanation / Answer
HI, There was small silly mistake.
I have fixed.
Program is working fine.
import javax.swing.JOptionPane;
class Validator
{
//array of valid numbers
private int [] valid=
{
5658845, 4520125, 7895122, 8777541, 8451277, 1302850,
8080152, 4562555, 5552012, 5050552, 7825877, 1250255,
1005231, 6545231, 3852085, 7576651, 7881200, 4581002};
/*the is valid method below uses squential ssearch to
determine if number appears in valid number list*/
public boolean isValid(int number)
{
boolean found =false; //search flag
int index=0; //array index
//search the valid array
while(!found && index< valid.length)
{
if(valid[index] ==number)
found=true;
else
index++;
}
//return the status of the search
return found;
}
}
public class validatorTest
{
public static void main(String []arg)
{
String input;
int accountNumber; //account number to validate
Validator val=new Validator();
//get a charge account number
input=JOptionPane.showInputDialog("Enter your charge account Number:");
accountNumber=Integer.parseInt(input);
//determine whether it is valid
if(val.isValid(accountNumber))
JOptionPane.showMessageDialog(null, "That's a valid account number.");
else
JOptionPane.showMessageDialog(null, "That's an invalid account number.");
System.exit(0);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.