• Log in or sign up for an account here: https://www.sythe.org/login

[tut] Calculating average (not GUI)

Blupig

BEEF TOILET
Joined
Nov 23, 2006
Sythe Gold
1,609
Messages
7,145
Age
33
Location
Canada
[tut] Calculating average (not GUI)
Code:
public class Average {
    
    public static void main(String[] args) {
    	
    	int first = 5;
    	int second = 7;
    	int third = 3;
    	
    	double avg = (first + second + third)/3;
    	
    		System.out.print(avg);
    	

    }
}

The first 3 lines of code for main are declaring 3 integers, which equal different numbers. These will be the 3 numbers used in the average calculation. Then, the 4th line is the double (decimal number) that the average will be, so in this case it's all the integers (ints) added together and divided by 3 (the number of ints). Then, they are printed out.
 
[tut] Calculating average (not GUI)
What if I want to take an average of non ints! Give it user input when its running... It would be better that way. :D
 
[tut] Calculating average (not GUI)
What if I want to take an average of non ints! Give it user input when its running... It would be better that way. :D

I'm still learning Java myself and haven't learned how to do that yet :(
 
[tut] Calculating average (not GUI)
Code:
import java.util.*;


public class Average 
{

	static Scanner s = new Scanner(System.in);

	public static void main(String[] args) 
	{

		System.out.print("Enter 1rst int:");
		int first = s.nextInt();
		System.out.print("Enter 2nd int:");
		int second = s.nextInt();
		System.out.print("Enter 3rd int");
		int third = s.nextInt();

		double avg = (first + second + third) / 3;

		System.out.print(avg);

	}
}

There ya go. You need to use a scanner to do it.

s.nextInt(); looks for an int input, and if its not an int it throws an error.

Here is a method that stops the error. D:

Code:
public static int getInput(String output) 
	{
		while (true) 
		{
			try 
			{
			System.out.print(output);
			return s.nextInt();
			} 
			catch (InputMismatchException e) 
			{
				s.next();
			}
		}

	}

the parameter is what text you want displayed like "Enter int A:" It basicly looks for the error which is if you put the wrong input in, if it finds it, the program exacutes the catch statement which takes the next input what ever it is, then the while loops jumps back to the start and begins again until it can get out threw the return statement. To use my method, do this


Code:
int first = getInput("Enter A:");


Hope you learned something. :D
 
[tut] Calculating average (not GUI)
SuF, I ran your code and it didn't work.
Do I have to import something or what?
It said: Cannot find symbol Scanner.
Thanks.


EDIT: Haha, of course, just added import java.util.*; and it worked.. my bad.. , your bad too! Hehe.
 
[tut] Calculating average (not GUI)
fuck, forgot to put the import... thought i had... o.O
 
[tut] Calculating average (not GUI)
Code:
public class Average {
    
    public static void main(String[] args) {
	
	static Scanner s = new Scanner(System.in);
	
	int ctr=0;
	double avg=0;
	for (int ctr=0; ; ctr++)
    	{
		System.out.print("Enter int #" + ctr + ":");

		try {avg+= s.nextInt();}
		catch (InputMismatchException e) 
		{break;}
	}
    	
	System.out.print(avg/ctr);
    	

    }
}

would this work? havent done java in forever :( (downloading eclipse right now ^^ only 10 hours left)
 
Runescape Gamblingrunescape gambling
Back
Top