0

So i have this code :

for(int i=0;i<number;i++) { 
System.out.println("WHAT HOUSEHOLD DO YOU WANT TO CONNECT IN APARTMENT NO."+(i+1));
        System.out.println("1 ) OVEN");
        System.out.println("2 ) TV");
        System.out.println("3 ) VACCUUM CLEANER");
        System.out.println("4 ) REFRIGERATOR");
        int option = cin.nextInt();
        switch(option) {
        case 1: {
            for(initiereAparate e : aparate)
            fw.write((i+1)+" "+e.ob1.denumire+" "+e.ob1.material+" "+e.ob1.culoare+" "+e.ob1.origine+" "+e.ob1.consumEnergie+"\r\n"); break;
        }
        case 2: {
            for(initiereAparate e : aparate)
            fw.write((i+1)+" "+e.ob2.denumire+" "+e.ob2.material+" "+e.ob2.culoare+" "+e.ob2.origine+" "+e.ob2.consumEnergie+"\r\n"); break;
        }
        case 3: {
            for(initiereAparate e : aparate)
            fw.write((i+1)+" "+e.ob3.denumire+" "+e.ob3.material+" "+e.ob3.culoare+" "+e.ob3.origine+" "+e.ob3.consumEnergie+"\r\n"); break;
        }
        case 4: {
            for(initiereAparate e : aparate)
            fw.write((i+1)+" "+e.ob4.denumire+" "+e.ob4.material+" "+e.ob4.culoare+" "+e.ob4.origine+" "+e.ob4.consumEnergie+"\r\n"); break;
        }
        }
}

I would like to know how to read multiple inputs.For example i will type in one line 1 2 4 , and this numbers will execute 3 cases at once.In my code that i already have i can type only 1 number and it will execute only one case statement

4
  • You'd have to read the whole line with cin.nextLine() then split the resulting string, then convert each sub string to int. Commented Mar 21, 2020 at 13:35
  • Note, you could skip the conversion to int and just use the string: case "1": Commented Mar 21, 2020 at 13:41
  • Style note: brackets { } on the case statements are not needed here. Brackets on for loops are preferred (though strictly not required here). Commented Mar 21, 2020 at 13:47
  • Why is your program shouting at you? (All caps is considered shouting.) Commented Mar 21, 2020 at 14:36

2 Answers 2

2

You would need to change your input function to expect a string rather than an int. And then simply split your string into number and loop trought the number to execute your switch case.

P.S please note that this code is untested, don't just copy and paste it expecting everything to work perfectly. Try to understand what I did and apply it to your own context.

for (int i = 0; i < number; i++) {
        System.out.println("WHAT HOUSEHOLD DO YOU WANT TO CONNECT IN APARTMENT NO." + (i + 1));
        System.out.println("1 ) OVEN");
        System.out.println("2 ) TV");
        System.out.println("3 ) VACCUUM CLEANER");
        System.out.println("4 ) REFRIGERATOR");
        // we change this to nextLine() because we want a string.
        String option = cin.nextLine();
        // we split our string at white space, this give us an array of string;
        String[] options = option.split("\\s+");
        // we parse every options in the array
        for (int j = 0; j < options.length; j++) {
            // we need to cast the option back to an integer since, it's what we are
            // comparing.
            int currentOption = Integer.parseInt(options[j]);
            switch (currentOption) {
            case 1: {
                for (initiereAparate e : aparate)
                    fw.write((i + 1) + " " + e.ob1.denumire + " " + e.ob1.material + " " + e.ob1.culoare + " "
                            + e.ob1.origine + " " + e.ob1.consumEnergie + "\r\n");
                break;
            }
            case 2: {
                for (initiereAparate e : aparate)
                    fw.write((i + 1) + " " + e.ob2.denumire + " " + e.ob2.material + " " + e.ob2.culoare + " "
                            + e.ob2.origine + " " + e.ob2.consumEnergie + "\r\n");
                break;
            }
            case 3: {
                for (initiereAparate e : aparate)
                    fw.write((i + 1) + " " + e.ob3.denumire + " " + e.ob3.material + " " + e.ob3.culoare + " "
                            + e.ob3.origine + " " + e.ob3.consumEnergie + "\r\n");
                break;
            }
            case 4: {
                for (initiereAparate e : aparate)
                    fw.write((i + 1) + " " + e.ob4.denumire + " " + e.ob4.material + " " + e.ob4.culoare + " "
                            + e.ob4.origine + " " + e.ob4.consumEnergie + "\r\n");
                break;
            }
        }
    }
}

As Arvind Kumar Avinash pointed out in the comment, it is important to add the keyword break to you switch cases, this ensure that you only treat one case per iteration.

Sign up to request clarification or add additional context in comments.

5 Comments

Make sure you put break in each case. Also, add default to show the relevant message when the user selects a number other than 1 to 4.
@ArvindKumarAvinash You are right, let me add that in. Thank you for pointing it out.
The break statements are there. Just hidden at the end of the line.
@JohnnyMopp - you are right. Nicolas - I've formatted your code to make it visible.
@Nicolas I'am getting java.lang.NumberFormatException: For input string: "" , something with this line int currentOption = Integer.parseInt(options[j]);
0

Yes, you can do that. You can use following idea:

String option = cin.nextLine();
String[] data=option.split(" ") // split by space character
for(int i=0;i<data.length;i++){
int option = Integer.parseInt(data[i]);
switch(option)...}

2 Comments

Please note that OP is already using i as their loop variable. You might want to change it to something else.
Yes, you are right. @Nicolas you can use any other variable name: i,j,k..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.