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

C Programming How do you read in a text file line by line into a an...
C Programming How do you read in a text file line by line into a an array. example, i have the text file containing: line 1: "qwertyuiop" line 2: "asdfghjkl" line 3: "zxcvbnm" Then in the resulting array i get this: array:"qwertyuiopasdfghjklzxcvbnm"
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
1) How do you read the contents of a text file that contains whitespace characters as...
1) How do you read the contents of a text file that contains whitespace characters as part of its data? 2) What capability does the fstream data type provide that the ifstream and ofstream data types do not? 3) What header file do you need to include in a program that performs file operations? 4) What data type do you use when you want to create a file stream object that can write data to a file? 5) What data...
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!
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
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.
"""    CS 125 - Intro to Computer Science    File Name: CS125_Lab1.py    Python Programming...
"""    CS 125 - Intro to Computer Science    File Name: CS125_Lab1.py    Python Programming    Lab 1    Name 1: FirstName1 LastName1    Name 2: FirstName2 LastName2    Description: This file contains the Python source code for deal or no deal. """ class CS125_Lab1(): def __init__(self): # Welcome user to game print("Let's play DEAL OR NO DEAL")    # Define instance variables and init board self.cashPrizes = [.01, .50, 1, 5, 10, 50, 100, 250, 500, 1000, 5000,...
Write a java program that will read a file called stateinfo.txt and will store the information...
Write a java program that will read a file called stateinfo.txt and will store the information of the file into a map. The stateinfo.txt file contains the names of some states and their capitals. The format of stateinfo.txt file is as follows. State                Capital ---------                         ----------- NSW               Sydney VIC                 Melbourne WA                 Perth TAS                 Tasmania QLD                Brisbane SA                   Adelaide The program then prompts the user to enter the name of a state. Upon receiving the user input, the program should...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT