Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

How to query data from text file in Java? see code: /* * To change this license header, choose License Headers in Project Properties. *

How to query data from text file in Java? see code: /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package lab_4; import java.util.Scanner; import java.io.*; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; /** * * @author tingram */ public class LAB_4 { /** * @param args the command line arguments */ public static void main(String[] args) throws IOException { // TODO code application logic here Scanner sc = new Scanner(System.in); // assign a file name and declare the file objects String myFile = "C:\\Users\\tingram\\Desktop\\data.txt"; FileWriter file = new FileWriter(myFile); PrintWriter outfile = new PrintWriter(file); // variable for the number of records in the data table int numRecords = 0; // variables for the fields of the data table String strName = ""; double dblCost = 0; int intQuantity = 0; char chrLocation = '\0'; // input the number of records to be processed System.out.println("How many records are for processing?"); numRecords = sc.nextInt(); for (int i = 1; i <= numRecords; i++) { // obtain user input System.out.println("item description"); strName = sc.next(); System.out.println("item cost"); dblCost = sc.nextDouble(); System.out.println("item quantity"); intQuantity = sc.nextInt(); System.out.println("item location"); chrLocation = sc.next().charAt(0); // write the data to the file outfile.print(strName + ", $"); outfile.print(dblCost + ", "); outfile.print(intQuantity + ", "); outfile.println(chrLocation); } outfile.close(); // assign a file name and declare the file objects String dataFile = "C:\\Users\\tingram\\Desktop\\data.txt"; String line = ""; String csv = ","; FileReader fileIn = new FileReader(dataFile); BufferedReader br = new BufferedReader(fileIn); try { while ((line = br.readLine()) != null) { // use comma as a value separator String[] data = line.split(csv); System.out.println(data[0] + "," + data[1] + "," + data[2] + "," + data[3]); } } catch (IOException e) { e.printStackTrace(); } Scanner sn = new Scanner(dataFile); /*SET UP QUERIES*/ //QUERY 1 COUNT QUANTITIES while (sn.hasNext()) { String record = sn.nextLine(); String[] data = {"strName", "dblCost","intQuantity", "chrLocation" }; if(data[3].equalsIgnoreCase("C")) { System.out.println(record); } } br.close(); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

How To Make A Database In Historical Studies

Authors: Tiago Luis Gil

1st Edition

3030782409, 978-3030782405

More Books

Students also viewed these Databases questions