1,105,320 Community Members

how to find largest number?

Member Avatar
rayda
Newbie Poster
22 posts since Jan 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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

Member Avatar
Rashakil Fol
Super Senior Demiposter
2,596 posts since Jun 2005
Reputation Points: 982 [?]
Q&As Helped to Solve: 209 [?]
Skill Endorsements: 42 [?]
Team Colleague
 
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.

Member Avatar
Ezzaral
Posting Sage
7,431 posts since May 2007
Reputation Points: 2,714 [?]
Q&As Helped to Solve: 953 [?]
Skill Endorsements: 31 [?]
Moderator
Featured
 
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...

Member Avatar
doleh
Newbie Poster
9 posts since Jul 2008
Reputation Points: -2 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
-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);
}
}
Member Avatar
stultuske
Posting Expert
5,290 posts since Jan 2007
Reputation Points: 935 [?]
Q&As Helped to Solve: 774 [?]
Skill Endorsements: 35 [?]
Featured
 
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 three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article