I am told to create a fillArray method that will read integers from the file "data.txt" and store them in the array. I'm told to assume that the file has no more than 100 items in it.
This is what I have so far:
public void fillArray()
{
int curVal;
Scanner input = null;
try
{
input = new Scanner(new File("data.txt"));
// set the current number of items in the array to zero
while (input.hasNextInt())
{
curVal = input.nextInt();
// add code to store curVal into the array and update other information as needed
}
input.close();
}
catch (FileNotFoundException e)
{
System.out.println("Could not find data.txt file");
System.exit(1);
}
}
I am unsure what I am missing, if anyone can help, I would be very grateful. Thank you.
thanks for the question,
You need to create an array of size 100. and then keep adding the numbers you read from the file in the array at the location given by the variable index. What you have so far, is to read all the numbers in the file but the code to store the numbers in the array is missing.
Since you have not shared the complete problem statement, I am assuming there is an int[] array by the name numbers and we want to populate the data from the file into this int[] array.
I have updated your class to do that. Rest everything is all good and sweet : )
If you have any problem, please do comment, no need to post another question for this. Will help you
thanks .
================================================================================
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class SomeClassName {
// assuming you have an array numbers[] of size 100 declared
private int[] numbers = new int[100];
public void fillArray() {
int curVal;
Scanner input = null;
try {
input = new Scanner(new File("data.txt"));
// set the current number of items in the array to zero
int index = 0; // the position in the array where we are going to store the current number read
while (input.hasNextInt()) {
curVal = input.nextInt();
// add code to store curVal into the array and update other information as needed
numbers[index] = curVal;
index += 1; // increment the index by 1 so that we can add the next number in the next index
}
input.close();
} catch (FileNotFoundException e) {
System.out.println("Could not find data.txt file");
System.exit(1);
}
}
public static void main(String[] args) {
}
}
======================================================================
I am told to create a fillArray method that will read integers from the file "data.txt"...
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) { ...
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...
I am trying to read from a file with with text as follows and I am not sure why my code is not working. DHH-2180 110.25 TOB-6851 -258.45 JNO-1438 375.95 CS 145 Computer Science II IPO-5410 834.15 PWV-5792 793.00 Here is a description of what the class must do: AccountFileIO 1. Create a class called AccountFileIO in the uwstout.cs145.labs.lab02 package. (This is a capital i then a capital o for input/output.) a. This class will read data from a file...
I am getting an error from eclipse stating that: Exception in thread "main" java.lang.Error: Unresolved compilation problem: at EvenOdd.main(EvenOdd.java:10) I am unable to find what I can do to fix this issue. Can you please assist? My code is below: import java.util.InputMismatchException; import java.util.Scanner; public class EvenOdd { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("Welcome to this Odd program!"); int userInput1 = input.nextInt(); } public...
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...
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...
Need Help ASAP!! Below is my code and i am getting error in
(public interface stack) and in StackImplementation class. Please
help me fix it. Please provide a solution so i can fix the
error.
thank you....
package mazeGame;
import java.io.*;
import java.util.*;
public class mazeGame {
static String[][]maze;
public static void main(String[] args)
{
maze=new String[30][30];
maze=fillArray("mazefile.txt");
}
public static String[][]fillArray(String file)
{
maze = new String[30][30];
try{...
I am working on the divide/conquer algorithm. I am having a trouble with a print for output from reading the file. Here is my work. When you see I put the comment with TODO. that one I am struck with readfile. I wonder if you'd able to help me to fix the readfile to find a single number. Here is the input3.txt (1 12 13 24 35 46 57 58 69). after that, the output should be 0. int mergeInversion(int...
I am given an input file, P1input.txt and I have to write code to find the min and max, as well as prime and perfect numbers from the input file. P1input.txt contains a hundred integers. Why doesn't my code compile properly to show me all the numbers? It just stops and displays usage: C:\> java Project1 P1input.txt 1 30 import java.io.*; // BufferedReader import java.util.*; // Scanner to read from a text file public class Project1 { public static...
I have a program that reads a file and then creates objects from the contents of the file. How can I create a linked list of objects and use it with the package class instead of creating and using an array of objects? I am not allowed to use any arrays of objects or any java.util. lists in this program. Runner class: import java.util.Scanner; import java.io.*; class Runner { public static Package[] readFile() { try { File f = new...