Beginner needs help! Java code
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Beginner needs help! Java code

Hybrid View

  1. #1
    Join Date
    Jan 2013
    Posts
    20

    Beginner needs help! Java code enclosed

    I have this code and have it almost complete. all I need to do is have it start
    back where the non integer was entered instead of the integer. for example if you are on the 3rd integer entered and I entered "F" instead, it will prompt you to enter an integer and the move to number 4 instead of starting all over again. Can anyone please help.

    import java.io.File;
    import java.util.Scanner;


    public class ParseInt {


    public static void main(String[] args) {
    int num1 =0, num2=0, num3=0, num4=0, num5=0, num6=0, num7=0, num8=0, num9=0, num10=0;
    int x = 10, count;


    System.out.println("Enter 1st of 10 numbers to finnd the Average ");

    Scanner series = new Scanner(System.in);
    for (count = 1; count <= x; count++)
    {
    try{
    String int1 = series.next();
    num1 = Integer.parseInt(int1);
    System.out.println("Next Number!Number 1 = " + num1);
    String int2 = series.next();
    num2 = Integer.parseInt(int2);
    System.out.println("Next Number!Number 2 = " + num2);
    String int3 = series.next();
    num3 = Integer.parseInt(int3);
    System.out.println("Next Number!Number 3 = " + num3);
    String int4 = series.next();
    num4 = Integer.parseInt(int4);
    System.out.println("Next Number!Number 4 = " + num4);
    String int5 = series.next();
    num5 = Integer.parseInt(int5);
    System.out.println("Next Number!Number 5 = " + num5);
    String int6 = series.next();
    num6 = Integer.parseInt(int6);
    System.out.println("Next Number!Number 6 = " + num6);
    String int7 = series.next();
    num7 = Integer.parseInt(int7);
    System.out.println("Next Number!Number 7 = " + num7);
    String int8 = series.next();
    num8 = Integer.parseInt(int8);
    System.out.println("Next Number!Number 8 = " + num8);
    String int9 = series.next();
    num9 = Integer.parseInt(int9);
    System.out.println("Next Number!Number 9 = " + num9);
    String int10 = series.next();
    num10 = Integer.parseInt(int10);
    System.out.println("Next Number!Number 10 = " + num10);

    double numaverage = ((num1 + num2 + num3 + num4 + num5 + num6 + num7 + num8 + num9 + num10)/x);
    System.out.println("your average of the integers entered equals......" + numaverage);
    }

    catch (NumberFormatException e){
    System.out.println("You entered a non-integer, and an Error occurred. Please enter an integer?");
    }
    }
    }

    }
    Last edited by dstevens; February 2nd, 2013 at 10:22 AM. Reason: java code

  2. #2
    Join Date
    Aug 2011
    Location
    West Yorkshire, U.K.
    Posts
    54

    Re: Beginner needs help! Java code

    What you need is an array of integers to hold the user input once it has been validated (i.e. converted to integers as you are doing with Integer.parseInt), a for loop to populate the array, and inside that for loop another while loop to validate the input and not move on to the next input until a valid input is received. Your average calculation at the end then needs to sum all the integers in the array and divide by the number of digits.

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center