473,320 Members | 2,088 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

how to correct this do while loop

int tryagain;
do{
String cur_cat;
String msg="";
msg = "Select Category\n";
msg += "\n1. IT";
msg += "\n2. Geography";
msg += "\n3. Mathematics";
msg += "\n4. Physics";
cur_cat = JOptionPane.showInputDialog(msg);

int cur_index = 0;

for(int i=0;i<myquiz.getallquestion().length; i++){
if(myquiz.getQuesFromAll(i).getCategory().equalsIg noreCase(cur_cat)){
myquiz.setQuesToCur(cur_index, myquiz.getQuesFromAll(i));
cur_index++;
}
}

boolean complete = false;
String ans;
cur_index=0;
while(!complete){
ans = myquiz.getQuesFromCur(cur_index).show();
if(ans.equalsIgnoreCase("1") || ans.equalsIgnoreCase("2") || ans.equalsIgnoreCase("3")|| ans.equalsIgnoreCase("4")){
myquiz.getQuesFromCur(cur_index).setUserAnswer(ans );
if(cur_index<5){
cur_index++;
}
}else{
JOptionPane.showMessageDialog(null, "Invalid choice.");
}
if(cur_index == 5) complete = true;
}

JOptionPane.showMessageDialog(null, "Result = " + myquiz.getResult() + " out of 5.");

tryagain = JOptionPane.showConfirmDialog(null, "Try again?");
}while(tryagain == 0);

}

}





i want to show error if my input is less than 1 or greater than 4 but i dun know how to do it in string so please can some1 help me?thx
Jul 13 '09 #1
3 2176
JosAH
11,448 Expert 8TB
Hava a look at the article Text Based Menus. It uses a technique for checking a numerical answer being in a certain range.

kind regards,

Jos
Jul 13 '09 #2
ThatThatGuy
449 Expert 256MB
Have a look at your code....
look at this line...

cur_cat = JOptionPane.showInputDialog(msg);
That above line of code has not been type casted to int...since cur_cat is a integer variable and JOptionPane.showInputDialog() retuns an object....
so change this line to like this...

Expand|Select|Wrap|Line Numbers
  1. cur_cat = Integer.parseInt(JOptionPane.showInputDialog(msg));
Now for showing an error for values less than 1 and greater than 4.
just below the line the above code...

Expand|Select|Wrap|Line Numbers
  1.  
  2. if(cur_cat<1 || cur_cat>4)
  3. JOptionPane.showMessageDialog(this,"Invalid Input: Enter values between 1 and 4");
  4.  
Enjoy coding......
any queries you can post me...here
Jul 13 '09 #3
thx i am able to solve that problem now :D
Jul 13 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: JDJones | last post by:
Just want to verify. I have a form and I want to parse any semi-colons out of the submitted info and replace with commas. Would this be the correct way to do it? $question1 = preg_replace(";",...
10
by: Kiko Tores | last post by:
If there is something wrong , could you please point it out ( if you can give me an example as well) int find( int array, int size, int key ) { int begin=0, end=size-1, middle; while...
13
by: Alf P. Steinbach | last post by:
The third part of my attempted Correct C++ tutorial is now available, although for now only in Word format (use free Open Office if no Word), and also, it's not yet been extensively reviewed -- ...
0
by: Avi Schwartz | last post by:
In this issue the following statement is made: "In plpgsql, you cannot run a dynamic SELECT statement and be able to do anything with the selected data. You cannot assign the selected value to a...
2
by: p19010101 | last post by:
Basically I need to populate a form's listview with entries from a DB table, the form is called by the main form. All these is fine. However, the population may take a long time due to the size...
17
by: homepricemaps | last post by:
hey folks, have a logic question for you. appreciate the help in advance. i am scraping 3 pieces of information from the html namely the food name , store name and price. and i am doing this...
2
by: gdarian216 | last post by:
I am tring to write a program that will take a string of text input and correct the spacing and capitalization. I have been able to get it to except the string by using getline. I know that the...
13
by: Carramba | last post by:
Hi! I have written some peace of code, but I wonder if it's legal for ansi-c I have no problem to compiling it, but since I'm inexperience and the output is not correct I have doubts. Thank you...
3
by: anchitgood | last post by:
I have tried a lot but unable to correct just 1 logical error in the following code. This error is in the for loop, when fine and minutes are printed as the garbage values. Please check it out: ...
5
by: greg | last post by:
sokol wrote: The sched module is *not* designed for multithreading. It assumes that the thread waiting for events is the only one putting events into the queue, so it's impossible for an...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.