java question: Write a program that reads in data from a text file named in.txt. Compute the sum of all the valid integers in the input file. Likewise, compute the sum of all the valid doubles in the input file. Write the former to an output file called int_total.txt, and write the latter to an output file called double_total.txt. B) Write a program that converts the Java source code from the next-line brace style to the end-of-line brace style. For example, the following Java source in (a) uses the next-line brace style. Your program converts it to the end-of-line brace style in (b).
(a) Next-line brace style public class Test { public static void main(String[] args) { // Some statements } }
(b) End-of-line brace style public class Test { public static void main(String[] args) { // Some statements } }
Java class files must be named after the class. (For example, for a class called Test, the file must be called Test.java.) Thus the original code will be in a file called ClassName.java (fill in ClassName with whatever test class you use). Preserve the original code style by writing it to a file called ClassName_newlinebraces.java. Write the end-of-line brace style to ClassName.java.
A.
// Java program to calculate the sum of all valid ints and
doubles from a file and output the result to 2 output files
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
public class FileSum {
public static void main(String[] args) throws FileNotFoundException {
double dblTotal = 0;
int intTotal = 0;
File file = new File("in.txt"); //
provide full path to file
// check if file exists
if(file.exists())
{
Scanner fileScan
= new Scanner(file); // open the input file
// read till the
end of file
while(fileScan.hasNext()) {
if(fileScan.hasNextInt()) // if next element
read is an integer
intTotal +=
fileScan.nextInt(); // read and add the integer to intTotal
else if(fileScan.hasNextDouble()) // if next
element read is a double
dblTotal +=
fileScan.nextDouble(); // read and add the double to dblTotal
else // if neither integer nor double, read and
discard the next element as string
fileScan.next();
}
fileScan.close(); // close the input file
// create and
open 2 output files
PrintWriter
intWriter = new PrintWriter("int_total.txt");
PrintWriter
dblWriter = new PrintWriter("double_total.txt");
// output the
sum of all valid integers
intWriter.write("Sum of all valid integers in file in.txt :
"+intTotal);
// output the
sum of all valid doubles
dblWriter.write("Sum of all valid doubles in file in.txt:
"+dblTotal);
// close the
output files
intWriter.close();
dblWriter.close();
}else
System.out.println("Unable to open file in.txt");
}
}
//end of program
Output:
Input file:

Output File:


java question: Write a program that reads in data from a text file named in.txt. Compute...
Filename(s): ReformatCode. java Public class: ReformatCode Package-visible class(es): none Write a program that reformats Java source code from the next-line brace style to the end-of-line brace style. The program is invoked from the command line with the input Java source code file as args [0] and the name of the file to save the formatted code in as args [1]. The original file is left untouched. The program makes no other changes the source code, including whitespace. For example, the...
I need to write a program in java that reads a text file with a list of numbers and sorts them from least to greatest. This is the starter file. import java.util.*; import java.io.*; public class Lab3 { static final int INITIAL_CAPACITY = 5; public static void main( String args[] ) throws Exception { // ALWAYS TEST FOR REQUIRED INPUT FILE NAME ON THE COMMAND LINE if (args.length < 1 ) { System.out.println("\nusage: C:\\> java Lab3 L3input.txt\n"); System.exit(0); } //...
Write a program that will check a Java file for syntax errors. The program will ask for an input-file name and output-file name and will then copy all the code from the Java input file to the Java output file, but with the following changes: 1. Any syntax error found in the Java input file will be corrected: a. Missing semicolon b. Missing compound statements (curly braces) c. All comments have to start with // and end with a period....
Question 1[JAVA] Add a method in the Main class named getLines() that takes a filename as a String and returns each line in the file in an array. Have each element of the array be a String. Do not include the newline at the end of each line. Use a BufferedReader object to read the file contents. Note, the contents of input.txt will be different (including the number of lines) for each test case. Example: getLines( "input.txt" ) returns (an...
Java. (20 pts) Write a program that reads all the numbers from the file mynums.dat and prints out the sum of the positive values from the file. Assume that the file contains only numeric values. You must output an error if the file can't be opened. You must write the complete program and the output when the program runs successfully must conform exactly to the sample output. Bonus ( 5 pts). Output an error if the file contains non-numeric...
4.3Learning Objective: To read and write text files. Instructions: This is complete program with one Java source code file named H01_43.java (your main class is named H01_43). Problem: Write a program that prompts the user for the name of a Java source code file (you may assume the file contains Java source code and has a .java filename extension; we will not test your program on non-Java source code files). The program shall read the source code file and output...
JAVA Code: Complete the program that reads from a text file and counts the occurrence of each letter of the English alphabet. The given code already opens a specified text file and reads in the text one line at a time to a temporary String. Your task is to go through that String and count the occurrence of the letters and then print out the final tally of each letter (i.e., how many 'a's?, how many 'b's?, etc.) You can...
Homework Assignment on Mimir: Read in a file "numbers.txt" into a Java program. Sum up all the even numbers over 50. Print the result to the console using System.out.println(); The file will only have numbers. Code: import java.io.FileNotFoundException; import java.io.FileReader; import java.util.Scanner; /** * * @author David */ public class ReadingFiles { /** * @param args the command line arguments */ public static void main(String[] args) throws FileNotFoundException { // TODO code application logic here...
java 1. Write a method header on line three with the following specs: Returns: a boolean Name: beTrue Parameters: none public class Main { { return true; } } 2. Write a method header on line two with the following specs: Returns: a String Name: makeCapital Parameters: a String named "name" You should not be writing code on any line other than #2 public class Main { { String ans = name.toUpperCase();...
Write a java program that declares 10 element array (of type integers), creates and initializes the array, and perform the sum of elements of the array using for loop. public class SumArray { public static void main (String[], args) { } // end of main } // end of SumArray class