Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Need help with Code

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need help with Code

    I need help writing a program that reads to positive integers, one at a time, in 5 attempts.
    I must leave one attempt to guess the second integer. So if the user does not input the correct answer by the fourth attempt, they can not get the first integer right.

    This is the code I have so far.
    package readtwopostiveinteger;
    import java.util.*;
    import java.io.*;
     
     
    public class ReadTwoPostiveInteger {
        private static final int numAttempts=5;
        private static int posInt;
     
        public static void main(String[] args) {
         int i = 0;
         System.out.println("Enter two postive integers,one at a time, # of Attemps Remaining:"+numAttempts);
         Scanner keyb = new Scanner(System.in);
     
         for(i=1; i<=numAttempts-1; i++) {
            try { posInt = keyb.nextInt();{
            if (posInt>0)
            System.out.println("Correct");}
            if (posInt<=0) throw new InputMismatchException();          
                       }
            catch(InputMismatchException ime){
            System.out.println("Your Input was not a positive integer");
            if (i== numAttempts) System.out.println ("sorry");
            else System.out.println("Enter a postive integer,#of attemps remaining:" +numAttempts);
             continue;}
                     }
     
     
     
                 } 
     
     
     
                }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,434
    Thanks
    49
    Thanked 2,216 Times in 2,189 Posts

    Default Re: Need help with Code

    Can you explain what your problem is? Do you get errors? Please post the full text of the messages.

    What happens when the program executes? Copy and paste here the console for when you execute the program. Add some comments to it explaining what the problem is.

    The formatting of the code (the locations of the }s) make the code hard to read. The } should not be on the same line after a statement. The } should line up vertically beneath the line with the pairing {.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. How to Translate working code into code with a Tester Class
    By bankoscarpa in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 15th, 2012, 02:13 PM
  2. Replies: 3
    Last Post: September 3rd, 2012, 11:36 AM
  3. code to refresh and check the code of a webpage..
    By vaggelis in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 7th, 2012, 06:43 AM
  4. problem in my code java code graph editeur
    By kisokiso in forum Java Theory & Questions
    Replies: 5
    Last Post: January 6th, 2012, 07:36 AM
  5. Code is giving an error in console, but there are no errors apparent in the code!
    By JamEngulfer221 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 15th, 2011, 08:30 PM