简体   繁体   中英

How do I validate user input in java against two arrays?

My task is to code a small program that would calculate an administrator access code that changes daily based on a predefined algorithm (month * day * constant).

I have the gist of the code written for my main method with the basic functionality pulling from java.time.LocalDate & format.DateTimeFormatter and then parsing the string into an integer and then calculating and displaying the results.

String month = DateTimeFormatter.ofPattern("MM").format(localDate);
String date = DateTimeFormatter.ofPattern("dd").format(localDate);
int monthResult = Integer.parseInt(month);
int dateResult = Integer.parseInt(date);
int adminAccess = monthResult * dateResult * seed;
System.out.println("Admin Passcode is: " + adminAccess);

But now I want to take this to the next level and incorporate an option to calculate the access code manually via user input.

I want to validate the user input and allow numeric or string input before taking the input and assigning the correct integer representation based on their input. I'm looking to ultimately do something along these lines (Anticipated Program Flow) 1 . I'm not sure if I'm just too far above my head, but I can't quite wrap my mind around what functionality I should use a For, Do While or a Switch to process the validation. All I have right now is:

class userManual {
            public void run() {
                String [] months = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
                String [] numMonths = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"};
                Scanner scan = new Scanner(System.in);
                String manual = scan.next();
                for () {}
            }
        }

Any ideas would be greatly appreciated!!

try the following. I haven't optimized it but put it together quick and dirty to help you with the idea. Make sure to understand the concept rather than just copy-pasting code. Hope this helps!

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;

class UserManual {

  public static void main(String[] args) {
    Map<String, Integer> months = new HashMap<String, Integer>();
    months.put("January", 1);
    months.put("February", 2);
    months.put("March", 3);
    months.put("April", 4);
    months.put("May", 5);
    months.put("June", 6);
    months.put("July", 7);
    months.put("August", 8);
    months.put("September", 9);
    months.put("October", 10);
    months.put("November", 11);
    months.put("December", 12);
    Scanner scan = new Scanner(System.in);
    String manual = scan.next();
    String strMonth = "";
    if (months.containsKey(manual)) {
      strMonth = manual;
    } else if (months.containsValue(Integer.valueOf(manual))) {
      for (Entry<String, Integer> entry : months.entrySet()) {
        if (entry.getValue().equals(Integer.valueOf(manual))) {
          strMonth = entry.getKey();
        }
      }
    }
    if (!strMonth.equals("")) {
      System.out.println("Valid Month --> " + strMonth);
    } else {
      System.out.println("Input is invalid!");
    }
    scan.close();
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM