Results 1 to 4 of 4

Thread: quick help

  1. #1
    jwl
    jwl is offline Member
    Join Date
    Jul 2012
    Posts
    93
    Rep Power
    0

    Default quick help

    Java Code:
        public static void main(String[] args) {
            int numIntegers = 5;
            int[] inputInts = new int[numIntegers];
            Scanner scan = new Scanner(System.in);
                for (int i = 0; i < numIntegers; i++) {
                try {     
                inputInts[i] = scan.nextInt();
                System.out.println("Got integer input: " + inputInts[i]);   
                } catch (InputMismatchException ime) {
                    System.out.println("Got non-integer input: " + scan.nextInt());
                }
            } 
        }
    }

    Can someone tell me whats wrong with this code?
    Last edited by jwl; 10-23-2012 at 08:50 PM.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,038
    Rep Power
    10

    Default Re: quick help

    You're going to have to tell us. Does it compile? Does it run? Does it throw an Exception? Generate incorrect output? Something else?

    Have you stepped through this with a debugger, or at least added some print statements, to figure out what's going on?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Arbalest is offline Member
    Join Date
    Sep 2012
    Posts
    11
    Rep Power
    0

    Default Re: quick help

    add this :
    Java Code:
    public static void main(String[] args) {
            final int numIntegers = 5; // need to be a const
            int[] inputInts = new int[numIntegers];
            Scanner scan = new Scanner(System.in);
                for (int i = 0; i < numIntegers; i++) {
                try {     
                System.out.print("input "+i+" : ");
                inputInts[i] = scan.nextInt();
                System.out.println("Got integer input: " + inputInts[i]);   
                } catch (InputMismatchException ime) {
                    System.out.println("Got non-integer input: " + scan.nextInt());
                }
            } 
        }
    }

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: quick help

    Please go through the Forum Rules -- particularly the third paragraph.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Should be a quick fix :)
    By Alienz in forum New To Java
    Replies: 5
    Last Post: 04-08-2011, 02:23 AM
  2. Quick Help!!
    By MrA in forum New To Java
    Replies: 1
    Last Post: 11-10-2010, 08:38 PM
  3. Quick help
    By locazor in forum New To Java
    Replies: 15
    Last Post: 10-17-2010, 08:07 PM
  4. Quick help
    By Qwertypoiuy in forum New To Java
    Replies: 5
    Last Post: 01-06-2010, 11:49 PM
  5. Need help quick!
    By Manikyr in forum New To Java
    Replies: 11
    Last Post: 01-28-2009, 05:08 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •