Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why is my while-loop throwing errors 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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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++;
        }
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading