this is my java coding:

import java.util.Scanner;
import java.io.*;
public class readNumbers
{
public static void main(String[]args) throws IOException
{
 int num;
 String filename;
 Scanner keyboard = new Scanner(System.in);
System.out.print("Enter the filename: ");
filename=keyboard.nextLine();
FileWriter fwriter=new FileWriter(filename);
PrintWriter outputFile=new PrintWriter(fwriter);
for(int i=1; i<=5; i++)
 {
 System.out.print("Enter a number: ");
 num=keyboard.nextInt();
 outputFile.println(num);
keyboard.nextLine();
 }
 outputFile.close();
int sum;
String str;
double average;
FileReader freader=new FileReader(filename);
 BufferedReader inputFile=new BufferedReader(freader);
sum=0;
str=inputFile.readLine();
 while(str!=null)
 {
	 sum=sum+Integer.parseInt(str);
                 str=inputFile.readLine();
 }
 inputFile.close();

 System.out.println("The sum is: "+sum);

 average=sum / 5.0;
 System.out.println("the average is: "+average);
}
}

i dont know how to write the coding to find the largest number..
anyone know how to do it?
thanks

Recommended Answers

All 4 Replies

You have a variable there that keeps track of the sum of the numbers. Using the same pattern, make another variable that keeps track of the largest number seen so far. Initialize it with the value Integer.MIN_VALUE.

Well, you're going to need a variable to keep track of the largest number entered so far. If you think about it a minute, updating that variable should be pretty easy in your input loop.

On a side note, never throw exceptions from main(). Use a try-catch block.

Also, use [code] [/code] tags when posting code so that formatting and indentation is preserved.

edit: Bah, was replying when the above was posted...

import java.util.Scanner;
import java.io.*;
public class readNumbers
{
public static void main(String[]args) throws IOException
{
int num;
String filename;
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter the filename: ");
filename=keyboard.nextLine();
FileWriter fwriter=new FileWriter(filename);
PrintWriter outputFile=new PrintWriter(fwriter);
for(int i=1; i<=5; i++)
{
System.out.print("Enter a number: ");
num=keyboard.nextInt();
outputFile.println(num);
keyboard.nextLine();
}
outputFile.close();
int sum;
String str;
double average;
FileReader freader=new FileReader(filename);
BufferedReader inputFile=new BufferedReader(freader);
sum=0;
str=inputFile.readLine();
int max=0;

while(str!=null)
{int temp=Integer.parseInt(str);
if(max>temp)
max=temp;
sum=sum+Integer.parseInt(str);
str=inputFile.readLine();
}
inputFile.close();

System.out.println("The sum is: "+sum);

average=sum / 5.0;
System.out.println("the average is: "+average);
System.out.println("the Max is: "+max);
}
}
commented: What is the point of this post? -2

@Doleh: where's your explanation? writing copy-me code won't help any one to learn anything.
@rayda: either you keep track of the largest number, as Ezzaral suggested, or you store all the given (inputted) numbers into an array.
iterate over tjat array and:

if ( arrayNumbers[position] > maxNumber)
  maxNumber = arrayNumbers[position];
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.