javawhile-loopcompiler-errorsidentifierunexpected-token

Why am I getting errors from my while-loop in Java?


Beginner here. I'm having trouble writing a while loop (at section c) that outputs all numbers divisible by 5 and by 7 in the interval [35, 175] next to each other, separated by spaces. I am getting all kinds of errors at the line of the while loop, which I have to use, as per the assignment.

Here is my Code:

public class Assignment2 {

    public static void main(String[] args) {

        // TODO: Implement your solution
        String firstText = "A noteworthy and suitable language.";
        String secondText = "The number of characters is not enough!";

        // section a)
        int numberOfTexts = 1;

        while (numberOfTexts <= 1) { 
            String result = firstText
                    .replace("a", "-a") .replace("A", "-A")
                    .replace("e", "-e") .replace("E", "-E")
                    .replace("i", "-i") .replace("I", "-I")
                    .replace("o", "-o") .replace("O", "-O")
                    .replace("u", "-u") .replace("U", "-U");
            System.out.println(result);
            numberOfTexts++;
        }

        // section b)
        String result = "";
        int textLength = firstText.length();

        for (int i = 0; i < textLength; i++) {
            char currentLetter = firstText.charAt(i);
            if ((int) currentLetter >= 75 && (int) currentLetter <= 90) { //uppercase
                result += currentLetter;
            } else if ((int) currentLetter >= 107 && (int) currentLetter <= 122) { //lowercase
                result += currentLetter;
            }
        }
        System.out.println(result);
        result = "";
    }

    // section c)
    int num = 35;

    while (num < 175) { //line 45
        boolean requirement = (num % 5 == 0) && (num % 7 == 0);
        if (requirement) {
            System.out.print(num + " ");
        }
        num++;
    }
}

The errors are as follows:

Unexpected token :45, '>' or ',' expected :45,
Identifier expected :45,
Identifier expected :45,
Unexpected token :45

I tried using int start, end, current; but it gave the same errors. I also tried getting rid of the boolean requirement and just using its contents in the if statement, which gave the same result (as expected). Googling didn't help much either. The while loop declaration remains underlined red.


Solution

  • This part of your code is not within a method or initializer.

        int num = 35;
    
        while (num < 175) { //line 45
            boolean requirement = (num % 5 == 0) && (num % 7 == 0);
            if (requirement) {
                System.out.print(num + " ");
            }
            num++;
        }
    

    You just need to put that code inside the main method – as in the below code.

    public class Assignment2 {
    
        public static void main(String[] args) {
    
            // TODO: Implement your solution
            String firstText = "A noteworthy and suitable language.";
            String secondText = "The number of characters is not enough!";
    
            // section a)
            int numberOfTexts = 1;
    
            while (numberOfTexts <= 1) { 
                String result = firstText
                        .replace("a", "-a") .replace("A", "-A")
                        .replace("e", "-e") .replace("E", "-E")
                        .replace("i", "-i") .replace("I", "-I")
                        .replace("o", "-o") .replace("O", "-O")
                        .replace("u", "-u") .replace("U", "-U");
                System.out.println(result);
                numberOfTexts++;
            }
    
            // section b)
            String result = "";
            int textLength = firstText.length();
    
            for (int i = 0; i < textLength; i++) {
                char currentLetter = firstText.charAt(i);
                if ((int) currentLetter >= 75 && (int) currentLetter <= 90) { //uppercase
                    result += currentLetter;
                } else if ((int) currentLetter >= 107 && (int) currentLetter <= 122) { //lowercase
                    result += currentLetter;
                }
            }
            System.out.println(result);
            result = "";
    
            // section c)
            int num = 35;
    
            while (num < 175) { //line 45
                boolean requirement = (num % 5 == 0) && (num % 7 == 0);
                if (requirement) {
                    System.out.print(num + " ");
                }
                num++;
            }
        }
    }