1. Member
Join Date
Sep 2011
Posts
12
Rep Power
0

## variables in loops

I am learning loops and this is what I need to do. Essentially ask user for some numbers and get an average. Then I need to ask them if they'd like to enter another set. I cannot use arrays and have some code here.

Java Code:
``` try
{
numAB = Integer.parseInt (AB);
numHits = Integer.parseInt (hits);
System.out.println (numAB);
System.out.println (numHits);
System.out.print ("Enter another player :");
if (numHits>numAB){
System.out.println("You are a dumbass!!");
System.exit(1);
}
if (option=="yes"){
System.out.print ("Enter  name: ");
System.out.print ("Enter at bats: ");
System.out.print ("Enter hits: ");
}```

2. Moderator
Join Date
Apr 2009
Posts
11,302
Rep Power
18

## Re: variables in loops

Why are you creating a new reader for each input?
You only need one.

3. Member
Join Date
Sep 2011
Posts
12
Rep Power
0

## Re: variables in loops

Well before the try I needed to store Name NumAB num

4. Moderator
Join Date
Apr 2009
Posts
11,302
Rep Power
18

## Re: variables in loops

But you have 4 buffered readers, all referring to System.in.
You only need one.

Your indenting could do with tidying up, as it's not the easiest thing to follow at the moment.
And what does it do at the moment?

5. Member
Join Date
Sep 2011
Posts
12
Rep Power
0

## Re: variables in loops

It takes user input for a baseball player's name number of hits and number of at bats. There is another method to calculate the average of the numAB/numHits to get their batting average

Java Code:
```import java.io.*;
import java.lang.*;
import java.util.*;
import java.text.*;
public class Baseball
{
public static void main (String[]args) throws IOException
{
String firstName;
String lastName;
String AB;
String hits;
String option;
int numAB = 0;
int numHits = 0;
System.out.print ("Enter name: ");
System.out.print ("Enter at bats: ");
System.out.print ("Enter hits: ");
//System.out.println(firstName+" "+lastName+" "+AB+" "+hits+" "+option);
try
{
numAB = Integer.parseInt (AB);
numHits = Integer.parseInt (hits);
System.out.println (numAB);
System.out.println (numHits);
if (numHits&gt;
numAB)
{
System.out.println("You are a dumbass!!");
System.exit(1);
}
}
catch (NumberFormatException e)
{
System.out.println (AB);
System.out.println (hits);
}
float Average = getAverage ((float) numHits, numAB);
DecimalFormat formatter = new DecimalFormat (".###");
System.out.println (formatter.format (Average));
}
public static float getAverage (float numHits, float numAB)
{
float Average = numHits / numAB;
return (Average);
}
}```

6. Moderator
Join Date
Apr 2009
Posts
11,302
Rep Power
18

## Re: variables in loops

Second, for the average, at some point you need a 'total' and a 'count'.
So your loop needs to go round a number of times equal to the number of numbers the user is to enter (that is the 'count').

7. Member
Join Date
Sep 2011
Posts
12
Rep Power
0

## Re: variables in loops

I will try one buffered reader, but how do I print the results for the user then ask them if they want to enter another player (y or n) and continue or stop based on their answer.

It is dividing hits/at bats so no counter is necessary.

8. Moderator
Join Date
Apr 2009
Posts
11,302
Rep Power
18

## Re: variables in loops

Sorry, I misunderstood your original post.
OK, so the average is working then.

You need a loop, probably do-while, which will continue until the user enters 'n'.
Java Code:
```DO
Get player details
Ask user if want to enter another one.
That's the basic flow.

9. Member
Join Date
Sep 2011
Posts
12
Rep Power
0

## Re: variables in loops

Would this be in the try statement?

10. Moderator
Join Date
Apr 2009
Posts
11,302
Rep Power
18

## Re: variables in loops

The loop needs to cover asking the user for input, which seems to happen outside your try/catch block.

11. Member
Join Date
Sep 2011
Posts
12
Rep Power
0

## Re: variables in loops

am I missing something obvious?

Java Code:
```import java.io.*;
import java.lang.*;
import java.util.*;
import java.text.*;
public class Baseball
{
public static void main (String[]args) throws IOException
{

String lastName;
String AB;
String hits;
String user;
boolean option;
int numAB = 0;
int numHits = 0;
do
System.out.print ("Enter name: ");
System.out.print ("Enter at bats: ");
System.out.print ("Enter hits: ");
while (user=="y")
option=true;

if (option=true){
try
{
numAB = Integer.parseInt (AB);
numHits = Integer.parseInt (hits);
System.out.println (numAB);
System.out.println (numHits);

float Average = getAverage ((float) numHits, numAB);
DecimalFormat formatter = new DecimalFormat (".###");
System.out.println (formatter.format (Average));

}
}

catch (NumberFormatException e)
{
System.out.println (AB);
System.out.println (hits);
}

System.out.print ("enter option");

}

public static float getAverage (float numHits, float numAB)
{
float Average = numHits / numAB;
return (Average);
}
}```