0

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!!

Blorgbeard
  • 101,031
  • 48
  • 228
  • 272
  • Easiest thing to do is to wrap the arrays in a list (using `Arrays.asList`), then use `contains`. – Andy Turner Jan 08 '18 at 20:22
  • Possible duplicate of [How can I test if an array contains a certain value?](https://stackoverflow.com/questions/1128723/how-can-i-test-if-an-array-contains-a-certain-value) – Salem Jan 08 '18 at 20:23
  • In your specific case, i would suggest you to use a map rather than 2 arrays. It is not a good practice to have 2 separate data structures, being maintained separately, but to be used as dependent on each other. Then for validation, you can easily use contains for keyset and values in the map. Once the validation passes, find the corresponding value for the string month, if needed. – moonlighter Jan 08 '18 at 20:26

1 Answers1

0

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();
  }
}
moonlighter
  • 541
  • 3
  • 14
  • If I'm understanding the concept correctly, then if I want to output the VALUE rather than the KEY to strMonth I should write something that essentially swaps containsKey and containsValue with some minor tweaks. This should allow me to parse the string value as an integer and plug it into my formula, right? – Matthew Bailey Jan 08 '18 at 21:21
  • I cannot get the if for Value to work. `if (values.containsValue(manual)) {` `strMonth = Integer.parseInt(manual);` `}` `else {` `if (values.containsKey(manual)) {` `for (Map.Entry entry : values.entrySet()) {` `if (entry.getKey().equals(valueOf(manual))) {` `strMonth = entry.getValue();` `}` `}` `}` `}` Every time I try using the VALUES field, my result in strMonth is 0, but when I type the text month it works 100% of the time. Is it because I'm inputting the scanner read as a string? I guess I'm still stuck, but I like where you pointed me. – Matthew Bailey Jan 09 '18 at 17:08
  • you are trying too hard without grasping the concept but totally understood... it's the learning curve. If you want the value instead of key in strMonth, just use if (values.containsKey(manual)) { strMonth = values.get(manual).toString(); } else if (values.containsValue(Integer.valueOf(manual))) { strMonth = manual; } – moonlighter Jan 09 '18 at 17:59