3 Replies - 311 Views - Last Post: 02 February 2013 - 12:48 PM Rate Topic: -----

#1 dstevens  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 33
  • Joined: 02-February 13

problem with loop, help please!

Posted 02 February 2013 - 08:26 AM

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 then 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?");
            }
        }
    }

}

This post has been edited by GregBrannon: 02 February 2013 - 08:48 AM
Reason for edit:: Added code tags, please use code tags. Also, properly indent your code.


Is This A Good Question/Topic? 0
  • +

Replies To: problem with loop, help please!

#2 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1363
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: problem with loop, help please!

Posted 02 February 2013 - 08:36 AM

Try and use a method for getting a number from the user. A good basis could be

private int getIntFromUser(String prompt) {
    boolean goodInput = false;
    int result = 0;
 // loop until user has given good input
    while(!goodInput) {
           // display prompt
           // continually get input from the user. If its good set goodInput to true.
           // otherwise the loop will keep running until the user has given an int
   }

   return result;
}


Then in main you only have to call this method 10 times with different prompts. You can see how this way reduced code repetition which is good for programmers!

int first = getIntFromUser("Enter first number");
..
// and so on

Was This Post Helpful? 0
  • +
  • -

#3 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: problem with loop, help please!

Posted 02 February 2013 - 09:10 AM

Ryano121's suggestion is right on (of course). Anytime you find yourself typing/doing/coding the same thing more than once, think "this is a good time to write a method." Seeing the logic flaws in your original code might serve as possible learning points:

The for loop will execute x (10) times no matter what. Good inputs, bad inputs, the for loop will execute 10 times.

Then, inside the for loop, the user is prompted to enter 10 numbers. If the user follows the instructions and properly enters 10 numbers at each prompt in the try block, a total of 100 numbers will be entered. 10 times through the for loop collecting 10 numbers each time = 100 inputs.

As it is now, the for loop is redundant. A while or do/while loop to successfully complete the try block all the way through ONCE may have been what you were thinking.

A for loop INSIDE the try block that asks the user for a number 10 times would accomplish the objective and would eliminate the need to type each prompt 10 times.

Good luck!
Was This Post Helpful? 0
  • +
  • -

#4 dstevens  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 33
  • Joined: 02-February 13

Re: problem with loop, help please!

Posted 02 February 2013 - 12:48 PM

Thank you everyone who answered my post. I do appreciate your help and look forward to future post and answers, for I have a lot to learn. I finally have a code that works with the help from this forum, and it is as follows...
import java.io.File;
import java.util.Scanner;


public class ParseInt {

	/**
	 * parseint method
	 */
	public static void main(String[] args) {
		int num1 =0, sum = 0;
		int x = 10, count = 1, err1 = 1;
		
		
		System.out.println("Enter 10 numbers to find the Average ");
		
		Scanner series = new Scanner(System.in);
		for (count = 1; count <= x; count++)
			
			try{
							
			String int1 = series.next();
			num1 = Integer.parseInt(int1);
			sum = sum + num1;
			
				
			}
	 		catch (NumberFormatException e)
	 		{
	 		//System.out.println(count);//testing purposes
	 		System.out.println("You entered a letter or symbol, and an Error occured. Please enter an integer?");
	 		if (err1== 1 && count<10){
	 			
	 			count = count - err1;
	 			//System.out.println(count);//testing purposes
	 			}
	 		else{
	 			count++;
	 		}
	 			
	 		 
	 	}
		double numaverage = ((sum)/x);
		System.out.println("your average of the integers entered equals......" + numaverage);
	}
}


This post has been edited by macosxnerd101: 02 February 2013 - 08:40 PM
Reason for edit:: Please use code tags

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1