PDA

View Full Version : Java Programmers..?



3rd gen noob
12-01-2003, 03:22 AM
i'm learning java at uni just now and usually i prepare my program before the practical.
normally, myself and TClite try to program at the same time, conversing on msn messenger until one of us finishes first (him without exception :angry: )
so this week, i think it only fair to cheat :rolleyes:

anyway, does anyone know how i would insert a loop into the following program so that the program would ask for another country until the user presses the "cancel" button?

a very big thanks in advance too :)

/**
* CheeseSwitch
*
*


* Choose a country using ListBox and a Switch Statement
*
* @author
*/

import javabook.*;

public class CheeseSwitch
{
public static void main (String[] args)
{
MainWindow mainWindow = new MainWindow("Country Chooser");
MessageBox messageBox = new MessageBox( mainWindow );

ListBox countryList = new ListBox( mainWindow, "Select Country");

int choice;

mainWindow.setVisible(true);

countryList.addItem("Scotland");
countryList.addItem("England");
countryList.addItem("France");
countryList.addItem("Greece");
countryList.addItem("Netherlands");

choice = countryList.getSelectedIndex();

switch ( choice )
{

case ListBox.NO_SELECTION :
messageBox.show("No selection made");
break;

case ListBox.CANCEL:
messageBox.show("Cancelled !");
break;

case 0 :
messageBox.show("Scottish Cheese");
break;

case 1 :
messageBox.show("English Cheese");
break;

case 2 :
messageBox.show("French Cheese");
break;

case 3 :
messageBox.show("Greek Cheese");
break;

case 4 :
messageBox.show("Dutch Cheese");
}
}
}


p.s. sorry in advance for the indentation, for some reason the board software likes to mess it up :(

I.am
12-01-2003, 04:33 AM
Here is the modified code for it. You don't need to show mainWindow, showing message box is enough as you are only using message box and listbox. So I got rid of it. If you want you can add that. Doesnt make a difference only brings an empty window in the background of your listbox.

The rest of the code is clear itself. Feel free to ask.


import javabook.*;

public class CheeseSwitch{


public static void main (String[] args){

MainWindow mainWindow = new MainWindow("Country Chooser");
MessageBox messageBox = new MessageBox( mainWindow );

int choice =0;

while(choice!=ListBox.CANCEL){
ListBox countryList = new ListBox( mainWindow, "Select Country");
countryList.addItem("Scotland");
countryList.addItem("England");
countryList.addItem("France");
countryList.addItem("Greece");
countryList.addItem("Netherlands");

choice = countryList.getSelectedIndex();


switch ( choice ){

case ListBox.NO_SELECTION :
messageBox.show("No selection made");
break;

case 0 :
messageBox.show("Scottish Cheese");
break;

case 1 :
messageBox.show("English Cheese");
break;

case 2 :
messageBox.show("French Cheese");
break;

case 3 :
messageBox.show("Greek Cheese");
break;

case 4 :
messageBox.show("Dutch Cheese");
}

}

//Here choice==ListBox.CANCEL
messageBox.show("Cancelled");
}

}


Hope this helps.
I.am

Edit: Let me know if thats what you needed.

3rd gen noob
12-01-2003, 04:37 AM
thank you very much :D

i hope i can return the favour some time... :)

the only reason i was using the mainwindow was because it was included in the template we were given...i see now that it's not needed

I.am
12-01-2003, 04:40 AM
No Problem. ;) btw doesnt that Wu Textbook sucks.

3rd gen noob
12-01-2003, 04:56 AM
Originally posted by I.am@1 December 2003 - 03:40
No Problem. ;) btw doesnt that Wu Textbook sucks.
well, i've not bought it yet
i keep on meaning too, but the agenda always turns to pool... :rolleyes:

i've got a 100% record so far in programming anyway, so i'm not doing badly, at the moment :)

thanks again though