java
read integers from this binary file and when the value is 0 then stop reads and print its summation
here is my code
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Random;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.FileInputStream;
class Main {
public static void main(String[] args) {
String fname = "out.txt";
prepare(fname);
ObjectInputStream inputStream =
null;
// create code here
}
public static void prepare(String fname) {
ObjectOutputStream outputStream =
null;
Random generator = new
Random(30);
try {
outputStream =
new ObjectOutputStream(new FileOutputStream(fname));
for (int i = 0;
i < generator.nextInt(20) + 4; i++)
outputStream.writeInt(generator.nextInt(50000) -
25000);
outputStream.writeInt(0);
outputStream.close();
}
catch (FileNotFoundException e)
{
System.out.println("Error file " + fname);
System.exit(0);
}
catch (IOException e) {
System.out.println("Error " + fname);
System.exit(0);
}
}
}

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.Random;
public class ReadIntFromBinary {
public static void main(String[] args) throws FileNotFoundException {
String fname = "out.txt";
prepare(fname);
long sum = 0;
DataInputStream inputStream = new DataInputStream(new FileInputStream(fname));
try {
while (true) {
int n = inputStream.readInt();
if(n == 0) {
break;
}
sum += n;
}
} catch (Exception e) {
// Read EOF
} finally {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("The sum of the numbers is: " + sum);
}
public static void prepare(String fname) {
ObjectOutputStream outputStream = null;
Random generator = new Random(30);
try {
outputStream = new ObjectOutputStream(new FileOutputStream(fname));
for (int i = 0; i < generator.nextInt(20) + 4; i++)
outputStream.writeInt(generator.nextInt(50000) - 25000);
outputStream.writeInt(0);
outputStream.close();
} catch (FileNotFoundException e) {
System.out.println("Error file " + fname);
System.exit(0);
} catch (IOException e) {
System.out.println("Error " + fname);
System.exit(0);
}
}
}
If the file does not contain any 0, then We read everything and show the sum.
java read integers from this binary file and when the value is 0 then stop reads...
Analyze the code to determine what the code does, how the data is structured, and why it is the appropriate data structure (i.e. linked list, stack or queue). Note that these are examples of classes and methods and do not include the "main" or "test" code needed to execute. When you are done with your analysis, add a header describing the purpose of the program (include a description of each class and method), the results of your analysis, and add...
I need help with my IM (instant messaging) java program, I created the Server, Client, and Message class. I somehow can't get both the server and client to message to each other. I am at a roadblock. Here is the question below. Create an IM (instant messaging) java program so that client and server communicate via serialized objects (e.g. of type Message). Each Message object encapsulates the name of the sender and the response typed by the sender. You may...
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...
composed the following java code to read a string from a text file but receiving compiling errors. The text file is MyNumData.txt. Included the original java script that generated the output file. Shown also in the required output results after running the java program. I can't seem to search for the string and output the results. Any assistance will be greatly appreciated. import java.io.BufferedReader; import java.io.FileReader; import java.util.ArrayList; public class Main { public static void main(String[] args) { System.out.print("Enter the...
Please help me fix my errors. I would like to read and write the text file in java. my function part do not have errors. below is my code import java.io.FileInputStream; import java.io.InputStreamReader; import java.io.FileWriter; import java.io.IOException; public class LinkedList { Node head; class Node { int data; Node next; Node(int d) { data = d; next = null; } } void printMiddle() { Node slow_ptr...
How to write a Java file out that that reads from numbers.txt with these numbers 2 6 7 9 5 4 3 8 0 1 6 8 2 3 and write to a file called totalSum.txt that looks like: 2+6+7+9=24 and so on using this code import java.io.*; import java.util.Scanner; public class FileScanner { public static void main(String[] args) { /* For Homework! Tokens*/ Scanner file = null; PrintWriter fout= null; ...
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...
QUESTION The ReadFile class opens and reads a text file. The WriteFile class opens and writes to a file. Compile and run these programs. There are several ways to read/write text files. The examples shown here are just one way of achieving this. Look at the API for the BufferedReader class. The readline() method is a simple way to read the text file line by line. It returns null when the end of the file has been reached. https://docs.oracle.com/javase/8/docs/api/java/io/BufferedReader.html Look...
Trying to practice this assignment Argument list: the *yahoonews.txt Data file: yahoonews.txt Write a program named WordCount.java, in this program, implement two static methods as specified below: public static int countWord(Sting word, String str) this method counts the number of occurrence of the word in the String (str) public static int countWord(String word, File file) This method counts the number of occurrence of the word in the file. Ignore case in the word. Possible punctuation and symbals in the file...
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); } //...