Can you help me my assignment? I am having trouble with this script program. i t
ID: 3770377 • Letter: C
Question
Can you help me my assignment? I am having trouble with this script program. i tried adding the number as told to do so in the directions but i am getting stuck somewhere. The directions and script is below. Thank you for any help!!
Modify the script to read from a file rather than the keyboard. Use the techniques learned from the chapter and the prior tutorials. Be sure to:
Replace the code under the # Read the input values comment with your own code to open and read from a file, appending the numbers in the file to the values list.
Skip the requirement that the data ends when ‘Q’ is read .. you can just use a for loop to read the data. The for loop will stop when the end of file is reached.
Print an error message if the file could not be opened.
Using a text editor or Idle, create an input file. The filename should be “numbers.txt” and contain a list of ten numbers, one per line, in no particular order. You can use the sample numbers shown on page 2.
##
# This program reads a sequence of values and prints them, marking the largest value.
#
# Create an empty list.
values = []
# Read the input values.
print("Please enter values, Q to quit:")
userInput = input("")
while userInput.upper() !="Q" :
values.append(float(userInput))
userInput = input("")
# Find the largest value.
largest = values[0]
for i in range(1, len (values)):
if values[i] > largest :
largest = values [i]
# print all values, marking the largest.
for element in values :
print(element, end="")
if element == largest :
print(" <== largest value" , end="")
print()
A sample series of numbers to place in the numbers.txt file to test the script.
!0
23.5
1
-50
256
5
92
256
9
5
By the way, did you remember to have your code close the file when done reading? (You can skip doing that if you used the “with” syntax shown in the tutorial.)
Explanation / Answer
import java.util.Scanner;
import java.io.IOException;
import java.util.ArrayList;
import java.util.*;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.List;
import java.io.*;
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.