add the numbers and print the output

add the numbers and print the output

There will be 5 numbers (each in a new line) written in a text file. Text file name will be ?input2.txt?. Your Java code should read that text file, add all the numbers and print the output in a new file named ?output2.txt?.

View Answers

March 26, 2012 at 11:03 AM

import java.io.*;
class ReadNumbersFromFile
{
    public static void main(String[] args) 
    {
        try{
        BufferedReader br=new BufferedReader(new FileReader("input.txt"));
        String str="";
        int sum=0;
        while((str=br.readLine())!=null){
            int num=Integer.parseInt(str);
            sum+=num;
        }
        br.close();
        BufferedWriter bw=new BufferedWriter(new FileWriter("output.txt"));
        bw.write(Integer.toString(sum));
        bw.close();
        System.out.println("Data has been written in the file.");
        }
        catch(Exception E){}
    }
}









Related Tutorials/Questions & Answers:
print numbers
print numbers  1234 123 12 1234   1234 123 12 1234
print numbers in traingle shape
print numbers in traingle shape  1 22 333 4444 i want output like this please help me   class Pyramid { public static void main(String[] args) { for(int i=1;i<=4;i++){ for(int j=1;j<
Advertisements
Application for print pyramid of numbers
Application for print pyramid of numbers  What is the code for printing the numbers like 1 1 1 1 2 2 1 1 3 4 3 1 1 4 7 7 4 1
how to add to numbers in java
how to add to numbers in java  how to add to numbers in java
print 100 numbers using loops
print 100 numbers using loops  how to print from 1 to 100 using for loop ?   Hi Friend, You can use the following code:ADS_TO_REPLACE_1... Numbers { public satic void main(string args[]_ { for(int a=1;a<
Add Two Numbers in Java
Add Two Numbers in Java     ... these arguments and print the addition of those numbers. In this example, args... into integer type. Now you can add these values into a sum variable and print
Applet for add two numbers
); add(text2); label3 = new Label("Sum of Two Numbers: "); label3.setBounds(20,80,130,20); add(label3); output = new TextField(5); output.setBounds(150,80,100,20); add(output
Print Output in C LAnaguage - Development process
Print Output in C LAnaguage  I want to Print Following Output in C Language with Combined For Loops Not Seprate Loops pleaseHelp Me
print the even numbers between the range 100-200
print the even numbers between the range 100-200  Q ?write an application that print out the even numbers between the range 100-200 ? 1- which aer not divisible by 7 and 5 ? 2- Number of the numbers which are not divisible
ModuleNotFoundError: No module named 'add_numbers'
ModuleNotFoundError: No module named 'add_numbers'  Hi, My Python... 'add_numbers' How to remove the ModuleNotFoundError: No module named 'add_numbers' error? Thanks   Hi, In your python environment
Add two big numbers
Add two big numbers       In this section, you will learn how to add two big numbers... that takes both numbers. Now, you will have to define it and use the add() method
C Program to Print Following Output - Development process
C Program to Print Following Output  Hello Sir I wnat to print Followning output in C Language with for loop How I Can Print it? 5 5 4 5 4 3 5 4 3 2 5 4 3 2 1  Hi Friend, Try the following: #include
How to print the following output using c program
How to print the following output using c program  1) 4 3 4 2 3 4 1 2 3 4 2) A B C D E F G H I J
How to print a equilateral triangle patren with numbers
How to print a equilateral triangle patren with numbers  I figure out how to use for loop to print the triangle but i couldn't get the exact values as this http://im37.gulfup.com/FzrOb.jpg could you please help me
How to add two numbers in Java?
this will print: Sum is: 30 So, in this program you learned how to sum(add) two numbers... How to add two numbers in a Java program? In Java you can easily add... programming you can make methods to add the numbers. In this tutorial we
how to write a program in java to print numbers in equalateral triangle
how to write a program in java to print numbers in equalateral triangle  the output must be 1 324 76589   Here is an example of pattern 1 2 3 4 5 6 7 8 9 Example: public class NumberTriangle{ public
write an application that print out the even numbers between the range 100-200
write an application that print out the even numbers between the range 100-200   write an application that print out the even numbers between... and determine the even numbers between them. class EvenNumbers { public static
how to sort the elements of hashmap and to print output the key and the value attached to it
how to sort the elements of hashmap and to print output the key and the value attached to it  how to sort the elements of hashmap and to print output the key and the value attached
add tow numbers with out using arthamatic operaters
add tow numbers with out using arthamatic operaters  add tow numbers with out using arthamatic operaters
Add Complex Numbers Java
How to Add Complex Numbers Java In this Java tutorial section, you will learn how to add complex Numbers in Java Programming Language. As you are already aware of Complex numbers. It is composed of two part - a real part and an imaginary
Add two big numbers - Java Beginners
Add two big numbers - Java Beginners  Hi, I am beginner in Java and leaned basic concepts of Java. Now I am trying to find example code for adding big numbers in Java. I need basic Java Beginners example. It should easy
Print
itself, representing standard output, is an instance of java.io.PrintStream class. Standard output is, on most operating systems, console output. println: It is a method of java.io.PrintStream class that lets you output some text
output
output  Sir,I am experiencing some problems with the output of this program.I got some useful help this website,but the output of the program isn't producing what it should.let me explain the output below is what happen when
JavaScript determine add, product of two numbers
JavaScript determine add, product of two numbers In this section, you... numbers. To find this, we have created two textboxes and four button...; function add(){ var num1=document.form.text1.value; var num2
print
print  How to print JFrame All Componant?   Please visit the following link: http://www.roseindia.net/java/example/java/swing/Print.shtml
What will be the Output?
What will be the Output?  int[] number = {5,3,6,10,1} int temp; for (int i=0; inumbers[j]) { temp=numbers[i]; numbers[i]=numbers[j] number[j]=temp
output variable
output variable  if i want to print output of three variables when i wrote the code e.g SYStem.out.PRINTln(answer + answer1 + answer 2) only output of answer is getting printed out? can you help
Print a statement
Print a statement  hello what would we output for this statement System.out.println ("5"+"A" + 3);   helloADS_TO_REPLACE_1 output will be 5A3
Numbers pyramid
Numbers pyramid  Hi sir, Can you please tell me how to output this using nested for loops? 1 2, 1 1, 2, 3 4, 3, 2, 1 1, 2, 3, 4, 5 6, 5, 4, 3, 2, 1 1, 2, 3, 4, 5, 6, 7 8, 7, 6, 5, 4, 3, 2, 1 1, 2, 3, 4, 5, 6, 7, 8, 9 10, 9, 8
automorphic numbers
automorphic numbers  how to find automorphic number in java   Hi Friend, Pleas visit the following link:ADS_TO_REPLACE_1 Automorphic numbers Thanks
sorting numbers
sorting numbers  How to sort the numbers in ascending order   import java.util.*; class SortNumbers{ public static void main(String...=input.nextInt(); list.add(num); } System.out.println("Numbers
Displaying checked checkbox in same page as well as print on another page ....also add next pages cheches checkbox into previous list
Displaying checked checkbox in same page as well as print on another page ....also add next pages cheches checkbox into previous list  1... checkboxes. So, i want such output, which can show previous pages selected checkbox
numbers
input output
Input And Output       Introduction The Java I/O means Java Input/Output and is a part... used for writes byte to output stream. It implements a buffered output
Prime Numbers - IDE Questions
Prime Numbers  Create a program that calculates the prime numbers from any inputted start and end range of values (e.g. 5 and 28) and print the prime numbers in rows of 10. Hint: Use a counter to count how many values are printed
Prime Numbers
Prime Numbers  Create a complete Java program that allows the user to enter a positive integer n, and which then creates and populates an int array with the first n prime numbers. Your program should then display the contents
print without newline in python
") But the output is coming in new lines. I want to know how to print without...("Python") You can use the end="" with print() method. Here is output...print without newline in python  Hi, I have a Python program
print without newline in python
") print("Python") But the output is coming in new lines. I want to know how...print without newline in python  Hi, I have a Python program in which it prints strings. But the output is coming in new line. But I want
Output of flex
Output of flex  hi....... please provide the name of the output file. What is the output of flex applications? please rply ASAP........ Thanks
triangle output
triangle output  program to get the following output
Rational Numbers
Rational Numbers   Write and fully test a class that represents rational numbers. A rational number can be represented as the ratio of two integer values, a and b, where b is not zero. The class has attributes for the numerator
Sum of positive and negative numbers
that would be great Write a program to read in 10 integer values and output...; import java.util.*; class Numbers { public static void main(String...=0,sum2=0; int num[]=new int[10]; System.out.println("Enter 10 numbers
what is the output?
what is the output?  public class MyAr{ public static void main(String argv[]) { int[] i=new int[5]; System.out.println(i[5]); } }   It gives ArrayIndexOutOfBoundsException as you haven't add element to array. Correct
Random numbers - Development process
Random numbers  hi, How to generate unique random numbers between range like(10 to 50)  Hi friend, class RandomNumber { public... to this number int aNumber = (int) (Math.random() * 40 + 10); //print
Listing all even numbers between two numbers
Listing all even numbers between two numbers  Hi, How to write code to list all the even numbers between two given numbers? Thanks   Hi, You function will take 2 numbers and the find the even numbers between
ModuleNotFoundError: No module named 'print_print'
ModuleNotFoundError: No module named 'print_print'  Hi, My Python... 'print_print' How to remove the ModuleNotFoundError: No module named 'print_print' error? Thanks   Hi, In your python environment
triangle output
triangle output  program to get the following output: * *   Hi Friend, Please specify the pattern properly.ADS_TO_REPLACE_1 Thanks
What is the output if....
What is the output if....  Here is my code. what is the output...;When we put 50 as a lower limit value, output comes: (adsbygoogle... violation. After the catch block When we put 150 as a lower limit value, output
output of the program
output of the program  public static int sum(List list) { int sum = 0; for ( Iterator iter = list.iterator(); iter.hasNext(); ) { int i = ((Integer)iter.next()).intValue(); sum += i; } return sum
add
How to add two int numbers in Java Example  How to add two int numbers in Java Example  Here is a java example that accepts two integer from the user and find their sum. import java.util.*; class AddNumbers

Ads