Question

In: Computer Science

Java Programming: Scan a file and set up 3 different arrays from scanning through the .txt...

Java Programming:

Scan a file and set up 3 different arrays from scanning through the .txt file. you should have 2 string arrays and 1 integer array.

I need help knowing how to read the file so that it puts the correct information into an array. It is all separated by spaces.

.txt file:

S SABQ138 3
A AABQ205 2
S SABQ127 1
A AABQ313 2
S SABQ126 2
A AABQ302 2

Solutions

Expert Solution

Dear Student ,

As per the requirement submitted above , kindly find the below solution.

Here a new java program with name "Main.java" is created, which contains following code.

Main.java :

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
//Java class
public class FileReader {
//main method
   public static void main(String[] args) throws IOException {
       // creating array of Strings
       String[] strArray1 = new String[6];
       String[] strArray2 = new String[6];
       // creating array of integer
       int[] intArray = new int[6];
       FileInputStream fstream = new FileInputStream("sample.txt");
       BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
       String strLine;
       int i = 0;// declaring variable i
       // declaring a new array to split file
       while ((strLine = br.readLine()) != null) {
           String[] newArray = strLine.split(" ");// Splitting each line based on space
           strArray1[i] = newArray[0];// storing 0th element in strArray1
           strArray2[i] = newArray[1];// storing 1st element in strArray2
           intArray[i] = Integer.parseInt(newArray[2]);// storing 2nd element in intArray
           i++;// increment value of i
       }
       // print array elements
       System.out.println("strArray1 elements : ");
       for (i = 0; i < strArray1.length; i++) {
           System.out.print(strArray1[i] + " ");// print each array element
       }
       System.out.println("\nstrArray2 elements : ");
       for (i = 0; i < strArray2.length; i++) {
           System.out.print(strArray2[i] + " ");// print each array element
       }
       // print intArray elements
       System.out.println("\nintArray elements : ");
       for (i = 0; i < intArray.length; i++) {
           System.out.print(intArray[i] + " ");// print each array element
       }
   }

}

======================================================

Output : Compile and Run above program to get the screen as shown below

Screen 1 :Main.java

NOTE : PLEASE FEEL FREE TO PROVIDE FEEDBACK ABOUT THE SOLUTION.


Related Solutions

JAVA - Quick Sort .txt and Return ----------------------------------------------------------------------------- The program should input a .txt file like...
JAVA - Quick Sort .txt and Return ----------------------------------------------------------------------------- The program should input a .txt file like below and must use a Quick sort Algorithm ================ 6 10 4 19 10 12 8 6 0 1 2 3 ================ The first number "6" represents the index of the element to return after sort the second number on the top "10" represents the number of elements or size of array. The following numbers and lines are the elements that need to go...
#Java I am reading from the txt file and I put everytihng inside of the String[],...
#Java I am reading from the txt file and I put everytihng inside of the String[], like that: String[] values = str.split(", "); But, now I have to retrieve the data from it one by one. How can I do it? Here is the txt file: OneTime, finish the project, 2020-10-15 Monthly, wash the car, 2020-11-10 Thanks!
Your task is to modify the program from the Java Arrays programming assignment to use text...
Your task is to modify the program from the Java Arrays programming assignment to use text files for input and output. I suggest you save acopy of the original before modifying the software. Your modified program should: contain a for loop to read the five test score into the array from a text data file. You will need to create and save a data file for the program to use. It should have one test score on each line of...
Java - Text File to Arrays and output ------------------------------------------------------------------------------ I need to have a java program...
Java - Text File to Arrays and output ------------------------------------------------------------------------------ I need to have a java program read an "input.txt" file (like below) and store the information in an respective arrays. This input.txt file will look like below and will be stored in the same directory as the java file. The top line of the text represents the number of people in the group for example. the lines below it each represent the respective persons preferences in regards to the other...
Java program problem 1 Come up with an idea for parallel arrays Create the arrays to...
Java program problem 1 Come up with an idea for parallel arrays Create the arrays to hold 5 items in each In the main(), load the arrays with data Then output the entire contents of the arrays Create a method called find1() and pass the 2 arrays to that method. problem 2 In the find1() method, ask the user to enter a value to search for Write logic to search the first array and then output the related data from...
(Java Programming Problem) Build two ArrayList lists from the Integer and String type arrays, populate the...
(Java Programming Problem) Build two ArrayList lists from the Integer and String type arrays, populate the lists with repeats (see example below). Write a generic method (removeDuplicates( ….. )) to remove those duplicate items and returns an ArrayList<E> list without duplicates. Original Integer List: [14, 24, 14, 42, 24, 25, 25, 23] No-Duplicate List: [14, 24, 42, 25, 23] Same generic method for the name list Original List: [Mike, Lara, Jenny, Lara, Jared, Jonny, Lindsey, Mike, Jared] No-Duplicate List: [Mike,...
Write a Java program to read a set of integers from a file, dataX, and a...
Write a Java program to read a set of integers from a file, dataX, and a set of ranges from a second file, rangeX, and, for each range [a, b] in rangeX, report the SUM of all the integers in dataX which are in the range [a, b]. As the integers are read from file dataX, insert them in a binary search tree. After all the integers have been inserted into the binary search tree, read the ranges from file...
How are PHP arrays different than arrays in other programming languages? Please discuss in depth.
How are PHP arrays different than arrays in other programming languages? Please discuss in depth.
Computer Science - Java Programming How do you read a text file and store three different...
Computer Science - Java Programming How do you read a text file and store three different pieces of information in the database when the given text file contains this info.: 12345 Computer Science Bob Stone 23456 Art James G. Ocean? These are written in the format as ID Class Name. I was going to take the three different pieces of information by separating them by spaces, but the number of spaces is random and I don't know how to adjust...
Java Programming Preferably Concepts: Generics Arrays Objects Part I: Write a routine in Java that takes...
Java Programming Preferably Concepts: Generics Arrays Objects Part I: Write a routine in Java that takes an array, the length of the array, and an element and returns the position of the element in the array. For example, given the array with values [2, 4, 6, 8] and element 6, the routine should return 2 (since counting from 0, 6 occurs at position 2 in the array). Your routine should use generics to enable your routine to be reusable for...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT