Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Your turn to code. We are going to create a program that will read numbers from a file and find the sum of all the


Your turn to code. We are going to create a program that will read numbers from a file and find the sum of all the numbers added together. Create a java file named NumbersList.java and copy the code below.

Create a .txt file, using JGrasp or Notepad. Type some numbers or use the following

0 1 1 2 3 5 8 13 21 34 55 89 144 233

Then save the file as numbers.txt. Follow the comments to get the project working.

import java.util.*;

import java.io.*;

class NumberList{

public static void main(String [] args){

try{

int sum = 0;

//1. Declare a file to open the numbers.txt file

//2. Create an instance of the scanner to scan the file you just created

//3. Complete the while loop to continue reading so long as the file has more values

while( ){

//4. Complete the statement below to store the next int in the file into num.

int num =

//This command prints out the number to make sure it is working.

System.out.println (\"num: \" + num);

//This line of code sums up all the numbers.

sum += num;

}

//This Displays the sum

System.out.println(\"The Sum is: \" + sum);

//5. Close the Scanner object

}

//If any other error occurred, display the message

catch(Exception e){

System.out.println(\"Error: \" + e.getMessage());

}


















Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Mobile Communications

Authors: Jochen Schiller

2nd edition

978-0321123817, 321123816, 978-8131724262

More Books

Students also viewed these Programming questions