0

I am beginning with Java and found a task for switch, which I am struggling with. Hope you can point me in the right way. User can input string 1, 1-, 2, 2-, 3, 3-, 4, 4- or 5 which can be of a value of 1 to 10. Switch is to be used to convert 1 to 1.0, 1- to 1.5, 2 to 2.0, 2- to 2.5, etc. ,, however I just need to store 1 or 0 that it is used, but in cannot be boolean, because I need to do some math on it. And this switch should repeat for a given number of repeats. This should be done without using array. So I came with an idea of creating a bunch of 9 declarations to store 1 or 0 for each 1, 1-, etc., and another 9 to store their corresponding value. This is fine as long I run it just once, but when I put in the loop, it gets messy not working properly. I feel it is my misunderstandings of breaks, but I just don´t see it. Thanks

The output in case of the loop looks like: Enter grade 1- Enter stock for 1-: 3 Enter grade Invalid gradeEnter grade

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);

    int goods = 5;

    int grade1 = 0;
    int grade15 = 0;
    int grade2 = 0;
    int grade25 = 0;
    int grade3 = 0;
    int grade35 = 0;
    int grade4 = 0;
    int grade45 = 0;
    int grade5 = 0;

    int stock1 = 0;
    int stock15 = 0;
    int stock2 = 0;
    int stock25 = 0;
    int stock3 = 0;
    int stock35 = 0;
    int stock4 = 0;
    int stock45 = 0;
    int stock5 = 0;

    for( int i = 1; i <= goods; i++ ){  //start of for

        System.out.println("Enter grade");   // grade input
        String select1 = (sc.nextLine());

        switch (select1) {                    //here starts switch
            case "1":
                grade1 = 1;
                System.out.print("Enter stock for 1: ");
                stock1 = sc.nextInt();
                break;
            case "1-":
                grade15 = 1;
                System.out.print("Enter stock for 1-: ");
                stock15 = sc.nextInt();
                break;
            case "2":
                grade2 = 1;
                System.out.print("Enter stock for 2: ");
                stock2 = sc.nextInt();
                break;
            case "2-":
                grade25 = 1;
                System.out.print("Enter stock for 2-: ");
                stock25 = sc.nextInt();
                break;
            case "3":
                grade3 = 1;
                System.out.print("Enter stock for 3: ");
                stock3 = sc.nextInt();
                break;
            case "3-":
                grade35 = 1;
                System.out.print("Enter stock for 3-: ");
                stock35 = sc.nextInt();
                break;
            case "4":
                grade4 = 1;
                System.out.print("Enter stock for 4: ");
                stock4 = sc.nextInt();
                break;
            case "4-":
                grade45 = 1;
                System.out.print("Enter stock for 4-: ");
                stock45 = sc.nextInt();
                break;
            case "5":
                grade5 = 1;
                System.out.print("Enter stock for 5: ");
                stock5 = sc.nextInt();
                break;
            default:
                System.out.print("Invalid grade");
        }  // end of switch

    }  //end of for



    // test output of values for further use

    System.out.println (grade1);
    System.out.println (grade15);
    System.out.println (grade2);
    System.out.println (grade25);
    System.out.println (grade3);
    System.out.println (grade35);
    System.out.println (grade4);
    System.out.println (grade45);
    System.out.println (grade5);
    System.out.println();
    System.out.println (stock1);
    System.out.println (stock15);
    System.out.println (stock2);
    System.out.println (stock25);
    System.out.println (stock3);
    System.out.println (stock35);
    System.out.println (stock4);
    System.out.println (stock45);
    System.out.println (stock5);
} 
  • 2
    Does this answer your question? [Scanner is skipping nextLine() after using next() or nextFoo()?](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo) – OH GOD SPIDERS Jul 15 '21 at 15:04
  • 3
    "it gets messy not working properly." Is a very bad error description. You should always describe what exactly is happening that you consider an error and add any error messages you might get to your question. Just saying "It does weird stuff" is not helpful for debugging. However looking at the code I'm pretty sure that the scanner skipping the nextLine is one of the problems. – OH GOD SPIDERS Jul 15 '21 at 15:06
  • Thank you, you are so right, adding sc.nextLine() made it working. I was so blindfolded by the switch, that i did not look else where. Must learn more about proper debugging. I´m still like crawling and still have a long way to walk. So once again thank you. – Sakraprace Jul 15 '21 at 15:17
  • 1. You should localize the issue. Figure out generally what's going wrong. What is my output? What is my expected output? What is causing me to not receive the expected output? 2. You should post a MWE(Minimum Working Example). For more project based languages like Java you wont have this much but the principle is to only provide as much information as needed to describe your issue. For example just one variable and case statement would have sufficed for this. – Thomas Jul 15 '21 at 20:53

0 Answers0