Hi everyone,

I have a system where a user can order some predefined items. To do this, I have been using a switch statement, e.g:

System.out.println("Product?");

        System.out.println("\n1. Chair\n2. Table\n3. Desk\n4. Other");

        int sel = 0;

        sel = console.nextInt();

        switch(sel){          
        
        case 1:
        
        <code>

        case 2:

        <code>

        case 3:

        <code>

However, once i've reached the end of the individual cases, I would like them to repeat, so the user could buy more than one item at once, and have this added to an array list. I think I have the array worked out, but the loop is proving problematic. I understand that the code below is one way I can do it, but I don't know how to increase the counter so the statement can loop properly.

Can anyone please help? :)

for(int i = 0; i < 5; i++){
  System.out.println("Product?");  
  System.out.println("\n1. Chair\n2. Table\n3. Desk\n4. Other"); 
  int sel = 0; 
  sel = console.nextInt();
  switch(sel){
    ...
  }
}

Recommended Answers

All 2 Replies

In this scenario you want to use "while" loop instead of for loop and you should set some interruption sequence. Like user input zero and you get out of the loop and continue with what ever you have after it (confirm address,print invoice etc)

private void call(){
		
        System.out.println("\n1. Chair\n2. Table\n3. Desk\n4. Other");

        int sel = 0;

        sel = console.nextInt();

        switch(sel){          
        
        case 1:
        
        <code>

        case 2:

        <code>

        case 3:

        <code>
        
        case 4:
        	exit

	}
        
    call();

i think this l b a good way
call the method again after the switch condition and have a case for exit, to come out of the method.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.