import java.io.*;
class Ex33
{
	public static void main(String[] args)
	{
		Console console=System.console();
		System.out.println("Please enter a number");
		String input;
		input=console.readLine();
		int n1;
		n1=Integer.parseInt(input);
                double sum;
                int LessThanAv;
                
              
                for(count=0;count<100;count++)
                {    sum=(n1+sum)/count;
                     System.out.println("Please enter the next number");   
                     
                }

              while(n1<sum;)
              { 
              System.out.println(n1);
              }
          }

}

Edited 6 Years Ago by peter_budo: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)

NORM, what i am trying to do:

my task is to compose a code, where the user needs to feed 100 numbers. all those numbers need to be averaged out at the end. when i get the final average, i need to filter out all the numbers that he typed that are above the average.


here is my code so far. what i dont get , is how to store all the numbers that he typed and then apply them second time to compare against the final average.

@NewOrder
Use Code Tags the next time you post code.

Now when it comes to your program, first store all the numbers entered by the user in an Array. So you can perform processing on them later.

Calculating the Average is simple, just sum up all the numbers
The following chunk of code shouts out loud that you haven't given it clear thought or run your program even once.

for(count=0;count<100;count++){ 
  sum=(n1+sum)/count;

The code would always throw you a divide by zero error, as count is equal to 0 at the start of your loop and your program execution would terminate.

To calculate the average, take the sum of all the numbers the user entered (which you have stored in an array, using a loop) and then divide that total by the count of numbers entered.

Once you have calculated the average, once again parse through the entire array of numbers using a loop. Inside the loop use a switch or an If statement, to find out whether the number at the given position in the array is above average and print it.

first part is done. i think i might use another loop or an equation to filter the numbers above avarage

import java.io.*;
class Ex33
{
	public static void main(String[] args)
	{
		Console console=System.console();
                System.out.println("Please enter the next number");  
                String input;
                input=console.readLine(); 
		int n1;
                n1=Integer.parseInt(input);
                int sum=0;
                int sum2=0;
                int input2=0;
                input2=n1;
                int count=1;
              
                while(count<=5)
                           

                {    System.out.println("Please enter the next number");
                     input=console.readLine();
                     n1=Integer.parseInt(input);
                     sum=(n1+sum);
                   
                     count++; 
                }
                  sum2=sum/5;
                  System.out.println(sum2);
                 


                
                          while(count<=5)// this isnt calculated by java. why?
                          {if(n1<sum2)
                         System.out.println(n1);
                           else
               
                              count++;
                                      }        
             
          }

}

Edited 6 Years Ago by NewOrder: n/a

Again you have posted code with no explanation.
Does this code do what you want it to?
If not, show its output and explain what is wrong and what you'd like for it to output.

it should look like that..

there is a question.. the user puts a number

there are 5 questions and 5 inputs

now the program should sum all of the inputs and convert them to an average

the average will be a variable.

now all his 5 answers will be evaluated again against the new sum. numbers that are smaller than sum should be printed

Ok, that is what the program is supposed to do, now
Please show its output and explain what is wrong and what you'd like for it to output.

i can get the average of the input.

but i need to keep a record of everything the user types... (All his numbers)

i need that record to compare to the average of all the numbers he used

I STORED THEM. BUT HOW DO I CONVERT THEM BACK TO NUMBERS.

import java.io.*;
class Ex33
{
	public static void main(String[] args)
	{
		Console console=System.console();
                System.out.println("Please enter the next number");  
                String input;
                input=console.readLine(); 
		int n1;
                n1=Integer.parseInt(input);
                int sum=0;
                int sum2=0;
                int input2=0;
                input2=n1;
                int count=1;
                int  DummyVariable=0;
                
                
                String All="";
              
               
              
                
                while(count<5)// this is 5 only for practice. it should be 100 instead!
                           

                {    System.out.println("Please enter the next number");
                     input=console.readLine();
                     n1=Integer.parseInt(input);
                     sum=(n1+sum);
                     All+=n1+",";   
                     count++; 
                }
                   
                   sum2=sum/4;
                  System.out.println("Your average is"+" "+sum2);
                  System.out.println(All);
                  int AllNumbers;  
                  AllNumbers=Integer.parseInt(All);//i dont know how to convert string to numbers
                  int count3=1;
                          
                         while(count3<5)// this is 5 only for practice. it should be 100 instead!
                          {if(AllNumbers<sum2)  
                           System.out.println(n1);
                 
                             else 
                             DummyVariable++;
                          count3++;}   
                  }
}
Comments
NO YOU DID NOT

AllNumbers=Integer.parseInt(All);//i dont know how to convert string to numbers

The parseInt() converts a String to an int.

What is this program supposed to do? Add comments to the code describing what it does so that we can see and not have to scroll back to previous parts of this post to find out.

Some quick tips:

1) you can use sum += n1; instead of sum = sum + n1;

2) sum2 (the average) should be a float/double value unless you truly do not care about the decimal expansion

3) if you have an array int[] vals = new int[100]; then you can use the methods in class Arrays to do a quicky reduction:

Arrays.sort(vals); // sorts the integers in ascending order

// find the index where value is greater than the average (I assume 
// here that you have the average as a double value already)
int index = 0;
boolean found = false;
while( !found )
{
    if( (double)vals[i] < average )
    {
         index++;
    }
    else
    {
         found = true;
    }
}

// and then create the new array with values all less than the average
int[] lowerVals = Arrays.copyOf(vals,index);

@fresidue Sometimes the instructor wants the student to know how to work his way thru an array manually. You do need to know this. Using tools like the sort() method and copyOf() method doesn't let the student learn how to manipulate an array.

Comments
Exactly

I STORED THEM. BUT HOW DO I CONVERT THEM BACK TO NUMBERS.

Nice attitude, but you might remember I mentioned something about ARRAYs and even pasted the same link to you twice.
You are instead using a String to store your numbers,hence the pains of getting them back. Heres a few pointers:-

A variable example 'n1' on Line 10 of the code you posted, can hold only ONE value at a time, if you do 'n1=10', then it stores the value 10, next if you do 'n1=92', it stores the value 92, the value '10' is overwritten and you can't get it back, thats what is happening in your first time code you posted. In every iteration of your first while, the statement n1=Integer.parseInt(input); was overwriting the old value entered by the user (in the previous iteration) with the new value, hence at the end of the loop, 'n1', would contain only the last value.
An Array on the other hand can store a group of values. Example int[] n = new int[10] , now 'n' can store 10 different integer values at the indices from 0 to 9. Heres an Example:-

int[] n = new int[10];
int index = 0;
Console console=System.console();
while (index < n.size) {
  input=console.readLine();
  n[index]=Integer.parseInt(input);
  index = index + 1;
}

The above chunk of code would read 10 numbers from the console and store them in your array. Suppose this should give you clear idea on Arrays and *hopefully* you will visit the link in my previous posts.


Anyway if you plan to continue on your current path, then Integer.parseInt(), can get you number from a String only if the String contains only a number i.e. you can extract the integer value 10 from the String which contains only "10", but your String would contain multiple numbers in the format 10,8,1 etc. You will need to split the String on "," and feed the individual Strings ("10","8","1", etc) to the Integer.parseInt() method.

Edited 6 Years Ago by stephen84s: n/a

This article has been dead for over six months. Start a new discussion instead.