Question

In: Computer Science

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 it accordingly.

How should I do this? Is it even possible?

Solutions

Expert Solution

Answer:


import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Scanner;

public class FileReader {

   public static void main(String[] args) {
       try {
           // creating file object here
           File fileObj = new File("test-data.txt");
          
           // creating scanner object to read file line by line
           Scanner scanner = new Scanner(fileObj);

           // priting header line of table
           System.out.format("%6s| %10s| %16s", "Id", "Class", "Name");
           System.out.println("\n--------------------------------------");

           // iteratng while to read file data
           while (scanner.hasNextLine()) {
              
               // reading line
               String line = scanner.nextLine();
              
               // spliting line by providing regex and limit to split function
               // "\\s" is regex for whitespace and 3 is limit, so this split the string upto 3 whitespaces
               String data[] = line.split("\\s", 3);
              
               // printng a data into table format using format spacifier
               System.out.format("%6s| %10s| %16s", data[0], data[1], data[2]);
               System.out.println("");
           }
          
           // closing scanner object
           scanner.close();
          
       } catch (FileNotFoundException e) {
           System.out.println("An error occurred.");
           e.printStackTrace();
       }

   }

}

Output:


Related Solutions

How to read a text file and store the elements into a linked list in java?...
How to read a text file and store the elements into a linked list in java? Example of a text file: CS100, Intro to CS, John Smith, 37, 100.00 CS200, Java Programming, Susan Smith, 35, 200.00 CS300, Data Structures, Ahmed Suad, 41, 150.50 CS400, Analysis of Algorithms, Yapsiong Chen, 70, 220.50 and print them out in this format: Course: CS100 Title: Intro to CS Author: Name = John Smith, Age = 37 Price: 100.0. And also to print out the...
java question: How would you be able to store a matrix from a text file into...
java question: How would you be able to store a matrix from a text file into a linked or doubly linked list, if you cannot use 2D arrays? input example: 1 2 3 4 1 3 2 4 4 2 3 1
Using c programming language How do you put data from a text file into a 2d...
Using c programming language How do you put data from a text file into a 2d array For example a text file with names and age: john 65 sam 34 joe 35 sarah 19 jason 18 max 14 kevin 50 pam 17 bailey 38 one 2d array should have all the names from the file and one 2d array should have all the ages and both arrays should be printed out separately and be 3x3
How do I do this: Write a program that can read a text file of numbers...
How do I do this: Write a program that can read a text file of numbers and calculate the mean and standard deviation of those numbers. Print the result in another text file. Put the result on the computer screen. EACH LINE OF THE PROGRAM MUST BE COMMENTED!
Please write a java program to write to a text file and to read from a...
Please write a java program to write to a text file and to read from a text file.
I need to write a java program (in eclipse) that will read my text file and...
I need to write a java program (in eclipse) that will read my text file and replace specific placeholders with information provided in a second text file. For this assignment I am given a text file and I must replace <N>, <A>, <G>, with the information in the second file. For example the information can be John 22 male, and the template will then be modified and saved into a new file or files (because there will be multiple entries...
In this course, you have learned that computer programming is key to computer science. The ability...
In this course, you have learned that computer programming is key to computer science. The ability to program a succession of instructions to resolve a computing problem is a valuable skill that is integral to many aspects of modern life. You have also seen the versatility and distinguishing characteristics that make Python 3 a popular choice for programmers. End-of-Course Essay Write a 1- to 2-page essay in which you connect this course to your own career or personal life. Imagine...
in java File encryption is the science of writing the contents of a file in a...
in java File encryption is the science of writing the contents of a file in a secret code. Write an encryption program that works like a filter, reading the contents of one file, modifying the data into a code, and then writing the coded contents out to a second file. The second file will be a version of the first file, but written in a secret code. Although there are complex encryption techniques, you should come up with a simple...
Assignment in C programming class: read the text file line by line, and place each word,...
Assignment in C programming class: read the text file line by line, and place each word, in order, in an array of char strings. As you copy each word into the array, you will keep a running count of the number of words in the text file and convert the letters of each word to lower case. Assume tgat you will use no more than 1000 words in the text, and no more than the first 10 characters in a...
In Java. Modify the attached GameDriver2 to read characters in from a text file rather than...
In Java. Modify the attached GameDriver2 to read characters in from a text file rather than the user. You should use appropriate exception handling when reading from the file. The text file that you will read is provided. -------------------- Modify GaveDriver2.java -------------------- -----GameDriver2.java ----- import java.io.File; import java.util.ArrayList; import java.util.Scanner; /** * Class: GameDriver * * This class provides the main method for Homework 2 which reads in a * series of Wizards, Soldier and Civilian information from the user...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT