Hi there, I'm working on a 'queue program' which will queue 30 job numbers(case 1) between 1 and 30, validated, in an array, display the array , (case 3) and remove a number from the back of the queue.(case 2)

I have the display array and remove from front of queue bits working but I'm struggling to figure out how I'm supposed to have the array values entered 1 at a time and then break back to the switch and case menu.

public static void fillArray(int[] arrayIn){        //filling array code, fills arrayIn with values
    			Scanner sc = new Scanner (System.in);
			System.out.print("Please enter a job number to the queue");
			System.out.println(" \r\n ");
			

			
			
			if(arrayIn[0]==0 && arrayIn[29]==0)
			{ //if 2
		
			for (int i = 0; i < arrayIn.length; i=i+1)                    
			{ //for 1

			System.out.print("enter value ");          //get input for values..


			int value=sc.nextInt();
			if (value>0 && value <31)
			{
			arrayIn[i] = value;		
			}
			else
			{
			System.out.println("Invalid Number, must be between 1 and 30 inclusive");
			}

			} 
		



			
			}

			else if(arrayIn[0]!=0 && arrayIn[29]!=0)
			{
			System.out.println("Queue is full, please use menu option 2");
			}


}

What ideally would be happening here is that instead of entering 30 at once, once the first value is entered, it would break back to the switch and case, and then the next time that case 1 was entered it would do the same except enter to arrayIn[1] instead of arrayIn[0], and so on until the array is full.

If you need me to post the other program code let me know.

This is probably ridiculously simple and I'm missing something obvious but I'm new in Java so go easy ;).

Cheers, Nick.

What ideally would be happening here is that instead of entering 30 at once, once the first value is entered, it would break back to the switch and case, and then the next time that case 1 was entered it would do the same except enter to arrayIn[1] instead of arrayIn[0], and so on until the array is full.

I'm not really sure what you mean here but let me give a crack at it. You have some sort of menu that lets users choose an option 1-3. If they choose option 1 to insert a value into the job queue, you want them only to enter one value and then display the menu to the user again?

I just want to make sure I dont misinterpret you and throw you off with some help that you dont really need.

Yes, Slimmy, that's exactly it.

I want them to enter 1 value, to the first blank(0) array index each time the fillArray method is used.

Then it should return to the menu, and if they chose the fillArray method again, they should enter one new value to the next blank array index, and then back to the menu again.

Ok, thats good. So then you should break down your problem a little bit. Your fillArray() are pretty much doing the job if you remove the for-loop. In the last if-statement I assume you are trying to check if the queue is full, this can be done a little bit easier if you introduce an index variable that keeps track of where in the array you currently are. If that index variable exceeds some value, 30 in your case, you know that the queue is full. Im not quite sure what you want to accomplish with the first if-statement.

Anyway, back to breaking down the problem. It should look something like this (for insertion only)

1. Show the menu
2. User chooses 1 -> you call fillArray()
3. Let the user enter ONE value and insert it at the current pos in the array
4. Show the menu again.

That should get you on your way with the insertions, remember that the for-loop is not necessary since you only want to handle one value/job at a time. The trick is to make the fillArray() do as little work as possible. You basically have it there in code but you try to handle a lot of stuff at the same time.

Edit: Some spelling errors

Thanks, I'll have a go at this probably tomorrow! :)

The first if statement above is my extremely sloppy way of checking if the array is empty/not full, I have a method somewhere else in my code called sum, to add the values, but I had to move things about and was in a rush when I re-wrote the fillArray to what it is now.

Wasn't having much luck so I slept on it a couple of nights, got it working in the end. Turns out I was completely overlooking the

break;

function/label.

Anyway here's what I did to get it working

public static void fillArray(int[] arrayIn){        //filling array code, fills arrayIn with values

			Scanner sc = new Scanner (System.in);
			if(arrayIn[0]!=0 && arrayIn[29]!=0)
			{
			System.out.println("The queue is full!\r\n Please use menu option 2 to remove a job number!");
			return;
			
			}
				
			int value=sc.nextInt();

			if(value >=1 && value <=30)
			{
			for (int i = 0;i<arrayIn.length;i=i+1)
			{	
			if(arrayIn[i]==0)
			{
			
			arrayIn[i]=value;
			break;
			}
			

			}
		
			
			}
			
			
			else
			{	
			System.out.println("Number must be between 1 and 30 inclusive");
			System.out.println("\r\n");	
			}	
			return;
}

Knew it was something stupid I was missing ^^.
I know, I know, my coding is terrible and there's a billion mistakes with my syntax and logic but it works :D.

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.