Here is my quest, I have the same value in records in a text file.
But I only get to find the first one and to display the first one.
How can I find all the record with that value and display them.
Here is the code,
package projectsby;
import java.io.File;
import java.util.Scanner;
import javax.swing.JOptionPane;
public class Find {
private static Scanner o;
public void openFile(){
try{
o = new Scanner(new File("/Users/e.h.j.hofman/NetBeansProjects/Projectsby/src/projectsby/Projects.txt"));
}
catch(Exception e) {
System.out.println("Could not find file");
}}
/**
*
* #param searchTerm
* #param filepath
*/
public void readEntries(String searchTerm, String filepath){
boolean found = false;
String Project = "";
String omschrijving = "";
String BU= "";
String afdeling = "";
try
{
o = new Scanner(new File(filepath));
//o.useDelimiter("[ \n]");
while(o.hasNext()
//&& !found )
{
Project = o.next();
omschrijving = o.next();
BU = o.next();
afdeling = o.next();
if(Project.equalsIgnoreCase(searchTerm))
{
found = true; }
}
if (found)
{
Here i suspect it will help when there is some kind of while loop.
JOptionPane.showMessageDialog
(null,"Project: " + Project + " " +omschrijving + "\nBU: "
+ BU + "\nAfdeling: " +afdeling);
} else {
JOptionPane.showMessageDialog(null,"Project not found");
}}
catch(Exception e)
{
JOptionPane.showMessageDialog(null, "Error");
}}
public void closeFile(){
o.close();
}}
It is a little hard to tell from your code formatting but the block of code in the "if (found)" statement will only be executed at the end of the file since it is outside the while loop. Maybe that code should be moved to the "if(Project.equalsIgnoreCase(searchTerm))" statement instead. Then it will execute on every record in the file, if the searchTerm is found.
Then there is no reason for the "found" boolean.
while (o.hasNext()) {
...
if(Project.equalsIgnoreCase(searchTerm)) {
JOptionPane.showMessageDialog ...
}
}
I have a csv file which is hashmapped, whenever the user enter the city name(key) it will display all the details of that city. I have to optimize the search result time, everytime the it is reading the file(instead of only once) and displaying the values.
The CSV files contains data like this :
city,city_ascii,lat,lng,country,iso2,iso3,admin_name,capital,population,id
Malishevë,Malisheve,42.4822,20.7458,Kosovo,XK,XKS,Malishevë,admin,,1901597212
Prizren,Prizren,42.2139,20.7397,Kosovo,XK,XKS,Prizren,admin,,1901360309
Zubin Potok,Zubin Potok,42.9144,20.6897,Kosovo,XK,XKS,Zubin
Potok,admin,,1901608808
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;
import java.io.IOException;
public class CSVFileReaders{
public static void main(String[] args) {
String filePath = "C:\\worldcities1.csv";
Scanner in = new Scanner(System.in);
System.out.println(" \n Enter the City name to be Searched : \n _> ");
long start = System.currentTimeMillis();
String searchTerm = in.nextLine();
readAndFindRecordFromCSV(filePath, searchTerm);
long end = System.currentTimeMillis();
System.out.println(" \n It took " + (end - start) + " Milli Seconds to search the result \n");
in.close();
}
public static void readAndFindRecordFromCSV( String filePath, String searchTerm) {
try{
HashMap<String,ArrayList<String>> cityMap = new HashMap<String,ArrayList<String>>();
Scanner x = new Scanner (new File(filePath),"UTF-8");
String city= "";
while(x.hasNextLine()) {
ArrayList<String> values = new ArrayList<String>();
String name = x.nextLine();
//break each line of the csv file to its elements
String[] line = name.split(",");
city = line[1];
for(int i=0;i<line.length;i++){
values.add(line[i]);
}
cityMap.put(city,values);
}
x.close();
//Search the city
if(cityMap.containsKey(searchTerm)) {
System.out.println("City name is : "+searchTerm+"\nCity details are accordingly in the order :"
+ "\n[city , city_ascii , lat , lng , country , iso2 , iso3 , admin_name , capital , population , id] \n"
+cityMap.get(searchTerm)+"");
}
else {
System.out.println("Enter the correct City name");
}
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}`
the time should be optimized and every time i search it is reading the entire file(which should happen)
Currently you mix the map initialization inside the search function.
You don't want that.
First, init the map, then use it in the search function.
To do that, extract a method for statements that instantiate and value the map and then refactor the readAndFindRecordFromCSV() method so that it accepts a Map as additional parameter :
public static void readAndFindRecordFromCSV( String filePath, String searchTerm, HashMap<String,ArrayList<String>> dataByCity) {...}
With refactoring IDE features, it should be simple enough : "extracting method" then "change signature".
Here is a code (not tested at runtime but tested at compile time) that splits the logical in separated tasks and also rely on instance methods :
public class CSVFileReaders {
private final String csvFile;
private HashMap<String, ArrayList<String>> cityMap;
private final Scanner in = new Scanner(System.in);
public static void main(String[] args) {
String filePath = "C:\\worldcities1.csv";
CSVFileReaders csvFileReaders = new CSVFileReaders(filePath);
csvFileReaders.createCitiesMap();
csvFileReaders.processUserFindRequest(); // First search
csvFileReaders.processUserFindRequest(); // Second search
}
public CSVFileReaders(String csvFile) {
this.csvFile = csvFile;
}
public void createCitiesMap() {
cityMap = new HashMap<>();
try (Scanner x = new Scanner(new File(csvFile), "UTF-8")) {
String city = "";
while (x.hasNextLine()) {
ArrayList<String> values = new ArrayList<String>();
String name = x.nextLine();
//break each line of the csv file to its elements
String[] line = name.split(",");
city = line[1];
for (int i = 0; i < line.length; i++) {
values.add(line[i]);
}
cityMap.put(city, values);
}
x.close();
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
}
public void processUserFindRequest() {
System.out.println(" \n Enter the City name to be Searched : \n _> ");
long start = System.currentTimeMillis();
String searchTerm = in.nextLine();
long end = System.currentTimeMillis();
System.out.println(" \n It took " + (end - start) + " Milli Seconds to search the result \n");
//Search the city
if (cityMap.containsKey(searchTerm)) {
System.out.println("City name is : " + searchTerm + "\nCity details are accordingly in the order :"
+ "\n[city , city_ascii , lat , lng , country , iso2 , iso3 , admin_name , capital , population , id] \n"
+ cityMap.get(searchTerm) + "");
} else {
System.out.println("Enter the correct City name");
}
}
}
The interesting part is here :
String filePath = "C:\\worldcities1.csv";
CSVFileReaders csvFileReaders = new CSVFileReaders(filePath);
csvFileReaders.createCitiesMap();
csvFileReaders.processUserFindRequest(); // First search
csvFileReaders.processUserFindRequest(); // Second search
The logical is clearer now.
Why do you create / load the CSV into a HashMap with every search ?
Just create the HashMap only once in the beginning, and then on every search just check whether it exists in the HashMap, eg move the read part into a separate method :
HashMap<String,ArrayList<String>> cityMap = new HashMap<String,ArrayList<String>>();
public static void readCSVIntoHashMap( String filePath) {
try{
Scanner x = new Scanner (new File(filePath),"UTF-8");
String city= "";
while(x.hasNextLine()) {
ArrayList<String> values = new ArrayList<String>();
String name = x.nextLine();
//break each line of the csv file to its elements
String[] line = name.split(",");
city = line[1];
for(int i=0;i<line.length;i++){
values.add(line[i]);
}
cityMap.put(city,values);
}
x.close();
...
}
Then have a separate method for searching :
public static void search(String searchTerm) {
if(cityMap.containsKey(searchTerm)) {
...
}
}
I have this code
static String sCurrentLine = null;
/* keyword */
static String keyword = null;
Scanner keywordFile = null, siteFile = null;
try {
keywordFile = new Scanner(new File("/home/mearts/keywords.txt"));
siteFile = new Scanner(new FileReader(fileChooser.getSelectedFile()));
sCurrentLine = siteFile.nextLine().trim();
keyword = keywordFile.nextLine().trim();
while (sCurrentLine != null){
while (keywordFile.hasNext() || keyword == null) {
System.out.println("Line--> " + keyword);
System.out.println("Current here >>" + sCurrentLine);
if (sCurrentLine.contains(keyword)) {
System.out.println("Found it-->> " + keyword);
keyword = keywordFile.nextLine();
System.out.println("next keyword " + keyword);
///* reset search to top of site file */
siteFile = new Scanner(new
FileReader(fileChooser.getSelectedFile()));
sCurrentLine = siteFile.nextLine().trim();
}
else {
sCurrentLine = siteFile.nextLine();
if (sCurrentLine == null) {
break;
}
if (!sCurrentLine.matches(keyword)){
System.out.println("The following keyword " + keyword + " does not exist in file "
+ fileChooser.getSelectedFile());
}
}
} //2nd while loop
}
}
catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
siteFile.close();
keywordFile.close();
}
and i have a text file called keywords which has a list of keywords in it,
but my logic is off an I cannot figure out why.
I think I may need to run the loop one last time but not sure how to do that
My issue is that the last word in the keyword file never gets read in. so the program stops at the 2nd to last element in the text file.
I am not sure that I understand what your code should do.
If I understood correctly your code, your task is to read keywords from a file with keywords and then find all keywords in another file. Is it correct?
You should separate reading keywords from the file and search for them in the file. You should 'load' keywords in a list and then search through the file.
To load keywords in list
keywordFile = new Scanner(new File("/home/mearts/keywords.txt"));
List<String> keywordsList = new ArrayList<>();
while (keywordFile.hasNextLine()) {
keywordsList.add(keywordFile.nextLine());
}
And to search for keywords in the file
siteFile = new Scanner((Readable) new FileReader(fileChooser.getSelectedFile()));
while (siteFile.hasNextLine()) {
String sCurrentLine = siteFile.nextLine().trim();
for (String keyword : keywordsList) {
if (sCurrentLine.contains(keyword)) {
System.out.println("Found it-->> " + keyword);
break;
}
}
System.out.println(
"The following keyword " + keyword + " does not exist in file " + fileChooser.getSelectedFile());
}
I hope this will help :)
My program is a Simple Student Management Database which collects the name, subject and phone numbers of students and adds to a database. I've been able to achieve the main logical operations like adding, deleting and searching students in the database. I'm unable to restrict students to enter only approved subjects like for example "English", "Maths" and "Computing" when adding into a HashMap collection. Any help would be appreciated. Here's the main code:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.HashMap;
import java.util.Scanner;
public class Menu {
private HashMap<String, Student> students;
public Menu() {
students = new HashMap<String, Student>();
}
private void eventLoop() {
Scanner scanner = new Scanner(System.in);
int choice = 0;
boolean exit = false;
this.readFromFile();
while (!exit) {
System.out.println("Welcome to Student Management System");
System.out.println("==============================");
System.out.println("(1) Add new student");
System.out.println("(2) Delete a student");
System.out.println("(3) Find Student By Name");
System.out.println("(4) List Students By Subject");
System.out.println("(5) List All Students");
System.out.println("(6) Exit System");
System.out.println("Choose an option: ");
try {
choice = Integer.parseInt(scanner.nextLine());
System.out.print('\u000C');
if (choice < 1 || choice > 6) {
System.err.println("Error : Choose an option between 1 and 6");
choice = 0;
}
} catch (NumberFormatException e) {
System.err.println("Error : Choose an option between 1 and 6");
choice = 0;
}
switch (choice) {
case 1:
this.addStudent(scanner);
break;
case 2:
this.deleteStudent(scanner);
break;
case 3:
this.findStudentByName(scanner);
break;
case 4:
this.findStudentsBySubject(scanner);
break;
case 5:
this.listStudents();
break;
case 6:
this.writeToFile();
exit = true;
}
}
scanner.close();
}
private void findStudentsBySubject(Scanner scanner) {
System.out.println("Enter the exact name of the subject:");
String subjectStr = scanner.nextLine();
boolean atleastOne = false;
for (String name : students.keySet()) {
if (students.get(name).getSubject().getName().toLowerCase().equals(subjectStr.toLowerCase())) {
System.out.println(students.get(name));
atleastOne = true;
}
}
if (!atleastOne) {
System.err.println("No students have enrolled for this subject.");
}
}
private void findStudentByName(Scanner scanner) {
System.out.println("Enter the exact name of the Student to search:");
String name = scanner.nextLine();
if (students.get(name.toLowerCase()) != null) {
System.out.println("Student details:");
System.out.println(students.get(name.toLowerCase()));
} else {
System.err.println(name + " not found in the database.");
}
}
private void deleteStudent(Scanner scanner) {
System.out.println("Enter the exact name of the Student to delete:");
String name = scanner.nextLine();
if (students.get(name.toLowerCase()) != null) {
students.remove(name.toLowerCase());
System.err.println("Student " + name + " deleted from the database.");
} else {
System.err.println(name + " not found in the database.");
}
}
private void addStudent(Scanner scanner) {
System.out.println("The information should be comma separated and in a single line.");
System.out.println("If the name is not unique, the system will throw an error.");
System.out.println("Enter the name, phone and subject of the new student.");
String line = scanner.nextLine();
System.out.print('\u000C');
String[] info = line.split(",");
if (info.length != 3) {
System.err.println("Please enter the information in the proper format.");
return;
}
String name = info[0];
String phone = info[1];
String subjectStr = info[2];
if (students.get(name.toLowerCase()) != null) {
System.err.println("This student already exists in the database.");
return;
}
if (phone.length() != 9) {
System.err.println("The phone number must contain exactly 9 digits.");
return;
}
if (phone.charAt(0) != '9') {
System.err.println("The phone number must start with '9'.");
return;
}
if (!phone.matches("^[0-9]*$")) {
System.err.println("The phone number must contain only numbers.");
return;
}
students.put(name.toLowerCase(), new Student(name, new Subject(subjectStr), phone));
System.err.println("Student added successfully");
}
private void listStudents() {
for (String name : this.students.keySet()) {
System.out.println(this.students.get(name));
}
}
private void readFromFile() {
try {
BufferedReader br = new BufferedReader(new FileReader(new File("./students.txt")));
String line;
while ((line = br.readLine()) != null) {
String[] info = line.split(",");
String name = info[0];
String phone = info[1];
String subjectName = info[2];
if (students.get(name.toLowerCase()) == null) {
Subject subject = new Subject(subjectName);
students.put(name.toLowerCase(), new Student(name, subject, phone));
} else {
System.err.println("There seems to be a duplicate student in the file.");
}
}
br.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private void writeToFile() {
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(new File("./students.txt")));
for (String name : students.keySet()) {
bw.write(students.get(name).toString());
bw.newLine();
}
bw.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Menu menu = new Menu();
menu.eventLoop();
}
}
Subject Class:
public class Subject {
private String name;
public Subject(String subjectName) {
this.setName(subjectName);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String toString() {
return this.getName();
}
}
When you have a restricted list of possible values you can use an Enum e.g.
enum Subject {
English, Maths, Computing
}
A collection of these is just an EnumSet and you can check it's value by calling
EnumSet<Subject> subjects = EnumSet.of(Subject.class);
Subject s = Subject.valueOf(subjectName);
subjects.add(s);
Using stream and filter you can easily achieve this
students.entrySet()
.stream()
.filter(.. some predicate...)
.map(Map.Entry::getValue)
.collect(Collectors.toList())
If you are sure you are going to get at most a single element that passed the filter (which is guaranteed by your filter), you can use findFirst :
Optional<List> o = students.entrySet()
.stream()
.filter( e -> e.getKey() == 1)
.map(Map.Entry::getValue)
.findFirst();
In the general case, if the filter may match multiple Lists, you can collect them to a List of Lists :
List<List> list = students.entrySet()
.stream()
.filter(.. some predicate...)
.map(Map.Entry::getValue)
.collect(Collectors.toList());
i am trying to make a java application with GUI.
i am writing a code that i want to let the User change some data and save these changes on a text file. Before doing that i want to delete the old data that is changed from a list and then rewrite the new data with the last change.if i am missing any class you wanted to see please tell me i will put it online as fast as possible
this is my
public void saveChanges(footBall Player, String name, String level,
int[] scores, int footSize) {
try {
if (CompetitorsList.size() != 0) {
for (Competitors C : CompetitorsList) {
if (C instanceof footBall) {
String Number = Player.playerNumber + "";
if (C.getPlayerNumberAsString().equals(Number)) {
System.out.println("c");
//the error hit me here when i try to remove the object from the list the exception error is java.util.ConcurrentModificationException
CompetitorsList.remove(C);
}
}
}
Name NewName = new Name(name);
System.out.println("Please get in2");
footBall NewPlayer = new footBall(Player.playerNumber, scores,
level, footSize, NewName);
CompetitorsList.add(NewPlayer);
SaveOnFile();
} else {
System.out.println("No List");
}
} catch (Exception ex) {
System.out.print("testing4");
System.out.print("something wrong" + ex);
}
}
this is the SaveOnFile method:
public void SaveOnFile() {
String scoresInString;
FileWriter fw;
try {
fw = new FileWriter("footBall");
for (Competitors C : CompetitorsList) {
if (C instanceof footBall) {
footBall Scores = new footBall();
scoresInString = Scores.returnScoreAsString(C.scores);
fw.write(C.playerNumber + ", " + C.name.getFullName()
+ ", " + C.level + ", " + scoresInString + ","
+ ((footBall) C).footSize() + "\n");
fw.write("\r\n");
}
}
fw.close();
}
// message and stop if file not found
catch (FileNotFoundException fnf) {
System.out.println("File not found ");
System.exit(0);
}
// stack trace here because we don't expect to come here
catch (IOException ioe) {
ioe.printStackTrace();
System.exit(1);
}
}
Calling remove() on a collection invalidates all active iterator. Instead, you have to use the Iterator.remove() method:
for(Iterator<Competitors> it = CompetitorsList.iterator(); it.hasNext(); ) {
Competitors C = it.next();
if(C instanceof ...) {
if(C.getPlayerNumberAsString().equals(Number))
it.remove();
...
This way, the iterator() knows about how the collection changes, which otherwise wouldn't be possible since the ArrayList doesn't track the Iterators it generated.
Alternatively, if you want to use the same "for-next" syntax and not change to the Iterator syntax, collect all the objects to be removed into a temporary collection. e.g.
ArrayList<Competitors> removeThese = new ArrayList<Competitors>();
for (Competitors C : CompetitorsList) {
if (wantToRemove(C)) // your number logic goes here...
removeThese.add(C);
}
CompetitorsList.removeAll(removeThese);