Can you convert this program from JOptionpane to System.out.print? please import
ID: 3579577 • Letter: C
Question
Can you convert this program from JOptionpane to System.out.print? please
import javax.swing.JOptionPane;
import java.util.Scanner;
import java.io.*;
class Validator
{
//Array of valid numbers
private int[] valid;
public Validator() throws IOException
{
valid = new int[20];
String inputfile;
// Get a file name
inputfile = JOptionPane.showInputDialog("Enter Your File Name");
File file = new File(inputfile);
Scanner input = new Scanner(file);
int i=0;
while (input.hasNext())
{
// Read a value from the file
valid[i] = input.nextInt();
i++;
}
// close the file
input.close();
}
/**
* isValid method
* This method uses a sequential search to
* determine whether the argument appears in
* in the array of valid numbers.
*/
public boolean isValid(int number)
{
boolean found = false; // Flag
int index = 0; // Array index
while (!found && index < valid.length)
{
if (valid[index] == number)
found = true;
else
index++;
}
return found;
}
}
import javax.swing.JOptionPane;
import java.io.IOException;
public class ValidatorDemo
{
public static void main(String [] arg)
throws IOException
{
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
The below code replaced JOptionPane with System.out.println();
Also used Scanner class with method next() to read String
// Changed Program
import java.util.Scanner;
import java.io.*;
class Validator
{
//Array of valid numbers
private int[] valid;
public Validator() throws IOException
{
valid = new int[20];
String inputfile;
// Get a file name
System.out.println("Enter File Name:");
inputfile = new Scanner(System.in).next();
File file = new File(inputfile);
Scanner input = new Scanner(file);
int i=0;
while (input.hasNext())
{
// Read a value from the file
valid[i] = input.nextInt();
i++;
}
// close the file
input.close();
}
/**
* isValid method
* This method uses a sequential search to
* determine whether the argument appears in
* in the array of valid numbers.
*/
public boolean isValid(int number)
{
boolean found = false; // Flag
int index = 0; // Array index
while (!found && index < valid.length)
{
if (valid[index] == number)
found = true;
else
index++;
}
return found;
}
}
public class Sample
{
public static void main(String [] arg)throws IOException
{
String input;
int accountNumber; // Account number to validate
Validator val = new Validator ();
// Get a Charge account number
System.out.println("Enter your charge account Number: ");
input = new Scanner(System.in).next();
accountNumber=Integer.parseInt(input);
// Determine whether it is valid
if(val.isValid(accountNumber))
System.out.println("That's a valid account Number.");
else
System.out.println(" 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.