Virtual Position Forum
Please register to watch content in detail
Thanks
Admin virtual position


Join the forum, it's quick and easy

Virtual Position Forum
Please register to watch content in detail
Thanks
Admin virtual position
Virtual Position Forum
Would you like to react to this message? Create an account in a few clicks or log in to continue.

need guidness in java code

View previous topic View next topic Go down

GMT + 3 Hours need guidness in java code

Post by sharmila farooqi Sun May 29, 2011 5:00 pm

sir plzz help mee!
my question about if user enter wrong input like enter text instead of number then out catch block will be executes and message appear what we type but sir i want to know how can i again get input or show the message again until user enter correct entry, like
String total=JOptionPane.showInputDialog("Enter total number");
try{
float i=Float.ParseFloat(total);//if exception catch block executes
}
catch(NumberFormatException nfe{
JOptionPane.showMessageDialoge("enter correct format");
}
avatar
sharmila farooqi
Monstars
Monstars

Posts : 35
Join date : 2011-05-29

Back to top Go down

GMT + 3 Hours Re: need guidness in java code

Post by Vuhelper Sun May 29, 2011 5:01 pm

Following code explains how you can take input from user again and again. You can modify logic according to your need.



Code:


import javax.swing.JOptionPane;



public class AgainInput {



public static void main(String[] args){



float input = -1 ;

while(input == -1){

input = takeInput();

}

}





public static float takeInput(){



float i;

String total=JOptionPane.showInputDialog("Enter total number");



try{



i=Float.parseFloat(total);//if exception catch block executes

return i;

}

catch(NumberFormatException nfe){

JOptionPane.showMessageDialog(null,"enter correct format");

return -1;

}



}

}
avatar
Vuhelper
Deep Bench
Deep Bench

Posts : 97
Join date : 2011-05-29

Back to top Go down

View previous topic View next topic Back to top

- Similar topics

Permissions in this forum:
You cannot reply to topics in this forum