invalid input problem<HELP>

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ALKASER266
    New Member
    • May 2008
    • 7

    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.ja va
    *
    *
    * @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.parseIn t(JOptionPane.s howInputDialog( out+"\n"+"Enter Your Option:"));




    switch(op){

    case 1:

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

    break;
    }
    op= Integer.parseIn t(JOptionPane.s howInputDialog( "Enter "+(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.sho wMessageDialog( null,out);
    break;
    case 3:
    op= Integer.parseIn t(JOptionPane.s howInputDialog( "Enter 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.sho wMessageDialog( null,out);

    break;
    case 4:
    op= Integer.parseIn t(JOptionPane.s howInputDialog( "Enter 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.sho wMessageDialog( 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.sho wMessageDialog( null,out);
    }


    break;
    case 5:

    op= Integer.parseIn t(JOptionPane.s howInputDialog( "Enter 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+"\nAver age Number ="+average;
    JOptionPane.sho wMessageDialog( null,out);

    break;
    case 7:
    break;

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

    }


    out="";
    op= Integer.parseIn t(JOptionPane.s howInputDialog( "Enter 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
  • sukatoa
    Contributor
    • Nov 2007
    • 539

    #2
    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 NumberFormatExc eption 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

    Comment

    • ALKASER266
      New Member
      • May 2008
      • 7

      #3
      no i`am not
      and thanks but there no easier way than Catching and Handling Exceptions?
      thanks

      Comment

      • Laharl
        Recognized Expert Contributor
        • Sep 2007
        • 849

        #4
        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.

        Comment

        Working...