Home Answers Viewqa Java-Beginners add the numbers and print the output

 
 


ganesh pardeshi
add the numbers and print the output
1 Answer(s)      a year and 2 months ago
Posted in : Java Beginners

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 Pages:
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<
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 numbers
print numbers  1234 123 12 1234   1234 123 12 1234
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
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...)); } } Output -2-3i -4-5i Addition of Complex Numbers are :-6-8i
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 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
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
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
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: class Loop...); } } } Thanks   class Numbers { public satic void main
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
Formatted Output
Java NotesFormatted Output Java 5 implements formatted output with printf.... Taming Tiger: Formatted output Some simple examples format(). Amazingly, there was no built-in way to right justify numbers in Java until Java 5. You had to use
add
How to add two numbers in Java  add two number   Here is a java example that accepts two integer from the user and find their sum. import java.util.*; class AddNumbers { public static void main(String[] args
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
QUE 50 ...please show output also...
and increment of 3 a. Add 3 string objects b. Print the content of vector c. Print size and capacity d. Add 4 integer objects in the vector...QUE 50 ...please show output also...  Q1. Write a program to find
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
add tow numbers with out using arthamatic operaters
add tow numbers with out using arthamatic operaters  add tow numbers with out using arthamatic operaters
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
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
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
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
I/O Program output error
not know how to take all the AgentID's and add their pValues together to output...I/O Program output error  Hello All, I am working on a program that requries me to read and analyze a .txt file and output the results to a .txt
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
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
Java write even numbers to file
have used print() method to store the even numbers to the file. println...Java write even numbers to file In this section, you will learn how to write the even numbers to file. By using the PrintWriter class, you can write any type
JavaScript array of numbers
print the list of arrays of numbers.          ... JavaScript array of numbers   ... in understanding 'Java Script array of numbers'. For this we use  Java
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
Print a statement
Print a statement  hello what would we output for this statement System.out.println ("5"+"A" + 3);   hello output will be 5A3
PHP Numbers Variables
PHP Variables Numbers We already define that variables are used to store the values or information in the form text strings, numbers or array. A variable... you can used again and again.  You can also assign numbers in PHP variable
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..., it does not display previous page's selected checkboxes. So, i want such 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
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
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
Calculate sum of even and odd numbers in Java
; System.out.println("Sum of odd numbers is: " + sum2); } } Output Sum of even...Calculate sum of even and odd numbers In this section, you will learn how to read the file that contains even and odd numbers and calculate their sum
prime numbers application
prime numbers application  how to add a comment indicating that the method will determine if a number is a prime number. In the next line add... will be named number. Add a left brace on next line. in the body of the method add
Java Pyramid of Numbers
Java Pyramid of Numbers  Hi, I want to know how the code to print the pyramid below works. It uses nested for loops. Pyramid: 1 2 1 2
how to print a new line after a certain number of prints
how to print a new line after a certain number of prints  How do I make this program print the inputs the user gives 5 per line? So in other words I want the program to print 5 input numbers per line. this is my code so far
output error - JSP-Servlet
output error  /*hi friends, the given below is my servlet program... javax.servlet.ServletException: Wrapper cannot find servlet class numbers or a class it depends... java.lang.ClassNotFoundException: numbers
Convert Roman Numbers to Decimal Numbers
Convert Roman Numbers to Decimal Numbers In the previous section, we have illustrated the conversion of Decimal Numbers to Roman. Here we are doing vice... is: " + decimal); } } Output: Enter a Roman Number: XV
Input and Output package
she used in.readint() out.writeln() commands to read input and print output. she created two new objects directly to use this statements. /* input and output...Input and Output package  Hi guys, My professor used input and output
C Print Pascal Triangle
C Print Pascal Triangle In this section, you will learn how to display... enumerated starting with row 0 which is having only the number 1. For other rows, add... in the first row is 0 + 1 = 1,whereas the numbers 1 and 2 in the second row are added
How to Print a Stack Trace Message
How to Print a Stack Trace Message    ... getMessage() method that is used with an object of the Exception class to print... information about the error process if you print a stack trace from the exception
Printing numbers up to N into a file
Printing numbers up to N into a file  I'd like to print the first N integers, that is, "1, 2, 3, 4, ..., N-1, N", say N equals 1000, or 10000 or whatever. I'd also like to have the result stored as a file instead of having
Find consecutive numbers whose sum is equal to input number.
Find consecutive numbers whose sum is equal to input number. In this section, you will learn how to display the consecutive natural numbers whose sum is equal... possible combination of consecutive naturalnumbers which add up to give the N
Generate array of random numbers
Generate array of random numbers You can generate a random number either...() but it is better to use Random class. If you want to generate a series of random numbers.... In this section, we are going to accept 10 numbers and display 5 numbers from them
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
print a rectangle - Java Beginners
print a rectangle  how do I print a rectangleof stars in java using...()); System.out.print("The number of triangles you want to output in each row...("The number of rows of triangles you want to output : "); int noOfRows

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.