How to search a CSV file based on an input field? - java

I am able to access only few number of lines (41 lines to be proper).after that I am unable to read.
import java.io.File;
import java.util.Scanner;
public class FileReader {
public static void main(String[] args) {
String filePath = "qwe.csv";
System.out.println("Enter the City name to be Searched\n");
Scanner in = new Scanner(System.in);
String searchTerm = in.nextLine();
readRecord(filePath, searchTerm);
}
public static void readRecord( String filePath, String searchTerm ) {
boolean found = false;
String City = ""; String City_Asciis = ""; String Lattitude = "";
String longitude = ""; String Country = "";
String iso_2 = ""; String iso_3 = ""; String Admin_Name = "";
String Capital = ""; String Population = ""; String Id = "";
try {
File file = new File(filePath);
Scanner x = new Scanner (file);
x.useDelimiter("[,\n]"); //to separate the data items
//hasNext - Returns true if the scanner has another token/value in its input
while(x.hasNext() && !found) {
City = x.next();
City_Asciis = x.next();
Lattitude = x.next();
longitude = x.next();
Country = x.next();
iso_2 = x.next();
iso_3 = x.next();
Admin_Name = x.next();
Capital = x.next();
Population = x.next();
Id = x.next();
if (City.equals(searchTerm)) {
found = true;
}
}
if (found) {
System.out.println(" The following details are of city : " + City +"\n The Ascii string would be : "
+ City_Asciis +"\n Its having the lattitude around : "
+ Lattitude + "\n and Longitude of : "+ longitude +"\n It is situated in : "
+ Country +"\n These have iso code like : "+ iso_2 +" and : "+ iso_3 +"\n It comes under : "
+ Admin_Name +" State \n Capital of this city is : "+ Capital +"\n The population is around : "
+ Population +"\n ZIP code is : "+Id+"");
}
else {
System.out.print("Enter the Correct City Name");
}
}
catch(Exception e1){
System.out.print("file not found \n");
e1.printStackTrace();
}
}
}
This code will load the searched city from the file path given, so that given a particular city name print the details of the city.

