Using User Input to Search Array Index to find string

Viewed 49

I'm pretty new to programming and very new to java so please be patient.

Basically, I'm trying to get the user-implemented number (after going through the math) to be used as an index value within the array to find out which color it belongs to.

Example. The user submits an integer. Let's say 66. 66 mod 12 would be 6. Using 6 I can go throught the array where [6] is assigned to white and print out white.

Fiber Optics Color Code Chart

Each color has 12 numbers. Blue 1-12, Orange 13-24 etc.

import java.util.Scanner;
import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        Scanner inputNum = new Scanner(System.in);  // User inputs Intager
        System.out.println("Enter Number...");

        String[] fiberColors = {"Blue", "Orange", "Green", "Brown", "Slate", "White", "Red", "Black", "Yellow", "Violet", "Rose", "Aqua"};

        int input = inputNum.nextInt(); // Read user input
        
         if (input < 145) { 
            int modNum = (input- 1) % 12; // Find the modulus number from 12
            
            for (int i = modNum; i == fiberColors.length; i++) {
                System.out.println(fiberColors[i]);
              }
            
         if (input >= 145) {
            System.out.println("Invalid Number/Over 144c"); // If user input is over 144 count
         }
        
        }

    }      
}
1 Answers

Here is a solution that works. I have added a protection to prevent an exception in the interpretation of the input.

import java.util.Scanner;

public class LemurienAgileForm {

    public LemurienAgileForm() {
        Scanner inputNum = new Scanner(System.in);  // User inputs Intager
        System.out.println("Enter Number...");

        String[] fiberColors = {"Blue", "Orange", "Green", "Brown", "Slate", "White", "Red", "Black", "Yellow", "Violet", "Rose", "Aqua"};

        String userIn = inputNum.nextLine();//Read user input

        try {
            int input = Integer.parseInt(userIn);//Try to get an int value
            if (input <= 0 || input > 144) {
                System.err.println("Invalid input (out of range)");
            } else {
                input--;
                System.out.println(fiberColors[input / 12]);
            }
        } catch (NumberFormatException e) {
            System.err.println("Invalid input (not a number)");
        }
    }
}

Edit: If the problem has been identified correctly, by entering a number, you are supposed to output the color in the array that is maped by the linked table. I take this from the statement

Each color has 12 numbers. Blue 1-12, Orange 13-24 etc.

Now what has been changed:

  1. Read the whole line from the console instead of a number (user can make wrong input)
  2. The try catch block tries to interpret the input as a number
  3. Check if the input is in the valid range (0, negative and >144 are invalid)
  4. The array starts with index 0. To map the numbers 1-12 to 0, the value is decreased by 1 and then divided by 12. Here is the integer divison used. This assigns the values of the columns to the color values of the corresponding column.

Now for the other possible solution. Assign the values in the rows to the colors in the first column. Change the content of else block to

int index = input % 12 - 1;
if(index < 0) {
    index = fiberColors.length - 1;
}
System.out.println(fiberColors[index]);

The reason for the if condition is that the modulo calculation returns the values 1-11 0 from which one is subtracted to adjust the index (new range 0-10 -1). Now the last value must be changed from -1 to the last array position.

Related