By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,247 Members | 1,057 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,247 IT Pros & Developers. It's quick & easy.

invalid input problem<HELP>

P: 7
Hey guyz

I am a java beginner and I have problem with part of the code.
The thing that I don't know how to do it in this code is how to show the user that he/she has input an invalid input
In my code the valid input will be anything except 1 to 7
and I hope that your answers will be as simple way as ever coz i am just a beginner, thanks

This is the code:

/**
* @(#)Araaypro.java
*
*
* @author
* @version 1.00 2008/5/13
*/
import java.io.*;
import javax.swing.*;
public class Araaypro
{
public static void main(String args[])
{

int op,i=0,s=0;
int Num[] = new int [20];
String out;
int total = 0;
int average = 0;

do{
out="**********MENU **********\n1. for Store\n2. for Print\n3. for Search\n4. for Remove\n5. for Set\n6. for Calculate\n7. for Quite";

op= Integer.parseInt(JOptionPane.showInputDialog(out+" \n"+"Enter Your Option:"));




switch(op){

case 1:

do{
if(i==20)
{ out=" You Exeed the array size";
JOptionPane.showMessageDialog(null,out);

break;
}
op= Integer.parseInt(JOptionPane.showInputDialog("Ente r "+(i+1)+" Integer Number or -99 for stop="));
Num[i]=op;
i++;

}while(op!=-99);



s=i-1;
break;
case 2:
out="";

for(int j=0; j<s; j++)
out+=(j+1)+" Number = "+Num[j]+"\n";
JOptionPane.showMessageDialog(null,out);
break;
case 3:
op= Integer.parseInt(JOptionPane.showInputDialog("Ente r search Number="));
int f=1;
for(int j=0; j<s; j++){
if(Num[j]==op)
f=0;
}
if(f==0)
out="search <"+op+"> Number Found "+"\n";
else
out="search <"+op+"> Number NOT Found "+"\n";

JOptionPane.showMessageDialog(null,out);

break;
case 4:
op= Integer.parseInt(JOptionPane.showInputDialog("Ente r Remove Number="));
int f1=1,k=0;
for(int j=0; j<s; j++){
if(Num[j]==op){
k=j;
f1=0;
}
}
if(f1==0)
{
out="search <"+op+"> has been removed "+"\n";
JOptionPane.showMessageDialog(null,out);

for(int j1=k; j1<=s-2; j1++)
Num[j1]=Num[j1+1];
s=s-1;
break;
}
else
{

out="search <"+op+"> Number NOT Found "+"\n";

JOptionPane.showMessageDialog(null,out);
}


break;
case 5:

op= Integer.parseInt(JOptionPane.showInputDialog("Ente r A Number to set into thr array="));

for (int i1 = 0;i1<Num.length;i1++)
Num[i1] = op;
s=Num.length;
break;
case 6:


for(int i2=0; i2<s; i2++)
total =total + Num[i2];


average = total /s;
out="Total = "+total+"\nAverage Number ="+average;
JOptionPane.showMessageDialog(null,out);

break;
case 7:
break;

default: out=" Wrong option enter 1 to 7 only";
JOptionPane.showMessageDialog(null,out);

}


out="";
op= Integer.parseInt(JOptionPane.showInputDialog("Ente r 0 to Exit, or 1 to go to menu="));

}while(op!=0);
}




}

Also there is another problem i have faced: how i can make it quite the program as soon as i chose:
7. for Quite
Without asking me to enter 0 to exit or 1 to go to menu?

Thank you very much
Hope to get your solution soon and thank you
May 17 '08 #1
Share this Question
Share on Google+
3 Replies


100+
P: 539
Are you from C?

Looks like familiar the way you implement your code....

Here is how to Define Methods and how to use them.

I guess you want the user to enter just an integer, A NumberFormatException is thrown if he/she does,
You can read Catching and Handling Exceptions for a while.

quit? an exit(int status) method can be found in System class.

You can show us the flow/algo of your program, we don't know what's the exact/wrong output of your program.

and please, inclose your code with a codetag next time...
eg. [/code] at the end and [code=JAVA] at the top of your code...

regards,
sukatoa
May 17 '08 #2

P: 7
no i`am not
and thanks but there no easier way than Catching and Handling Exceptions?
thanks
May 18 '08 #3

Expert 100+
P: 849
If you're using the Scanner class to read your input (if you're not, you should be!), you can use its hasNextInt() method with a while loop to make sure the user has entered an integer. You'll need to use an if statement to make sure it's in the proper range, though.
May 22 '08 #4

Post your reply

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