Question

Get doubles from input file. Output the biggest. Answer the comments throughout the code. import java.io.File;...

Get doubles from input file. Output the biggest. Answer the comments throughout the code.

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Lab8Num1 {
public static void main(String[] args) {
//Declaring variable to be used for storing and for output
double biggest,temp;
//Creating file to read numbers
File inFile = new File("lab8.txt");
//Stream to read data from file
Scanner fileInput = null;
try {
fileInput = new Scanner(inFile);
} catch (FileNotFoundException ex) {
//Logger.getLogger(Lab10.class.getName()).log(Level.SEVERE, null, ex);
}
//get first number and make it the biggest;
biggest=fileInput.nextDouble();
//loop through the file
while(fileInput.hasNextLine())
{
//getting doubles and comparing with current "biggest"
temp=fileInput.nextDouble();
if(temp>biggest)
biggest=temp;
}
System.out.println("The biggest number was " + biggest);
}
}

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

Solution:

The code provided in question is conceptually correct. Also the syntax seems correct. I have added better comments to help you understand the code properly.

also you might be making some errors while executing it. errors might be as follows

  1. wrong syntax for execution.
  2. file name not matching or location of added file name is not correct. (input file in code)

i have added the correct syntax for execution in my output screenshot, please go through that.

Also, it will help, if you store lab8.txt file in the same location as your program

code with changed comments

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

import java.io.File;

import java.io.FileNotFoundException;

import java.util.Scanner;


class Lab8Num1 {

public static void main(String[] args) {

double biggest,temp; //variables that we are using to

File inFile = new File("lab8.txt"); //we are creating a file object from file class which will read our file


Scanner fileInput = null;

/*this is a try catch block to handle exception i.e if you dont find the file this will raise

an exception */

try {

fileInput = new Scanner(inFile);

} catch (FileNotFoundException ex) {

}

// we are getting a stream of values from our file

biggest=fileInput.nextDouble(); // the first number that we get from our file has been initializes biggest.

//loop through the file

// this while loop will find the biggest number

while(fileInput.hasNextLine())

{

// the incoming numbers from files are stores in temp variable

temp=fileInput.nextDouble();

if(temp>biggest)// we are checking if the value of temp is larger than biggest.

biggest=temp;//if the above statement is true we are updating the value of biggest with temp

}

System.out.println("The biggest number was " + biggest); // print out the biggest number.

}

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

code screenshots:

Output Screenshot:

please upvote if you found this answer useful

Thank you

Add a comment
Know the answer?
Add Answer to:
Get doubles from input file. Output the biggest. Answer the comments throughout the code. import java.io.File;...
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
  • how would i use test.add because i would like to use add import java.io.File; import java.io.FileNotFoundException;...

    how would i use test.add because i would like to use add import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Scanner; public class checkFruit { private List<String> tests; public List<String> getFruit(String fruits) { tests = new ArrayList<String>(Arrays.asList(fruits.split(","))); for (String test : tests) { System.out.println(test); tests.add()//how would i use add here } return tests; } public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter file name: "); File file = new File(in.nextLine()); try { checkFruit...

  • Can anyone debug this for me please? Java code to copy package debugmeone; import java.io.File; import...

    Can anyone debug this for me please? Java code to copy package debugmeone; import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class ReadTextFile { private Scanner input; // Ignore the hint given by NetBeans public void openFile() { try { input = new Scanner( new File("accountrecords.txt")); } catch(Exception e) { System.out.println("Something bad just happened here."); System.exit(707); } catch( FileNotFoundException fnfe) { System.out.println("Error - File Not Found: accountrecords.txt"); System.exit(100); } } } ITCS-2590 Debugging Execse Chapter 11 - NetBeans IDE 8.2 File...

  • Problem: Use the code I have provided to start writing a program that accepts a large...

    Problem: Use the code I have provided to start writing a program that accepts a large file as input (given to you) and takes all of these numbers and enters them into an array. Once all of the numbers are in your array your job is to sort them. You must use either the insertion or selection sort to accomplish this. Input: Each line of input will be one item to be added to your array. Output: Your output will...

  • Swing File Adder: Build a GUI that contains an input file, text box and Infile button....

    Swing File Adder: Build a GUI that contains an input file, text box and Infile button. It also must contain and output file, text box and Outfile button. Must also have a process button must read the infile and write to the outfile if not already written that is already selected and clear button. It must pull up a JFile chooser that allows us to brows to the file and places the full path name same with the output file.  Program...

  • I need help debugging this Java program. I am getting this error message: Exception in thread...

    I need help debugging this Java program. I am getting this error message: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2 at population.Population.main(Population.java:85) I am not able to run this program. ------------------------------------------------------------------- import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Scanner; /* Linked list node*/ class node { long data; long year; String country; node next; node(String c,long y,long d) { country=c; year=y; data = d; next = null; } } public class Population { private static node head; public static void push(String...

  • Computer Science - Java Explain the code step by step (in detailed order). Also explain what...

    Computer Science - Java Explain the code step by step (in detailed order). Also explain what the program required. Thanks 7 import java.io.File; 8 import java.util.Scanner; 9 import java.util.Arrays; 10 import java.io.FileNotFoundException; 12 public class insertionSort 13 14 public static void insertionSort (double array]) 15 int n -array.length; for (int j-1; j < n; j++) 17 18 19 20 21 double key - array[j]; int i-_1 while ( (i > -1) && ( array [i] > key array [i+1] -...

  • Complete the code: package hw4; import java.io.File; import java.io.IOException; import java.util.LinkedList; import java.util.Scanner; /* * This...

    Complete the code: package hw4; import java.io.File; import java.io.IOException; import java.util.LinkedList; import java.util.Scanner; /* * This class is used by: * 1. FindSpacing.java * 2. FindSpacingDriver.java * 3. WordGame.java * 4. WordGameDriver.java */ public class WordGameHelperClass { /* * Returns true if an only the string s * is equal to one of the strings in dict. * Assumes dict is in alphabetical order. */ public static boolean inDictionary(String [] dict, String s) { // TODO Implement using binary search...

  • 1. Import file ReadingData.zip into NetBeans. Also, please download the input.txt file and store it into...

    1. Import file ReadingData.zip into NetBeans. Also, please download the input.txt file and store it into an appropriate folder in your drive. a) Go through the codes then run the file and write the output with screenshot (please make sure that you change the location of the file) (20 points) b) Write additional Java code that will show the average of all numbers in input.txt file (10 points) import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; import java.io.*; 1- * @author mdkabir...

  • See notes in Lab4T1SHYPI.java Use the program Lab4T1SHYPL.java to answer the following questions. Part 1: Create...

    See notes in Lab4T1SHYPI.java Use the program Lab4T1SHYPL.java to answer the following questions. Part 1: Create a new program XYZ1234Lab4T1Part1.java and copy the body from Lab4T1SHYPIjava into the new program. In this new program, make the (29 points total a 1.a) Replace the sets of variables with arrays for each kind of data. Ex. shPubl, shPub2, shPub3, and shPub4 should be replaced with a single array StringIl shPub. Replace the variables for superHeros, shYear, shIssue, and shMovi Arrays should hold...

  • Write a Java method that will take an array of integers of size n and shift...

    Write a Java method that will take an array of integers of size n and shift right by m places, where n > m. You should read your inputs from a file and write your outputs to another file. Create at least 3 test cases and report their output. I've created a program to read and write to separate files but i don't know how to store the numbers from the input file into an array and then store the...

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