Results 1 to 12 of 12
  1. #1
    mbw290 is offline Member
    Join Date
    Sep 2011
    Posts
    12
    Rep Power
    0

    Default 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 :");
          	option=bufferedReader4.readLine();
    	if (numHits>numAB){
    	System.out.println("You are a dumbass!!");
    	System.exit(1);
    	}
    		if (option=="yes"){
    			      System.out.print ("Enter  name: ");
          bufferedReader1 =
          new BufferedReader (new InputStreamReader (System.in));
          lastName = bufferedReader1.readLine ();
          System.out.print ("Enter at bats: ");
          bufferedReader2 =
          new BufferedReader (new InputStreamReader (System.in));
          AB = bufferedReader2.readLine ();
          System.out.print ("Enter hits: ");
          bufferedReader3 =
          new BufferedReader (new InputStreamReader (System.in));
          hits = bufferedReader3.readLine ();
    		}

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,015
    Rep Power
    20

    Default Re: variables in loops

    Why are you creating a new reader for each input?
    You only need one.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    mbw290 is offline Member
    Join Date
    Sep 2011
    Posts
    12
    Rep Power
    0

    Default Re: variables in loops

    Well before the try I needed to store Name NumAB num

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,015
    Rep Power
    20

    Default 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?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    mbw290 is offline Member
    Join Date
    Sep 2011
    Posts
    12
    Rep Power
    0

    Default 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
        {
            BufferedReader bufferedReader;
            BufferedReader bufferedReader1;
            BufferedReader bufferedReader2;
            BufferedReader bufferedReader3;
            BufferedReader bufferedReader4;
            bufferedReader = new BufferedReader (new InputStreamReader (System.in));
            String firstName;
            String lastName;
            String AB;
            String hits;
            String option;
            int numAB = 0;
            int numHits = 0;
            System.out.print ("Enter name: ");
            bufferedReader1 =
            new BufferedReader (new InputStreamReader (System.in));
            lastName = bufferedReader1.readLine ();
            System.out.print ("Enter at bats: ");
            bufferedReader2 =
            new BufferedReader (new InputStreamReader (System.in));
            AB = bufferedReader2.readLine ();
            System.out.print ("Enter hits: ");
            bufferedReader3 =
            new BufferedReader (new InputStreamReader (System.in));
            hits = bufferedReader3.readLine ();
            //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>
                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. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,015
    Rep Power
    20

    Default Re: variables in loops

    Step 1 is strip out those other readers and just use the single 'bufferedReader' for all your reading.

    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').
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    mbw290 is offline Member
    Join Date
    Sep 2011
    Posts
    12
    Rep Power
    0

    Default 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. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,015
    Rep Power
    20

    Default 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.
    WHILE answer equals 'y'
    That's the basic flow.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    mbw290 is offline Member
    Join Date
    Sep 2011
    Posts
    12
    Rep Power
    0

    Default Re: variables in loops

    Would this be in the try statement?

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,015
    Rep Power
    20

    Default Re: variables in loops

    The loop needs to cover asking the user for input, which seems to happen outside your try/catch block.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  11. #11
    mbw290 is offline Member
    Join Date
    Sep 2011
    Posts
    12
    Rep Power
    0

    Default 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
        {
    	     
            BufferedReader bufferedReader1;
            BufferedReader bufferedReader2;
            BufferedReader bufferedReader3;
            BufferedReader bufferedReader4;
            String lastName;
            String AB;
            String hits;
    	String user;
            boolean option;
            int numAB = 0;
            int numHits = 0;
    	do
            System.out.print ("Enter name: ");
           bufferedReader1 =new BufferedReader (new InputStreamReader (System.in));
            lastName = bufferedReader1.readLine ();
            System.out.print ("Enter at bats: ");
            bufferedReader2 =       new BufferedReader (new InputStreamReader (System.in));
            AB = bufferedReader2.readLine ();
            System.out.print ("Enter hits: ");
            bufferedReader3 =        new BufferedReader (new InputStreamReader (System.in));
            hits = bufferedReader3.readLine ();
    	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");
            bufferedReader4= new BufferedReader (new InputStreamReader (System.in));        user=bufferedReader4.readLine();
    	
            
    
        }
    
        public static float getAverage (float numHits, float numAB)
        {
            float Average = numHits / numAB;
            return (Average);
        }
    }

  12. #12
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

Similar Threads

  1. Need help with loops
    By MrPosotive in forum New To Java
    Replies: 3
    Last Post: 10-03-2012, 08:15 AM
  2. Replies: 20
    Last Post: 03-22-2011, 04:02 AM
  3. For loops and while loops
    By patz in forum New To Java
    Replies: 1
    Last Post: 03-20-2011, 05:32 PM
  4. how to use do while loops
    By mikeitalydz in forum New To Java
    Replies: 32
    Last Post: 09-26-2009, 08:30 PM
  5. What are Instance variables and static variables?
    By sandeshforu in forum New To Java
    Replies: 3
    Last Post: 09-09-2009, 05:48 PM

Posting Permissions

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