Information About This Project
In the realm of database processing, a flat file is a text file that holds a table of records.
Here is the data file that is used in this project. The data is converted to comma separated values ( CSV ) to allow easy reading into an array.
Table: Consultants
ID |
LName |
Fee |
Specialty |
101 |
Roberts |
3500 |
Media |
102 |
Peters |
2700 |
Accounting |
103 |
Paul |
1600 |
Media |
104 |
Michael |
2300 |
Web Design |
105 |
Manfred |
3500 |
Broadcasting |
Steps to Complete This Project
STEP 1 Open a Java Editor
Open NetBeans or Eclipse and create a Java project with the following details.
For Project Name include: DataApplication
For the Main Class include: DataApplication
In your Code window for this class, shown below, copy the program code shown in Figure 1 below, in the appropriate places, except substitute your own name in place of Sammy Student.
PROJECT Java File Processing Application: Database Application
Figure 1 Source Code for the Database File Processing Program
import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Scanner; // Programmer: Sammy Student public class DataApplication { public static void main(String[] args) { try { File fin = new File("data.txt"); Scanner scan = new Scanner(fin); ArrayList<String> theData = new ArrayList<String>();
// read the column headings from the flat text file String line = scan.nextLine(); while(scan.hasNextLine()) { line = scan.nextLine(); String[] list = line.split(","); int key = Integer.parseInt(list[0]); String name = list[1]; int fee = Integer.parseInt(list[2]); String specialty = list[3];
theData.add(String.valueOf(key)); theData.add(name); theData.add(String.valueOf(fee)); theData.add(specialty); } int count = 1; for (int i = 0; i < theData.size(); i++) { System.out.print(theData.get(i) + "\t\t"); if(count % 4 == 0 ) System.out.println(" "); count++; } scan.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } } } |
Create and Save a Text Data File
Open a text file within your project folder. Name the text file as: data.txt
Copy the CSV formatted data below into the text file that you just created.
Save the data in the text file.
[ Data File ]
ID,LName,Fee,Specialty
101,Roberts,3500,Media
102,Peters,2700,Accounting
103,Paul,1600,Media
104,Michael,2300,Web Design
105,Manfred,3500,Broadcasting
STEP 3 Build, Compile and Run the Program
From the menu select [ Run ] and click [ Run Project ] to run your app.
Observe the program's output. Notice that the data from the text file is read into an ArrayList and the array list elements are displayed in a console output statement. The data values are displayed in a tabular format.
STEP 4 Modify Your Program
You will now modify your database application by including a method named searchData() that receives the ArrayList elements and allows the program user to search for a name located within the flat file.
If the name of the consultant is located, a message as such is displayed by the search method otherwise a message indicating that the name is not found in the file.
The method for searching names in the flat file is shown below. Place the method in an appropriate location in your program code.
Remember to also write a statement that calls the method.
searchData(theData);
Save your program to update it and perform a trial run of your
modified code. Test your program by entering the
name of a consultant that appears in the text file and run the
program again using a name that is not in the file.
Take screen snapshots showing both a data found and data not found
example.
PROJECT Java File Processing Application: Database Application
Figure 2 Additional Source Code for the Database File Processing Program
public static void searchData(ArrayList<String> vals) { System.out.print("enter a name: "); Scanner sc = new Scanner(System.in); String strName = sc.nextLine().trim(); boolean found = false; for (int i = 0; i < vals.size(); i++) { if(vals.get(i).equals(strName.trim())) { found = true; break; } }
if(found == true) System.out.println(" data found "); else System.out.println(" data not found ");
sc.close(); }
|
STEP 5 Test the Program and Write the Data
Now modify your program again by including a new method that will determine if any consultant charges a fee that exceeds $ 2,000 .
Save and test your program.
Finally include another method that will allow the program user to query the flat file and show a count of the consultants that specialize in providing media services.
Save and test your program.
Hi, I have added all required methods.
Please let me know in case of any issue.
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
// Programmer: Sammy Student
public class DataApplication
{
public static void searchData(ArrayList<String> vals)
{
System.out.print("enter a name: ");
Scanner sc = new Scanner(System.in);
String strName = sc.nextLine().trim();
boolean found = false;
for (int i = 0; i < vals.size(); i++)
{
if(vals.get(i).equals(strName.trim()))
{
found = true;
break;
}
}
if(found == true)
System.out.println(" data found ");
else
System.out.println(" data not found ");
sc.close();
}
public static void main(String[] args)
{
try
{
File fin = new File("data.txt");
Scanner scan = new Scanner(fin);
ArrayList<String> theData = new ArrayList<String>();
// read the column headings from the flat text file
String line = scan.nextLine();
while(scan.hasNextLine())
{
line = scan.nextLine();
String[] list = line.split(",");
int key = Integer.parseInt(list[0]);
String name = list[1];
int fee = Integer.parseInt(list[2]);
String specialty = list[3];
theData.add(String.valueOf(key));
theData.add(name);
theData.add(String.valueOf(fee));
theData.add(specialty);
}
int count = 1;
for (int i = 0; i < theData.size(); i++)
{
System.out.print(theData.get(i) + "\t\t");
if(count % 4 == 0 )
System.out.println(" ");
count++;
}
scan.close();
System.out.println(theData);
searchData(theData);
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
}
}
/*
Sample run:
101 Roberts 3500 Media
102 Peters 2700 Accounting
103 Paul 1600 Media
104 Michael 2300 Web Design
105 Manfred 3500 Broadcasting
*/
Information About This Project In the realm of database processing, a flat file is a...
Trying to practice this assignment Argument list: the *yahoonews.txt Data file: yahoonews.txt Write a program named WordCount.java, in this program, implement two static methods as specified below: public static int countWord(Sting word, String str) this method counts the number of occurrence of the word in the String (str) public static int countWord(String word, File file) This method counts the number of occurrence of the word in the file. Ignore case in the word. Possible punctuation and symbals in the file...
Hello can somebody please help me with Project 15-3 File Cleaner assignment? This project is to be done while using Java programming. Here are what the assignment says… Create an application that reads a file that contains an email list, reformats the data, and writes the cleaned list to another file. Below are the grading criteria… Fix formatting. The application should fix the formatting problems. Write the File. Your application should write a file named prospects_clean.csv. Use title case. All...
1. Import file ReadingData.zip into NetBeans. Also, please download the input.txt file and store it into an appropriate folder in your drive. a) Go through the codes then run the file and write the output with screenshot (please make sure that you change the location of the file) (20 points) b) Write additional Java code that will show the average of all numbers in input.txt file (10 points) import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; import java.io.*; 1- * @author mdkabir...
JAVA project PLEASE complete/ create project with comments in the programming explaining everything Text file Year of storm/ Name of storm/ mmdd storm started/ mmdd storm ended/ magnitude of storm/ //made up data 2004/Ali/1212/1219/1 2003/Bob/1123/1222/3 1980/Sarah/0123/0312/0 1956/Michael/1211/1223/4 1988/Ryan/0926/1019/ 1976/Tim/0318/1010/0 2006/Ronald/0919/1012/2 1996/Mona/0707/0723/1 2000/Kim/0101/0201/1 2001/Jim/1101/1201/3 Text file Class storm{ private String nameStorm; private int yearStorm; private int startStorm; private int endStorm; private int magStorm; public storm(String name, int year, int start, int end, int mag){ nameStorm = name; yearStorm = year; startStorm...
JAVA Code: Complete the program that reads from a text file and counts the occurrence of each letter of the English alphabet. The given code already opens a specified text file and reads in the text one line at a time to a temporary String. Your task is to go through that String and count the occurrence of the letters and then print out the final tally of each letter (i.e., how many 'a's?, how many 'b's?, etc.) You can...
composed the following java code to read a string from a text file but receiving compiling errors. The text file is MyNumData.txt. Included the original java script that generated the output file. Shown also in the required output results after running the java program. I can't seem to search for the string and output the results. Any assistance will be greatly appreciated. import java.io.BufferedReader; import java.io.FileReader; import java.util.ArrayList; public class Main { public static void main(String[] args) { System.out.print("Enter the...
The following code uses a Scanner object to read a text file called dogYears.txt. Notice that each line of this file contains a dog's name followed by an age. The program then outputs this data to the console. The output looks like this: Tippy 2 Rex 7 Desdemona 5 1. Your task is to use the Scanner methods that will initialize the variables name1, name2, name3, age1, age2, age3 so that the execution of the three println statements below will...
I am creating a program that will allow users to sign in with a username and password. Their information is saved in a text file. The information in the text file is saved as such: Username Password I have created a method that will take the text file and convert into an array list. Once the username and password is found, it will be removed from the arraylist and will give the user an opportunity to sign in with a...
Use the csv file on spotify from any date Code from lab2 import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.Arrays; import java.util.Scanner; public class SongsReport { public static void main(String[] args) { //loading name of file File file = new File("songs.csv"); //reading data from this file //scanner to read java file Scanner reader; //line to get current line from the file String line=""; ...
13.5 LAB: Zip code and population (generic types)Define a class StatePair with two generic types (Type1 and Type2), a constructor, mutators, accessors, and a printInfo() method. Three ArrayLists have been pre-filled with StatePair data in main():ArrayList<StatePair> zipCodeState: Contains ZIP code/state abbreviation pairsArrayList<StatePair> abbrevState: Contains state abbreviation/state name pairsArrayList<StatePair> statePopulation: Contains state name/population pairsComplete main() to use an input ZIP code to retrieve the correct state abbreviation from the ArrayList zipCodeState. Then use the state abbreviation to retrieve the state name from the ArrayList abbrevState. Lastly,...