Who knows?
Without getting a headache, the code itself looks as though it should work and I personally can't off hand see why your read will only do 41 lines without doing a series of experiments with actual data and not many people really want to do that which is why you've been asked to provide some sample fictitious data.
It could be as simple as the fact that you are satisfying the boolean found variable criteria within the while loop condition and the loop breaks out stopping the read. I would suspect this since you do indicate that the "code will load the searched city from the file path given". I should think that this is not really what you would want for the mere reason that some countries contain the same City names. As a matter of fact, some States, Provinces, or Regions within the same Country can contain same City names. As an example did you know that in the United States alone, there are 88 Cities and Towns named Washington? I know, weird right, especially when you consider that there are only 50 States and 2 Territories. Benjamin Franklin was also one of the Founding Fathers of the United States and there are 35 cities and towns/villages that honorably carry the name of Franklin within that country.
If your data file or database is big enough then I'm sure you would want to display all cities that match your specific search criteria. With that being said perhaps what you need to do is get rid of that && !found condition for the while loop. I personally wouldn't use the Scanner#hasNext() method in the while loop condition either. It's an invitation for disaster since it's more focused towards checking the availability of tokens when used in combination with Scanner#next() rather than actual file lines. Use the Scanner#hasNextLine() in combination with the Scanner#nextLine() method then use the String#split() method to parse the CSV comma (,) delimited data lines one line at a time.
Below I provide a runnable Java code example which demonstrates the above mentioned methods. Your readRecord() method is used but considerably modified to accommodate the following options:
Return a List Interface (List<String>) of City information found
pertaining to the supplied search criteria.
Ignore (skip past) blank or comment lines within the CSV file. Comment lines can start with # or ;.
The option to ignore letter case during the search.
Allow for selection of the desired City Information field to which
the Search Criteria will be applied to. The City Information Fields
are:
City, CityAscii, Latitude, Longitude, Country, ISO2,
ISO3, AdminName, Capital, Population, and ID
Wildcard (? and *) characters can be used when supplying the desired search field so that the entire field name does not need to be provided, for example: lat* for Latitude. So, you can do a City Information search simply based on population if you like instead of a City Name.
Allow for wildcard (? and *) characters to be used within the
supplied search criteria for example: wash*. This tells the method
to search for any city which name starts with Wash like
Washington or Washougal or Washtucna.
Allow for the number of found city instances to be returned.
Below is the runnable code which demonstrates the above mentioned concepts. The code is well commented. There are Regular Expressions used within the code and if you want an explanation of those expressions then copy/paste them into regex101.com.
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class CityInfoRecords {
public static void main(String[] args) {
/* The appplication is started this way so that there
is no need for static methods or variables.
*/
new CityInfoRecords().startApp(args);
}
// Application Start method.
private void startApp(String[] args) {
String ls = System.lineSeparator(); // Not all OS Consoles work well with "\n"
String filePath = "qwe.csv"; // Path and file name of the data file.
Scanner in = new Scanner(System.in);
// Provide the City Info Field to base search from...
System.out.println("Enter the Data Field you want to search by:" + ls
+ "[City, CityAscii, Lattitude, Longitude, Country" + ls
+ "ISO2, ISO3, AdminName, Capital, Population, ID]" + ls
+ "Wildcards (? and *) can be used:");
String searchField = in.nextLine();
// Provide the Search Criteria to find within the supplied City Info Field.
System.out.println(ls + "Enter the search criteria you are looking for" + ls
+ "in " + searchField + ". Wildcards (? and *) are permitted:");
String searchCriteria = in.nextLine();
// Declare a List Interface of String and fill it
// with the call to the readRecord method.
List<String> cityInfoList = readRecord(filePath, searchField, searchCriteria, 0, "N/A");
// Display the returned List to console window.
for (int i = 0; i < cityInfoList.size(); i++) {
System.out.println(cityInfoList.get(i));
}
}
/**
* Returns a List Interface of the City Information found based on the supplied
* search criteria.<br><br>
*
* #param filePath (String) The full path and file name of the data file to read
* containing City information.<br>
*
* #param searchField (String) The City Information Field to based the supplied
* Search Criteria from. Any City Information Field can be supplied here and
* letter case is optional. The wildcard characters (? and *) can also be used
* here so that the entire field name does not need to be supplied, for example:
* <pre>
* lat* for the Latitude field or
* *asc* for the CityAscii field or
* iso? for either the ISO2 or ISO3 fields or simply
* City for the City field.</pre><br>
*
* The <b>?</b> wildcard character specifies any single alphanumeric character,
* as in ?an, which locates "ran," "pan", "can", and "ban".<br><br>
*
* The <b>*</b> wildcard character specifies zero or more of any alphanumeric
* character, as in corp*, which locates "corp", "corporate", "corporation",
* "corporal", and "corpulent".<br>
*
* #param searchCriteria (String) The search criteria string. This can be any
* string you would like to search for within the supplied City Information
* Field. By default letter case is ignored during searches therefore the
* supplied search criteria string does not need to be letter case specific
* however if you want the search to be case specific then set this methods
* optional ignoreLetterCase parameter to false.<br><br>
*
* Wildcard characters (? and *) can also be used within the Search Criteria
* string so as to expand the search to other possibilities, for example if
* the "City" field is supplied and a criteria string like: "wash*" is supplied
* then any city which name starts with "Wash" will have their city information
* returned.<br><br>
*
* The <b>?</b> wildcard character specifies any single alphanumeric character,
* as in ?an, which locates "ran," "pan", "can", and "ban".<br><br>
*
* The <b>*</b> wildcard character specifies zero or more of any alphanumeric
* character, as in corp*, which locates "corp", "corporate", "corporation",
* "corporal", and "corpulent".<br>
*
* #param numberOfFoundToReturn (int) The number of cities who's information
* should be returned. If 0 is supplied then all cities found will be returned.<br>
*
* #param noDataReplacement (String) Sometimes there is no data supplied for a
* specific field within the data file or the file data line may not contain
* the same amount of delimited data. Rather than returning NULL or Null String
* ("") for empty data fields you can supply here what to actually return in
* such a case. "N/A" is a good choice or perhaps: "Nothing Supplied". Whatever
* you like to use can be supplied here.<br>
*
* #param ignoreLetterCase (Optional - Boolean - Default is true) By default
* searches ignore letter case but if you want your search to be letter case
* specific then you can supply boolean false to this optional parameter.<br>
*
* #return (String List Collection) Information for every City found within the
* supplied data file which matches the supplied field and search criteria.
*/
public List<String> readRecord(String filePath, String searchField,
String searchCriteria, int numberOfFoundToReturn,
String noDataReplacement, boolean... ignoreLetterCase) {
String ls = System.lineSeparator(); // Not all OS Consoles work well with "\n" (property)
boolean ignoreCase = true; // Ignore letter case when searching (Default - property)
if (ignoreLetterCase.length > 0) {
ignoreCase = ignoreLetterCase[0];
}
boolean found = false; // Flag to indicate data was found (toggles)
int foundCounter = 0; // Indicates number of same data found (increments)
List<String> returnableList = // The List of found city information that will be returned (collection)
new ArrayList<>();
// City Information Variables (data fields)
String city;
String cityAscii;
String latitude;
String longitude;
String country;
String iso2;
String iso3;
String adminName;
String capital;
String population;
String id;
// Open Scanner to read data file...
// Try With Resources is used here to auto close the reader.
try (Scanner fileReader = new Scanner(new File(filePath))) {
// Iterate through data file...
while (fileReader.hasNextLine()) {
// Read file line by line and remove leading or
// trailing whitespaces, tabs, line breaks, etc.
String cityData = fileReader.nextLine().trim();
// Skip blank or comment lines (comment lines can be lines that start with # or ;)
if (cityData.equals("") || cityData.startsWith("#") || cityData.startsWith(";")) {
continue; // Get next file line
}
// Split the read line based on any comma delimited anomaly.
String[] cityInfo = cityData.split(",|,\\s+|\\s+,|\\s+,\\s+");
// The number of data pieces split from data line.
// Not all lines may contain the same amount of data.
int i = cityInfo.length;
/* Ternary is used to fill city information variables
so that data not provided will not be null or null string.
As an Example for the city variabel this is the same as:
if (i >= 1 && !cityInfo[0].equals("")) {
city = cityInfo[0].trim();
}
else {
city = noDataReplacement;
}
*/
city = (i >= 1 && !cityInfo[0].equals("")) ? cityInfo[0].trim() : noDataReplacement;
cityAscii = (i >= 2 && !cityInfo[1].equals("")) ? cityInfo[1].trim() : noDataReplacement;
latitude = (i >= 3 && !cityInfo[2].equals("")) ? cityInfo[2].trim() : noDataReplacement;
longitude = (i >= 4 && !cityInfo[3].equals("")) ? cityInfo[3].trim() : noDataReplacement;
country = (i >= 5 && !cityInfo[4].equals("")) ? cityInfo[4].trim() : noDataReplacement;
iso2 = (i >= 6 && !cityInfo[5].equals("")) ? cityInfo[5].trim() : noDataReplacement;
iso3 = (i >= 7 && !cityInfo[6].equals("")) ? cityInfo[6].trim() : noDataReplacement;
adminName = (i >= 8 && !cityInfo[7].equals("")) ? cityInfo[7].trim() : noDataReplacement;
capital = (i >= 9 && !cityInfo[8].equals("")) ? cityInfo[8].trim() : noDataReplacement;
population = (i >= 10 && !cityInfo[9].equals("")) ? cityInfo[9].trim() : noDataReplacement;
id = (i >= 11 && !cityInfo[10].equals("")) ? cityInfo[10].trim() : noDataReplacement;
// Determine the city data field we want to search in
String regex;
// Were wildcards used in the supplied Search Field string?
if (searchField.contains("?") || searchField.contains("*")) {
// Yes... Prep regex to get proper search field
regex = searchField.replace("?", ".?").replace("*", ".*?").toLowerCase();
}
else {
regex = "(?i)(" + searchField + ")";
}
// Get proper search field data
String field = "";
if ("city".toLowerCase().matches(regex)) {
field = city;
}
else if ("cityAsciis".toLowerCase().matches(regex)) {
field = cityAscii;
}
else if ("lattitude".toLowerCase().matches(regex)) {
field = latitude;
}
else if ("longitude".toLowerCase().matches(regex)) {
field = longitude;
}
else if ("country".toLowerCase().matches(regex)) {
field = country;
}
else if ("iso2".toLowerCase().matches(regex)) {
field = iso2;
}
else if ("iso3".toLowerCase().matches(regex)) {
field = iso3;
}
else if ("adminName".toLowerCase().matches(regex)) {
field = adminName;
}
else if ("capital".toLowerCase().matches(regex)) {
field = capital;
}
else if ("population".toLowerCase().matches(regex)) {
field = population;
}
else if ("id".toLowerCase().matches(regex)) {
field = id;
}
if (field.equals("")) {
System.err.println("Invalid Search Field Name Provided! (" + searchField + ")");
return returnableList;
}
// See if the search criteria contains wildcard characters
// A search can be carried out using wildcards in this method.
if (searchCriteria.contains("?") || searchCriteria.contains("*")) {
// There is...build the required Regular Expression (RegEx) to use.
regex = searchCriteria.replace("?", ".?").replace("*", ".*?");
// See if the data item matches the search criteria ignoring letter case if desired.
// The String.matches() method is used for this and ternary for ignoring letter case.
if (ignoreCase ? field.toLowerCase().matches(regex.toLowerCase()) : field.matches(regex)) {
found = true; // toogle flag to true if there is a match.
}
}
// No wildcard characters in search criteria...
// Ternary is used in condition to handle ignore letter case if desired.
else if (ignoreCase ? field.equalsIgnoreCase(searchCriteria) : field.equals(searchCriteria)) {
found = true; // toogle flag to true if there is a match.
}
// If the 'found' flag has been set to true...
if (found) {
// Add City information to returnable ArrayList
String info = ls + "The following details are of city: " + city + ls
+ "The Ascii string would be: " + cityAscii + ls
+ "It has the approximate Lattitude of: " + latitude + ls
+ "And the approximate Longitude of: " + longitude + ls
+ "It is situated in the country of: " + country + ls
+ "The city has iso codes like: " + iso2 + " and: " + iso3 + ls
+ "The State/Province/Region is: " + adminName + ls
+ "Capital of this city is: " + capital + ls
+ // Didn't know cities had capitals
"The population is approximately: " + population + ls
+ "City general ZIP code is: " + id;
returnableList.add(info); // Add to list
found = false; // Toggle found flag back to false in prep to locate more city data.
foundCounter++; // increment the found counter.
// If the First Instance Only flag is true then...
if (numberOfFoundToReturn > 0 && foundCounter == numberOfFoundToReturn) {
// Break out of the 'while' loop. We don't need anymore cities.
break;
}
}
}
// If the Found Counter was not incremented then
// we didn't find any data in file... Inform User.
if (foundCounter == 0) {
System.err.print(ls + "Can not find City Name (" + searchCriteria
+ ") in data file!" + ls);
}
}
catch (FileNotFoundException ex) {
System.err.print("City Data file not found! (" + filePath + ")" + ls);
}
// Return the List of found data.
return returnableList;
}
}
Create a new Java Application Project and name it CityInfoRecords. Copy and paste the above code over top of the Main Startup class. Run the application, read the console prompts carefully and enter the proper data.
The first prompt asks for a City Information Field name...enter: city.
The second prompt will ask for the search criteria for city...enter a city name in Upper or lower case (it doesn't matter). The city information will be displayed in console but only if that city name is contained within the City field within the data file.
Now run the code again and enter the same data except this time, for the city name just provide the first three letters of a city name and an asterisk (*) and then hit the enter key. Now any city information within your specific City Data File which starts with those supplied three letters will be displayed within the Console Window.
Play with it, try different fields to search from and play with the wildcard characters as well with your supplied field or search criteria data.
Now make readRecord a Class instead of a method which would be better.

Related

How to control the comparator to avoid the , inside the " " in a csv file

I am trying to sort the values by age and I get an error. There is a method in the program where it separates the values by ",". Upon inspecting the error, it seems that it also considers the , inside the string address (which it shouldn't do). Any ideas as to how I can make it ignore the , inside the double quoted values?
Here's the part of the code where I split the values:
while ((string = input.readLine()) != null) { // Reads each line and loops until there is no more text to be read
String[] list = string.split(",", 8); // Uses "," to split the data
String name = list[0] + "," + list[1]; // The parts of data is assigned their specific variable
String email = list[2];// The parts of data is assigned their specific variable
String address = list[3];
int age = Integer.parseInt(list[4]); // Using parseInt, the data becomes an integer so comparison will be possible
String residency = list[5];
int district = Integer.parseInt(list[6]);
String gender = list[7];
person.add(new Person(name, email, address, age, residency, district, gender)); // Passes the information from the txt file
}

Problem with record existence + text field criteria

Basically, a have a sign-up interface THAT should empty fileds and certain conditions and if record exist in data basewhere first it checks if text fields are empty as written below :
private void confirmActionPerformed(java.awt.event.ActionEvent evt) {
// String f= "jdbc:derby://localhost:1527/BStest";
String name=username.getText();
String gender=null;
String passw=String.valueOf(pass.getPassword());
String repassw=String.valueOf(repass.getPassword());
String phone2=phone.getText();
String emaill=email.getText();
boolean checkage=false,checknum=false,checkcap=false,checklaw=false,checkemail=false,checkdomain=false;
boolean checklen=false,checkbfat=false,checkpass=false,checkchar=false,checkgender=false;
boolean checkempty=true;
if(name.isEmpty()){//is text field empty
checkempty=false;
JOptionPane.showMessageDialog(null,"Add a username!");}
if(passw.isEmpty()){//is text field empty
checkempty=false;
JOptionPane.showMessageDialog(null,"Add a Password!");}
if(!repassw.equals(passw)){//confirmed password is the same for password
checkempty=false;
JOptionPane.showMessageDialog(null,"Type password Again!");
}
if(phone2.isEmpty()){//is text field empty
checkempty=false;
JOptionPane.showMessageDialog(null,"Add Phone number !");
}
if(emaill.isEmpty()){//is text field empty
checkempty=false;
JOptionPane.showMessageDialog(null,"Add email please !");
}
if (!m.isSelected()&& !female.isSelected()){//no gender selected
checkempty=false;
JOptionPane.showMessageDialog(null,"Choose a gender !!");}
else if (m.isSelected()&& female.isSelected()){//both selected it shoud be only one
checkempty=false;
JOptionPane.showMessageDialog(null,"Select only One !!");}
if(age.getText().isEmpty()){//is age text field has no input
checkempty=false;
JOptionPane.showMessageDialog(null,"Enter your age !!");}
Secondly checks the criteria for each text field as indicated in the comments below
if (m.isSelected()){// is female or male selected
checkgender=true;
gender="Male";
}
else if (female.isSelected()){
checkgender=true;
gender="Female";
}
int agee =Integer.parseInt(age.getText());
char c,c2=emaill.charAt(0);
String zereofive=phone2.substring(0, 2);//start with 05
String bfat = emaill.substring(0,emaill .indexOf("#"));//digits berfore #
String domain = emaill.substring(emaill .indexOf("#") + 1);//check domain
if(Character.isLetter(c2))//is first char off email iis letter
checkchar=true;
else
JOptionPane.showMessageDialog(null,"Emails only start with letters");
if (phone2.length()==10 && zereofive.equals("05"))//check length of phone number
checklen=true;
else
JOptionPane.showMessageDialog(null,"enter 10 digits for the phone number and starts with 05");
if(bfat.length()>=6 || bfat.length()<=15){//check digits before #
checkemail=true;
}
if(domain.equals("gmail.com") || domain.equals("hotmail.com")){//check domain of email
checkdomain=true;
}
else
JOptionPane.showMessageDialog(null,"Email domain is wrong");
if(agee>=18)//only can register in 18 or above
checkage=true;
else
JOptionPane.showMessageDialog(null,"You can't register because you are under 18. ");
if(passw.length()>7){// password at least 8 digit at least one captial and one small letter
checklen=true;
for (int i=0;i<passw.length();i++){
c=passw.charAt(i);
if(Character.isDigit(c))
checknum=true;
else if (Character.isUpperCase(c))
checkcap=true;
else if (Character.isLowerCase(c))
checklaw=true;
if(checknum && checkcap && checklaw )
checkpass=true;
}}
if(checkpass)//check of all 3 crietria of pass word is coorrect
checkpass=true;
else
JOptionPane.showMessageDialog(null,"password must be at least 8 digits \n at least 1 Upper Case letter \n at least 1 Lower Case letter \n at least 1 number \n");
and lastly stores in the database if criteria is meet and record is not exists
PreparedStatement reg,exist;
String query="INSERT INTO customer (Cu_name, Cu_password, Cu_age, Cu_gender, Cu_email, Cu_phone) VALUES (?, ?, ?, ?, ?, ?)";//enter user info in database
String record_exists="SELECT * FROM customer where Cu_name=? and Cu_password= ? and Cu_age=? and Cu_gender=? and Cu_email=? and Cu_phone=?";
try{
String f= "jdbc:derby://localhost:1527/BStest";
Connection connection = DriverManager.getConnection(
f, "meme", "Moudhi102");
reg=connection.prepareStatement(query);
exist=connection.prepareStatement(record_exists);
reg.setString(1, name);
reg.setString(2, passw);
reg.setInt(3, agee);
reg.setString(4, gender);
reg.setString(5, emaill);
reg.setString(6, phone2);
exist.setString(1, name);
exist.setString(2, passw);
exist.setInt(3, agee);
exist.setString(4, gender);
exist.setString(5, emaill);
exist.setString(6, phone2);
rs=exist.executeQuery();
if(rs==null){
if(checkpass &&checkage && !checkdomain && !checkemail && !checklen && checkchar && !checkgender && checkempty ){//if all criteria justified then add it to database
reg.executeUpdate();
JOptionPane.showMessageDialog(null,"Complete! new user added !! ");
}
}else//else it can't be added
{
JOptionPane.showMessageDialog(null, "Record already exists");
}
}
catch(SQLException ex){
JOptionPane.showMessageDialog(null,ex);
}
the problem is it doesn't show a message if record already exists in database and if I entered empty text in age + phone + email it shows the message that it is empty but in the NetBeans itself shows an error message in the output area like below
at
java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:592)
at java.lang.Integer.parseInt(Integer.java:615)
at event_system.Sign_up.confirmActionPerformed(Sign_up.java:275)
at event_system.Sign_up.access$000(Sign_up.java:20)
at event_system.Sign_up$1.actionPerformed(Sign_up.java:70)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6533)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6298)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4889)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
and doesn't do the coding part of these three fields and show their messages even for password if criteria is not meet and other field inputs are ok is shows a continuous message of record exists and Can't register check/fill you information.Please help me i'm stuck and can't figure it out
Deal with your exception first before even considereing to deal with your other problems since it will prevent the other portions of your code to be run from the point of exception and therefore be the root cause to all your problems at this time.
You can not pass a Null String ("") to the Integer#parseInt() method:
int agee = Integer.parseInt(age.getText());
If the Text Field that is to contain an age is empty then the JTextField#getText() method will return a Null String (""). If you supply this to the Integer#parseInt() method (as you've done in the above code) or anything other than an integer value then a NumberFormatException is generated. You need to take care of this possible condition and the use of a Ternary Operator (aka Conditional Operator) may be all you need:
int agee = age.getText().trim().equals("") ? 0 : Integer.parseInt(age.getText().trim());
This however doesn't cover the fact that a alpha character might accidently be supplied within the Age text field. To cover that problem it may be just better to do:
String theAge = age.trim().getText();
int agee = 0;
// Regular Expression used to ensure a Integer
// Numerical String value was supplied.
if (theAge.matches("\\d+")) {
agee = Integer.parseInt(theAge);
}
In the above line of code, IF the Age text-field contains nothing or blank spaces then the agee variable is supplied a integer 0 value ELSE the Integer.parseInt() method is used with the contents of the Text Field to fill the agee variable.
On a side note:
You utilize a lot of boolean flags for each of your text fields so as to ensure validity and other things. From what I can see you actually don't need any of them since you are checking all fields and your requirements are that all those fields actually be filled:
boolean checkage=false, checknum=false, checkcap=false, checklaw=false,
checkemail=false,checkdomain=false;
boolean checklen=false, checkbfat=false, checkpass=false, checkchar=false,
checkgender=false;
boolean checkempty=true;
I would get rid of all of them and and not use a flag at all. If at any time within your code any one of your Text Fields does not meet validity within the confirmActionPerformed() event then nothing should be read from or written to the database. As the fields are checked for validity and any of those fields fail you simply need to display the fact via Message Box then when the User selects the OK button we set focus on the Text Field currently being checked (age.requestFocus();) then exit the event with a return; statement. Database code can never be reached unless all your text fields passes validity. Your event code might look like this:
private void confirmActionPerformed(java.awt.event.ActionEvent evt) {
// String f= "jdbc:derby://localhost:1527/BStest";
String name = username.getText().trim();
String gender = null;
String passw = String.valueOf(pass.getPassword());
String repassw = String.valueOf(repass.getPassword());
String phone2 = phone.getText().trim();
String emaill = email.getText().trim();
// NAME
//is Name text field empty
if (name.isEmpty()) {
JOptionPane.showMessageDialog(null, "Add a username!");
username.requestFocus();
return;
}
// PASSWORD
//is Password field empty
if (passw.isEmpty()) {
JOptionPane.showMessageDialog(null, "Add a Password!");
pass.requestFocus();
return;
}
/* A Regular Expression is used here to ensure your desired
password rules apply - Minimum of 8 characters in length,
at least 1 Uppercase Letter, at least 1 Lowercase letter,
at least 1 digit. */
if (!passw.matches("^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)[a-zA-Z\\d]{8,}$")) {
JOptionPane.showMessageDialog(null, "Password must be:\n\n"
+ "- At least 8 characters in length,\n"
+ "- Contain at least 1 Uppercase letter,\n"
+ "- Contain at least 1 Lowercase letter,\n"
+ "- Contain at least 1 number.");
pass.requestFocus();
return;
}
// confirmed password is the same for password
if (!repassw.equals(passw)) {
JOptionPane.showMessageDialog(null, "Type password Again!");
repass.requestFocus();
return;
}
// PHONE
//is phone text field empty
if (phone2.isEmpty()) {//is text field empty
JOptionPane.showMessageDialog(null, "Add Phone number !");
phone.requestFocus();
return;
}
// starts with 05
String zereofive = phone2.substring(0, 2);
// check length of phone number
if (phone2.length() != 10 && !zereofive.equals("05")) {
JOptionPane.showMessageDialog(null, "enter 10 digits for the phone number and starts with 05");
phone.requestFocus();
return;
}
// E-MAIL
//is E-Mail text field empty
if (emaill.isEmpty()) {
JOptionPane.showMessageDialog(null, "Add email please !");
email.requestFocus();
return;
}
// E-Mail
char c, c2 = emaill.charAt(0);
String bfat = emaill.substring(0, emaill.indexOf("#"));//digits berfore #
String domain = emaill.substring(emaill.indexOf("#") + 1);//check domain
// is first char off email a letter
if (!Character.isLetter(c2)) {
JOptionPane.showMessageDialog(null, "Emails can only start with letters!");
email.requestFocus();
return;
}
//check digits before #
if (bfat.length() < 6 || bfat.length() > 15) {
JOptionPane.showMessageDialog(null, "Invalid digits in Email address!");
email.requestFocus();
return;
}
//check domain of email
if (!domain.equals("gmail.com") && !domain.equals("hotmail.com")) {
JOptionPane.showMessageDialog(null, "Email domain is wrong!");
email.requestFocus();
return;
}
// GENDER
//no gender selected
if (!m.isSelected() && !female.isSelected()) {
JOptionPane.showMessageDialog(null, "Choose a Gender!!");
return;
}
//both Genders are selected where it shoud be only one
else if (m.isSelected() && female.isSelected()) {
JOptionPane.showMessageDialog(null, "Select only One Gender!!");
return;
}
// is female or male selected
if (m.isSelected()) {
gender = "Male";
}
else if (female.isSelected()) {
gender = "Female";
}
// AGE
//is Age text field empty
String theAge = age.trim().getText();
if (theAge.isEmpty()) {
JOptionPane.showMessageDialog(null, "Enter your age !!");
age.requestFocus();
return;
}
int agee = 0; // Default
// Regular Expression used to ensure a Integer
// Numerical String value was supplied.
if (theAge.matches("\\d+")) {
agee = Integer.parseInt(theAge);
}
// only 18 + can register
if (agee < 18) {
JOptionPane.showMessageDialog(null, "You can't register because you are under 18!");
age.requestFocus();
return;
}
// DATABASE
Connection connection = null;
PreparedStatement reg, exist;
ResultSet rs = null;
String query = "INSERT INTO customer (Cu_name, Cu_password, Cu_age, Cu_gender, Cu_email, Cu_phone) VALUES (?, ?, ?, ?, ?, ?)";//enter user info in database
String record_exists = "SELECT * FROM customer where Cu_name=? and Cu_password= ? and Cu_age=? and Cu_gender=? and Cu_email=? and Cu_phone=?";
try {
String f = "jdbc:derby://localhost:1527/BStest";
connection = DriverManager.getConnection(f, "meme", "Moudhi102");
reg = connection.prepareStatement(query);
exist = connection.prepareStatement(record_exists);
reg.setString(1, name);
reg.setString(2, passw);
reg.setInt(3, agee);
reg.setString(4, gender);
reg.setString(5, emaill);
reg.setString(6, phone2);
exist.setString(1, name);
exist.setString(2, passw);
exist.setInt(3, agee);
exist.setString(4, gender);
exist.setString(5, emaill);
exist.setString(6, phone2);
rs = exist.executeQuery();
if (rs == null) {
reg.executeUpdate();
JOptionPane.showMessageDialog(null, "Complete! new user added !! ");
}
// User Already Exists In Database
else {
JOptionPane.showMessageDialog(null, "User already exists");
}
}
catch (SQLException ex) {
JOptionPane.showMessageDialog(null, ex);
}
finally {
if (reg != null) {
reg.close();
}
if (rs != null) {
rs.close();
}
if (connection != null) {
connection.close();
}
}
}
Age may not necessarily be a good piece of criteria to use in your query WHERE clause when checking database records for a duplicate record of a particular User. It only enforces the fact that each record is only valid for a single year. Things change and Age, E-Mails, Phone Numbers, etc are some of those things that can change often. There is obviously nothing wrong with the concept if only the Name and Password are used for access validity (which should suffice for most cases) and the remaining criteria is merely used for indicating that updating is required to the User data account.
You should consider Supplied Passwords to be hashed and it is the hash that is stored in database. When a User supplies a password then that password is hashed and compared to a hash already stored in database. No one, including yourself should ever know what the password truly is. Only the User should ever know what the password might be. jBCrypt works pretty good for this. To hash a password using this library:
Imports:
import org.mindrot.jbcrypt.BCrypt;
import static org.mindrot.jbcrypt.BCrypt.hashpw;
A Class Member Varaible:
// Define the BCrypt WORKLOAD to use when generating
// password hashes. 10-31 is a valid value.
private static final int WORKLOAD = 12;
To Hash a plaintext password so as to store in database:
/**
* This method can be used to generate a string representing an account
* password suitable for storing in a database. It will be an OpenBSD-style
* crypt(3) formatted hash string of length=60 The BCrypt WORKLOAD is
* specified in the above static variable, a value from 10 to 31. A WORKLOAD
* of 12 is a very reasonably safe default. This automatically handles
* secure 128-bit salt generation and storage within the hash.
*
* #param password_plaintext The account's plaintext password as provided
* during account creation, or when changing an
* account's password.
*
* #return String - a string of length 60 that is the bcrypt hashed password
* in crypt(3) format.
*/
public static String hashPassword(String password_plaintext) {
String salt = BCrypt.gensalt(WORKLOAD);
String hashedPassword = hashpw(password_plaintext, salt);
return hashedPassword;
}
To check a plaintext password with a stored hash from database:
/**
* This method can be used to verify a computed hash from a plaintext (e.g.
* during a login request) with that of a stored hash from a database. The
* password hash from the database must be passed as the second argument.
*
* #param passwordAsPlainText The accounts plaintext password, as provided
* during a login request
*
* #param storedEncryption The accounts stored password hash, retrieved
* from the authorization database
*
* #return boolean - true if the password matches the password of the stored
* hash, false otherwise
*/
/*
public static boolean checkPassword(String passwordAsPlainText, String storedEncryption) {
boolean passwordVerification;
if (null == storedEncryption || !storedEncryption.startsWith("$2a$")) {
throw new java.lang.IllegalArgumentException(
"Invalid encryption provided for comparison");
}
passwordVerification = checkpw(passwordAsPlainText, storedEncryption);
return passwordVerification;
}

How can I check if a string is available in a table?

Here's the table (tblemployees)
How can I convert my code that used text files to a code that will use database (tables).
int intcurrentLine = -1;
String[] strLineSplit = (sb.toString()).split("\\r?\\n"); // converts sb to string then splits it by line
int intNumElements = strLineSplit.length; // number of elements
while (intcurrentLine != (intNumElements - 1)) {
intcurrentLine++;
String[] strWords = (strLineSplit[intcurrentLine]).split(" ", 2); // splits the current line by the first instance(space)
if (strEmpID.equals(strWords[0])) { // checks if the employee ID is available
JOptionPane.showMessageDialog(null, "Welcome " + strWords[1] + ", you have successfully logged in.");
strCheck = 1; // to confirm and go to time in and out process
break;
}
if ((intcurrentLine + 1) == intNumElements) { // condition to state that ID cant be found from the employee list
JOptionPane.showMessageDialog(null, "No such employee, please check the ID No. that you entered.");
}
}
Now I would like to search a column if it contains an Employee number. How do I put it to a condition, I've been searching but unable to find a clear answer. They only put how to search like this
String queryCheck = "SELECT * from messages WHERE EmpIDNo = 'COMSCI0001'";
ResultSet res = st.executeQuery(queryCheck);
then I'm lost, how to make a condition where if the employee no. doesn't exists something would happen else something would happen. I'm just confuse how to make a condition for that.
You can do like this:
String queryCheck = "SELECT * FROM messages WHERE EmpIDNo = 'COMSCI0001' LIMIT 1";
ResultSet res = st.executeQuery(queryCheck);
boolean exists = res.next();
The boolean variable exists will indicate whether a matching record exists or not.
Notice that I added LIMIT 1 at the end of the SQL as an optimization to avoid fetching more data than you really need.

Changing Name Format from "Last, First MI." to "First MI. Last" in Java

I'm trying take the names that I am given by the program after it retrieves them from unstructured text, and display them in a user specified Format of either "First MI. Last" or "Last, First MI". Any ideas? So far it checks to see if a comma is present in the string. if so I want to switch the order of the words in the string and remove the comma, and if there is a middle initial and it doesnt' contain a period after it, I want to add one.
if (entity instanceof Entity) {
// if so, cast it to a variable
Entity ent = (Entity) entity;
SName name = ent.getName();
String nameStr = name.getString();
String newName = "";
// Now you have the name to mess with
// NOW, this is where i need help
if (choiceStr.equals("First MI. Last")) {
String formattedName = WordUtils
.capitalizeFully(nameStr);
for (int i = 0; i < formattedName.length(); i++) {
if (formattedName.charAt(i) != ',') {
newName += formattedName.charAt(i);
}
}
}
name.setString(newName);
network.updateConcept(ent);
Use a regular expression and String.replaceAll:
"Obama, Barack H.".replace("(\\w+), (\\w+) (\\w\\.)", "$2 $3 $1")
The result is Barack H. Obama.
This would be easier with substring. This assumes that the format is valid (you'll have to check that).
//Separate the names
String newName;
String lastName = name.substring(0, name.indexOf(","));
String firstName = name.substring(name.indexOf(",")+1);
//Check for a space indicating a middle Name
//Check to see if the middle name already has the period if not add it
if(firstName.trim().contains(" ") && !firstName.contains(".")) {
firstName += ".";
}
newName = firstName + " " + lastName;
//Set the name to whatever you're using
Note this won't work if names are allowed to contain "," " " or "."

Java: 2 runtime errors I can't figure out

I am working on a homework assignment, and I am going a little "above and beyond" what is called for by the assignment. I am getting a run-time error in my code, and can not for the life of me figure out what it is that I have done wrong.
Here is the assignment:
Write a program that displays a simulated paycheck. The program should ask the user to enter the date, the payee’s name, and the amount of the check. It should then display a simulated check with the dollar amount spelled out.
Here is my code:
CheckWriter:
/* CheckWriter.java */
// Imported Dependencies
import java.util.InputMismatchException;
import java.util.Scanner;
public class CheckWriter {
public static void main(String args[]) {
Scanner keyboard = new Scanner(System.in);
// Try to get the name
String name = "";
NameValidator validateName = new NameValidator();
while (validateName.validate(name) == false) {
System.out.println("Enter the name: ");
name = keyboard.nextLine();
if (validateName.validate(name) == false) {
System.out.println("Not a valid name.");
}
}
// Get the date
String date = "";
DateValidator validateDate = new DateValidator();
while (!validateDate.validate(date)) {
System.out.println("Enter the date (dd/mm/yyyy): ");
date = keyboard.nextLine();
if (!validateDate.validate(date)) {
System.out.println("Not a valid date.");
}
}
// Try to get the amount of the check
String checkAmount = "";
CurrencyValidator validateCurrency = new CurrencyValidator();
while (!validateCurrency.validate(checkAmount)) {
System.out.print("Enter the Check Amount (XX.XX): $");
checkAmount = keyboard.nextLine();
if (!validateCurrency.validate(checkAmount)) {
System.out.println("Not a valid check amount.");
}
}
String checkWords = checkToWords(checkAmount); // ERROR! (48)
System.out
.println("------------------------------------------------------\n"
+ "Date: "
+ date
+ "\n"
+ "Pay to the Order of: "
+ name
+ " $"
+ checkAmount
+ "\n"
+ checkWords
+ "\n"
+ "------------------------------------------------------\n");
}
private static String checkToWords(String checkAmount) {
/**
* Here I will use the string.split() method to separate out
* the integer and decimal portions of the checkAmount.
*/
String delimiter = "\\.\\$";
/* Remove any commas from checkAmount */
checkAmount.replace(",", "");
/* Split the checkAmount string into an array */
String[] splitAmount = checkAmount.split(delimiter);
/* Convert the integer portion of checkAmount to words */
NumberToWords intToWord = new NumberToWords();
long intPortion = Long.parseLong(splitAmount[0]); // ERROR! (84)
intToWord.convert(intPortion);
String intAmount = intToWord.getString() + " dollars";
/* Convert the decimal portion of checkAmount to words */
String decAmount = "";
long decPortion = Long.parseLong(splitAmount[1]);
if (decPortion != 0) {
NumberToWords decToWord = new NumberToWords();
decToWord.convert(Long.parseLong(splitAmount[1]));
decAmount = " and " + decToWord.getString() + " cents.";
}
return (intAmount + decAmount);
}
}
Note that I am using external class files to handle validation of the name, date, currency, and conversion from numbers to words. These class files all work as intended.
The error I am getting is:
Exception in thread "main" java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Long.parseLong(Unknown Source)
at java.lang.Long.parseLong(Unknown Source)
at CheckWriter.checkToWords(CheckWriter.java:82)
at CheckWriter.main(CheckWriter.java:46)
I have commented the lines in my code that are causing the errors that I am experiencing.
Could someone please assist me in figuring where my code is going wrong? I can include the other class files if you feel that it would be needed.
EDIT: When I run the code, it asks for the name and date. Before asking for the check amount is when it throws the error.
EDIT 2: A huge thank you to cotton.m! Thanks to his advice, I have changed the while statements to look like this:
while(!validateDate.validate(date) && date == "")
This has now fixed my issue. It would appear that when validating data with a regex expression, an empty string will return true.
The String you are trying to parse in an empty length string.
My suggestion would be to
1) Check the value of checkAmount at the start of checkToWords - if it is blank there's your problem
2) Don't do that split. Just replace the $ like you did the , (I think this is your real problem)
Also you are going to have another issue in that 10000.00 is not a long. I see you are splitting out the . but is that really what you want?
It is NumberFormatException, the value in checkAmount (method parameter) is not a valid Number.
You need to set checkAmount=checkAmount.replace(",", "");
Otherwise checkAmount will still have , inside and causes NumberFormatExcpetion.
Your issue is with your delimiter regex, currently you are using \.\$ which will split on a literal . followed by a literal $. I'm assuming that what you are actually intending to do is to split on either a . or a $, so change your delimiter to one of the following:
String delimiter = "\\.|\\$"
or
String delimiter = "[\\.\\$]"
As your code is now, checkAmount.split(delimiter) is not actually successfully splitting the string anywhere, so Long.parseLong(splitAmount[0]) is equivalent to Long.parseLong(checkAmount).
It should be:
String delimiter = "[\\.\\$]";
and then you have to check that splitWord[i] is not empty.

Categories