1.11M Members

how to find largest number?

 
0
 

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

 
0
 

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.

 
0
 

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...

 
-1
 
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);
}
}
 
0
 

@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];
You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article