-1

Hi please in a switch case program that I am developing, I am using a do..while loop to handle the case when a user enters a value that does not meet the condition but got stuck with what I should put in the "while" brackets as an error is shown on the "while" line..

package assignment;
import java.util.*;

public class Assignment {


    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        System.out.print("1)Monday\n2)Tuesday\n3)Wednesday\n4)Thursday\n5)Friday\n6)Saturday\n7)Sunday");

        System.out.println("");

        int day = input.nextInt();

        System.out.println(" ");
        do {
            switch (day) {
                case 1:
                    System.out.println("Monday");
                    break;
                case 2:
                    System.out.println("Tuesday");
                    break;
                case 3:
                    System.out.println("Wednesday");
                    break;
                case 4:
                    System.out.println("Thursday");
                    break;
                case 5:
                    System.out.println("Friday");
                    break;
                case 6:
                    System.out.println("Saturday");
                    break;
                case 7:
                    System.out.println("Sunday");
                    break;
                default:
                    System.out.println("Oh oh, that's not an accepted number, kindly try again");
                    break;
            }

            for (int clear = 0; clear < 1000; clear++) {
                System.out.println("\b");
            }

        } while (!(day.equals("1") || day.equals("2") || day.equals("3") || day.equals("4") || day.equals("5") || day.equals("6") || day.equals("7")));
    }
}
James Z
  • 12,209
  • 10
  • 24
  • 44
Nana_Yaw
  • 21
  • 5
  • 1
    `day` is an `int` not a `String`. Either change it to a `String` (And read it in as a String) or treat it like a primitive `int` where you check the value with a simple `day == 1` and not equals for example. – OH GOD SPIDERS Mar 05 '21 at 13:52
  • and ask for ```input.nextInt()``` within your do-while. Otherwise you'll loop without a way to break out of it. – Paul Mar 05 '21 at 13:56
  • @Paul how do I do that? Thank you – Nana_Yaw Mar 05 '21 at 14:08
  • @OH GOD SPIDERS thank you – Nana_Yaw Mar 05 '21 at 14:09
  • @Nana_Yaw Move your ```int day = input.nextInt()``` within the brackets of your do-while. And perhaps add a ```System.out.println("Please enter a number between 1 and 7")``` right above it, so it's clear that you're asking for it – Paul Mar 08 '21 at 07:37
  • Thanks @Paul for the heads up... However after moving the ```int day = input.nextInt(); ``` withing the do..while loop brackets, I received an error at the "while" line.. Below is the code.... – Nana_Yaw Mar 09 '21 at 17:32
  • @Nana_Yaw, I'm going to refer to a similar question which I answered a while back. Perhaps you could find some inspiration there to get your program to work. https://stackoverflow.com/a/62259585/10376405 – Paul Mar 10 '21 at 08:06

1 Answers1

0

Instead of checking day as a String, simply check it as an integer which it already is. No need to allocate extra memory when creating a new String to check with an integer.

When you begin your while loop, it seems that there is no way to check for new input. How would you be able to get new input EACH time in your loop?

There is no reliable way to clear your console cross platform as depending on the IDE you are using or which terminal UNIX or PowerShell or CMD. Take a look at this answer Java: Clear the console

Since this seems like a homework assignment, I suggest that you think about how your while loop conditions could be simplified.

Hint: Is there any way to check a range of numbers? What if you had to check 1000 different numbers, would you check each number with OR conditions?

BikeLinc
  • 46
  • 4