0

I'm trying to write some code that reads the tab-delimited data like that; Add SmartColorLamp CLamp2 On Add SmartColorLamp CLamp3 On 2000 50 Add SmartColorLamp CLamp4 On 0x00GG00 30 Add SmartColorLamp CLamp4 On 0xFFFFFFF 100 Add SmartColorLamp CLamp4 On 0x00FF00 70 in the text and conditions it to certain properties and then discards the linked list, but I'm getting an error I guess because of the hexadecimal check part. help?

I think there is a problem with the if statements I need to use, but I'm a beginner java developer, I need your help.

    if (parts[0].equals("Add") && parts[1].equals("SmartColorLamp")) { // Add SmartColorLamb command
        int kelvin = 4000;
        int brightness = 100;
        if (!initialTimeSet) { // initialTime is not set
            System.out.println("COMMAND: " + line);
            System.out.println("ERROR: First command must be SetInitialTime! Program is going to terminate!");
            System.exit(0);
        }
        if (parts.length > 3 && !parts[3].equalsIgnoreCase("On") && !parts[3].equalsIgnoreCase("Off")) {
            System.out.println("COMMAND: " + line);
            System.out.println("ERROR: Erroneous command!");
            return;
        }
        String initialStatus = "Off";
        if (parts.length > 3) {
            initialStatus = parts[3];
        }

        String deviceName = parts[2];
        boolean isAlreadyExists = devicesList.stream().anyMatch(device -> device.getName().equals(deviceName));

        if (isAlreadyExists) {
            System.out.println("ERROR: There is already a smart device with the same name!");
        }

        else {

            if (parts.length >4 && parts[4].matches("^[0-9A-Fa-f]+$")) {
                // If the value is a hexadecimal color code
               kelvin = Integer.parseInt((parts[4]));


            } if(parts.length >4) {
                // If the value is a kelvin temperature
                try {
                    kelvin = Integer.parseInt(parts[4]);
                } catch (NumberFormatException e) {
                    System.out.println("ERROR: Invalid kelvin or color code format!");
                    return;
                }
            }
            if (parts.length > 5) {

                try {

                    brightness = Integer.parseInt(parts[5]);
                } catch (NumberFormatException e) {
                    System.out.println("ERROR: Invalid Brightness format!");
                    return;
                }
                if (brightness < 0 || brightness > 100) {
                    System.out.println("ERROR: Brightness must be in range of 0%-100%!");
                    return;
                }
            }

            SmartColorLamb smartColorLamb = new SmartColorLamb(deviceName, initialStatus, kelvin, brightness);
            devicesList.add(smartColorLamb);
            System.out.println("SUCCESS: Smart Color lamp has been added to the devices list!");
        }
    }
xtrabyte
  • 23
  • 5
  • A [mcve] would help serve you better in the future. Based on my guess work of your data, I don't know where tabs should be, I either get `ERROR: Invalid kelvin or color code format!` or it works fine – MadProgrammer Apr 18 '23 at 03:51

1 Answers1

-1

I think there would be occurs error when parts[4] does't start with 0x.

yue zhang
  • 26
  • 2
  • What would you recommend in this situation? – xtrabyte Apr 18 '23 at 03:39
  • Try this: ```java if (hexString.matches("^(0x)?[0-9A-Fa-f]+$")) { // Valid hexadecimal number } else { // Not a valid hexadecimal number } ``` And I am sorry that I am new here so that I don't know how to format code. – yue zhang Apr 18 '23 at 03:47