473,383 Members | 1,748 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,383 software developers and data experts.

invalid input problem<HELP>

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
3 2296
sukatoa
539 512MB
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
no i`am not
and thanks but there no easier way than Catching and Handling Exceptions?
thanks
May 18 '08 #3
Laharl
849 Expert 512MB
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

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

Similar topics

31
by: da Vinci | last post by:
OK, this has got to be a simple one and yet I cannot find the answer in my textbook. How can I get a simple pause after an output line, that simply waits for any key to be pressed to move on? ...
15
by: Philipp Lenssen | last post by:
My friend has the following problem (background: we want to transform XML to XHTML via XSLT): "We copy XHTML fragments into an output by using the following template: <xsl:template match="*"...
4
by: Weasel | last post by:
Hey everyone, My names John and i just recently joined this googl group, and I joined because i need some help. I'm currently a Sophomore in High school and i'm really interested in computer...
2
by: Chuck | last post by:
I am trying to retrive one field from the "company info" table that contains several fields but I want the "company name" field. There is only one record in this table. When I entered the code...
0
by: Chuck | last post by:
Can someone let me know the name of a companies that provide first tier support. I have an Access application that I have sold to 12 companies here in the area and I was thinking about selling...
3
by: WØCBF | last post by:
I know this code has worked before but now appears to get a compile error. The code it seems to choke on line 12. I receive the following message and it highlights the "rst!" statement. Error...
2
by: WØCBF | last post by:
I am trying to copy the information from a form into a table. I have tried running the sql code from a macro by using the command : This works and writes the work 'test' into the field name...
3
by: tkirke | last post by:
How does one transfer a buffer object from python -c and back again (assuming the data gets modified)? I can't seem to get this or anything else to work, but am clueless as to what I'm doing wrong...
5
by: Icarus - iD_Ten_T helper | last post by:
First of all, my apologies if this should be in a php newsgroup and not here, but I thought this the best place to start. I want to parse the text from a <textareaform element but when I pass...
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...
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.