Question

Create a text file named “file1.txt” (by use of the notepad editor in Windows for instance)...

Create a text file named “file1.txt” (by use of the notepad editor in Windows for instance) containing the following integer values, one per line:

​12 5 13 56 90 52 82 52


Write a Java program that reads these values from the file and displays their sum on the screen.


0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.io.*;
import java.util.*;
//public class
public class fileNumSum {

public static void main(String args[]){


//scanner
Scanner s=new Scanner(System.in);
//reading filename
System.out.println("Enter the file name :: ");
String name;
name=s.nextLine();
//opening file
try{


File f = new File(name);
Scanner scanner = new Scanner(f);
int sum = 0;
//reading each line
while (scanner.hasNext()){
//adding number to sum
sum += scanner.nextInt();

}
//printing the sum
System.out.println("Sum : "+sum);


}catch(Exception err){
  
}
//end of program


}


}

Add a comment
Know the answer?
Add Answer to:
Create a text file named “file1.txt” (by use of the notepad editor in Windows for instance)...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Question 2: Assume that a text file containing a series of integers is named numbers.txt and...

    Question 2: Assume that a text file containing a series of integers is named numbers.txt and exists on the c:Temp. Write a program that reads all of the numbers stored in the file and calculates their total. In order to test this program, you may create a text file using notepad. No GUI is necessary. Name the program number.py

  • Completion is a screenshot of the batch file showing all of the steps to create the file system s...

    Completion is a screenshot of the batch file showing all of the steps to create the file system shown below. Instructions Create a batch file clifilesystem.cmd using Notepad which performs the following steps to create the file system shown below. Also use a proper comment for each step before the command. Proof your work. Finally submit the clifilesystem.cmd file in Blackboard Figure 1. The clijfilesystem.cmd file system Work from Desktop unless using an absolute path Desktop NOS110 OS Labs Homework...

  • C#: Create a file that contains your favorite movie quote. Use a text editor such as...

    C#: Create a file that contains your favorite movie quote. Use a text editor such as Notepad and save the file as Quote.txt. Copy the file contents and paste them into a word-processing program such as Word. Save the file as Quote.doc. Write console application that displays the sizes of the two files as well as the ratio of their sizes to each other. To discover a file's size, you can create a System.IO.FileInfo object using a statement such as...

  • a. Provide me with your code file, output file and the text file. 1. Create a...

    a. Provide me with your code file, output file and the text file. 1. Create a file with a series of integers. Save it as numbers. txt. Write a program that reads all the numbers and calculates their sum . 2. Create a file having different integers than the first one. Save it as numbers1. txt . Write a program that reads all the number and calculates their average. Important: The two files that you are creating will contain different...

  • - Create a folder called pgm1 - In your folder pgm1, USING A WINDOWS EDITOR, create...

    - Create a folder called pgm1 - In your folder pgm1, USING A WINDOWS EDITOR, create a java program named Your lastName, firstLetterOfYourFirstName, pgm1W.java that will do the following: Start here - From your main method call a new method named: processArray - In the processArray method, create one 10x10 two dimensions array named twoDarray - In each index of your twoDarray, load the multiplication of its corresponding xy locations - Using the printf java command display, in a perfect...

  • Create a java program that reads an input file named Friends.txt containing a list 4 names...

    Create a java program that reads an input file named Friends.txt containing a list 4 names (create this file using Notepad) and builds an ArrayList with them. Use sout<tab> to display a menu asking the user what they want to do:   1. Add a new name   2. Change a name 3. Delete a name 4. Print the list   or 5. Quit    When the user selects Quit your app creates a new friends.txt output file. Use methods: main( ) shouldn’t do...

  • Write a Java program that reads words from a text file and displays all the non-duplicate...

    Write a Java program that reads words from a text file and displays all the non-duplicate words in ascending order. The text file is passed as a command-line argument. Command line argument: test2001.txt Correct output: Words in ascending order... 1mango Salami apple banana boat zebra

  • Java Exceptions Suppose a library is processing an input file containing the titles of books in...

    Java Exceptions Suppose a library is processing an input file containing the titles of books in order to remove duplicates. Write a program that reads all of the titles from an input file called bookTitles.inp and writes them to an output file called uniqueTitles.out. When complete, the output file should contain all unique titles found in the input file. Create the input file using Notepad or another text editor, with one title per line. Make sure you have a number...

  • I need help parsing a large text file in order to create a map using Java....

    I need help parsing a large text file in order to create a map using Java. I have a text file named weather_report.txt which is filled with hundreds of different indexes. For example: one line is "POMONA SUNNY 49 29 46 NE3 30.46F". There are a few hundred more indexes like that line with different values in the text file and they are not delimited by commas but instead by spaces. Therefore, in this list of indexes we only care...

  • Homework 1- Merge Files: 1. Design a class named MergeFiles 1 Three file names are passed...

    Homework 1- Merge Files: 1. Design a class named MergeFiles 1 Three file names are passed as command-line arguments to MergeFiles as follows: java MergeFiles filel file2 mergedFile II. MergeFiles reads names stored in files file and file2 III. Merges the two list of names into a single list IV. Sorts that single list V. Ignores repetitions VI. Writes the sorted, free-of-repetitions list to a new file named mergedFile.txt VII. The names in all three files are stored as one...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT