dimanche 24 mars 2019

How to let JOptionPane accept integer but not String?

I am trying to add validation to my code (as my brother puts it) and that it should only accept numbers and not letters as it moves through the array. I have tried different methods but it still accepts letters and if it doesn't it crashes at array[1]. here's the part of my code:

public static void main(String[]Terminal) {
String Choice;
char response1;
String response = null;
String Display = null;
String TryAgain = null;
String Element = null;
boolean Validation = true;

int numberOfElements = 5; //array element numbers
int index;
int Choice1;

int[] Array = new int[numberOfElements]; //array

do {    // Rewind Option
    do {
for (index = 0; index < numberOfElements; index++) { // Loop for Array Input

    if(index==0) //Dialog Design, tied to Loop

    {Element = "First";}    
    else if 
    (index==1) {Element = "Second";} 
    else if
    (index==2) {Element = "Third";}
    else if 
    (index==3) {Element = "Fourth";}
    else if 
    (index==4) {Element = "Fifth";}


    response = JOptionPane.showInputDialog(null, "Enter the " + Element + " (" +(index+1)+ "): " ); //Display Dialog

// the validation should be here right?


}    
    int Array1 = Integer.parseInt(response);
    Array[index] = Array1;

Aucun commentaire:

Enregistrer un commentaire