The method updateEmployees(PersonnelManager pm) reads a text file and depending on the first character of each line (there are 3 possiblities) on the file, it executes different code. The PersonnelManager and Employee classes have no play in the problem, that's why I'm not including them here. Here is a sample input file:
n Mezalira, Lucas h 40000
r 5
d Kinsey
n Pryce, Lane s 50
r 4
Here is the method:
(the File and Scanner objects are declared out of the method)
public static boolean updateEmployees(PersonnelManager pm) {
try
{
file = new File(updates);
in = new Scanner(file);
}
catch (FileNotFoundException e)
{
System.out.println("Could not load update file.");
return false;
}
int currentLine = 1; //Keep track of current line being read for error reporting purpose
while (in.hasNext()) {
String line = in.nextLine();
//Check the type of update the line executes
//Update: Add new Employee
if (line.charAt(0) == 'n') {
String[] words = line.split("\\s"); //Split line into words. Index [0]: update type. [1]: last name. [2]: first name. [3]: employee type. [4]: wage.
words[1] = words[1].substring(0, words[1].length() - 1); //remove comma from last name
if (words.length != 5) { //If there are not 5 words or tokens in the line, input is incorrect.
System.out.println("Could not update. File contains incorrect input at line: " + currentLine);
return false;
}
if (words[3].equals("s")) //if employee is type SalariedEmployee
pm.addEmployee(new SalariedEmployee(words[2], words[1], Double.parseDouble(words[4])));
else if (words[3].equals("h")) //if employee is type HourlyEmployee
pm.addEmployee(new HourlyEmployee(words[2], words[1], Double.parseDouble(words[4])));
else {
System.out.println("Could not update. File contains incorrect input at line: " + currentLine);
return false;
}
//Display information on the console
System.out.println("New Employee added: " + words[1] + ", " + words[2]);
}
//Update: Raise rate of pay
if (line.charAt(0) == 'r') {
String[] words = line.split("\\s"); //Split line into words. Index [0]: update type. [1]: rate of wage raise
if (Double.parseDouble(words[1]) > 100.0) { //If update value is greater than 100
System.out.println("Error in line:" + currentLine + ". Wage raise rate invalid.");
return false;
}
for (int i =0; i<pm.howMany(); i++) { //Call raiseWages() method for all employees handled by the pm PersonnelManager
pm.getEmployee(i).raiseWages(Double.parseDouble(words[1]));
}
//Display information on the console
System.out.println("New Wages:");
pm.displayEmployees();
}
//Update: Dismissal of Employee
if (line.charAt(0) == 'd') {
String[] words = line.split("\\s"); //Split line into words. Index [0]: update type. [1]: last name of employee
if (words.length != 2) { //If there are not 2 words or tokens in the line, input is incorrect.
System.out.println("Could not update. File contains incorrect input at line: " + currentLine);
return false;
}
String fullName = pm.getEmployee(words[1]).getName(); //Get complete name of Employee from last name
pm.removeEmployee(words[1]);
//Display information on the console
System.out.println("Deleted Employee: " + fullName);
}
currentLine++;
}
return true;
}
Since there are 5 lines in the input file, the while loop should execute 5 times, but that's not what happens. when it reaches the 4th line in the input file: "n Pryce, Lane s 50", I get an "java.lang.StringIndexOutOfBoundsException" error, in line 25 of the code.
The problem occurs at lines 24 and 25:
String[] words = line.split("\\s"); //Split line into words. Index [0]: update type. [1]: last name. [2]: first name. [3]: employee type. [4]: wage.
words[1] = words[1].substring(0, words[1].length() - 1); //remove comma from last name
For the 4th line of input, the "line" String is not split in 5 Strings as it should. It is only split into one, which is in words[0] and it equals "n".
What I don't understand is the program used the same line of code to split the String for the first 3 lines of input, why doesn't it work on the 4th line?
When I change the input file to
n Mezalira, Lucas h 40000
r 5
d Kinsey
removing a second occurrence of the command "n", it works. In fact, every time I use an input file that uses the same command ("n", "r" or "d") more than once, the line where the command happens the second time will only be split into 1 string. containing the first token ( int this case "n", "r", or "d") on the line.
I hope I was clear in my explanation. If anyone know why this happens, please help.
Your split() call should really be split("\\s+") to allow for multiple blanks between fields.
Related
Every time I run it, gives this message (( InputMismatchException )) where is the problem from ?
File f = new File("nameList.txt");
try {
PrintWriter out;
out = new PrintWriter(f);
for (int i = 0; i < 4; i++) {
out.printf("Name : %s Age : %d ", "Rezaee-Hadi", 19);
out.println("");
}
out.close();
} catch (IOException ex) {
System.out.println("Exception thrown : " + ex);
}
try {
Scanner in = new Scanner(f);
String name = in.nextLine();
int age = in.nextInt();
for (int i = 0; i < 4; i++) {
System.out.println(name);
System.out.println(age);
}
in.close();
} catch (FileNotFoundException ex) {
System.out.println("Exception thrown : " + ex);
}
You are creating your data file in the following data format:
Name : Rezaee-Hadi Age : 19
Now, it really doesn't matter (to some extent) how you format your data file as long as you realize that you may need to parse that data later on. You really don't need to maintain a header with your data on each file line. We already know that the first piece of data on any file line is to be a Name and the second piece of data on any file line is to be the Age of the person the Name relates to. So, the following is sufficient:
Rezaee-Hadi, 19
If you want, you can place a header as the very first line of the data file so that it can easily be determined what each piece of data on each line relates to, for example:
Name, Age
Rezaee-Hadi, 19
Fred Flintstone, 32
Tom Jones, 66
John Smith, 54
This is actually a typical format for CSV data files.
Keeping with the file data format you are already using:
There is nothing wrong with using the Scanner#nextLine() method. It's a good way to go but you should be iterating through the file line by line using a while loop because you may not always know exactly how many actual data lines are contained within the file, for example:
Scanner in = new Scanner(f);
String dataLine;
while (in.hasNextLine()) {
dataLine = in.nextLine().trim();
// Skip Blank Lines
if (dataLine.equals("")) {
continue;
}
System.out.println(dataLine);
}
This will print all the data lines contained within your file. But this is not what you really want is it. You want to separate the name and age from each line which means then that you need to parse the data from each line. One way (in your case) would be something like this:
String dataLine;
Scanner in = new Scanner(f);
while (in.hasNextLine()) {
dataLine = in.nextLine().trim();
// Skip Blank Lines
if (dataLine.equals("")) {
continue;
}
String[] dataParts = dataLine.replace("Name : " , "").split(" Age : ");
System.out.println("The Person's Name: " + dataParts[0] + System.lineSeparator()
+ "The Person's Age: " + dataParts[1] + System.lineSeparator());
}
In the above code we iterate through the entire data file one line at a time using a while loop. As each line is read into the dataLine string variable it is also trimmed of any leading or trailing whitespaces. Normally we don't want these. We then check to make sure the line is not blank. We don't normally want these either and here we skip past those blank lines by issuing a continue to the while loop so as to immediately initiate another iteration. If the file line line actually contains data then it is held within the dataLine variable.
Now we want to parse that data so as to retrieve the Name and the Age and place them into a String Array. We do this by using the String#split() method but first we get rid of the "Name : " portion of the line using the String#replace() method since we don't want to deal with this text while we parse the line. In the String#split() method we supply a string delimiter to split by and that delimiter is " Age : ".
String[] dataParts = dataLine.replace("Name : " , "").split(" Age : ");
Now when each line is parsed, the Name and Age will be contained within the dataParts[] string array as elements located at index 0 and index 1. We now use these array elements to display the results to console window.
At this point the Age is a string located in the dataParts[] array at index 1 but you may want to convert this age to a Integer (int) type value. To do this you can utilize the Integer.parseInt() or Integer.valueOf() methods but before you do that you should validate the fact the the string you are about to pass to either of these methods is indeed a string numerical integer value. To do this you would utilize the String#matches() method along with a simple little Regular Expression (RegEx):
int age = 0;
if (dataParts[1].matches("\\d+")) {
age = Integer.parseInt(dataParts[1]);
// OR age = Integer.valueOf(dataParts[1]);
System.out.println("Age = " + age);
}
else {
System.out.println("Age is not a numerical value!");
}
The regular expression "\\d+" placed within the String#matches() method basically means, "Is the supplied string a string representation of a integer numerical value?". If the method finds that it is not then boolean false is returned. If it finds that the value supplied is a string integer numerical value then boolean true is returned. Doing things this way will prevent any NumberFormatException's from occurring.
Replace this:
int age=0;
while (in.hasNext()) {
// if the next is a Int,
// print found and the Int
if (in.hasNextInt()) {
age = in.nextInt();
System.out.println("Found Int value :"
+ age);
}
}
in place of this:
int age = in.nextInt();
Then you will not get "InputMismatchException" anymore..
I was given a file that has list of names phone numbers, calls in and out ect... Like this
Adams#Marilyn#8233331109#0#0#01012014#C
Anderson#John#5025559980#20#15#12152013#M
Baker-Brown#Angelica#9021329944#0#3#02112014#C
The # are delimiters between data items and each line has the call status as the last item.
I need to know how I can display each persons information on the screen in a format such as:
Name Phone Calls Out Calls In Last Call
Marilyn Adams (823) 333-1109 0 0 01-01-2104
John Anderson (502) 555-9980 20 15 12-15-2013
Angelica Baker-Brown (859) 254-1109 11 5 02-11-2014
I have to use substring method to extract the phone number and add parentheses/dashes ect...
So Far my code looks like this
Also I am in a beginners Java coding class....
import java.util.Scanner;
import java.io.*;
public class phonedata2_1 {
public static void main(String[] args) throws IOException {
String Phonefile, FirstName, LastName;
Scanner PhoneScan, fileScan;
System.out.println(" Name Phone Calls Out Calls In Last Call Status");
fileScan = new Scanner(new File("phonedata.txt"));
while (fileScan.hasNext()) {
Phonefile = fileScan.nextLine();
PhoneScan = new Scanner(Phonefile);
PhoneScan.useDelimiter("#");
System.out.println(PhoneScan.next() + " "
+ PhoneScan.next() + "\t"
+ PhoneScan.next() + "\t"
+ PhoneScan.next() + "\t"
+ PhoneScan.next() + "\t"
+ PhoneScan.next() + "\t"
+ PhoneScan.next());
}
System.out.println("\nTotal outgoing calls for the period: " + "\nTotal incoming calls for the period: \n");
}
}
Finds every "data-segment" by using regex, then splits it with # as delimiter and prints it:
File blub = new File("blub.txt");
Scanner scanner = null;
try
{
scanner = new Scanner(blub);
}
catch (FileNotFoundException e){}
while(scanner.hasNext("((\\S*)#){6}(\\w)"))
{
String buffer = scanner.next("((\\S*)#){6}(\\w)");
for(String value : buffer.split("#"))
System.out.print(value + " ");
System.out.println();
}
Output:
Adams Marilyn 8233331109 0 0 01012014 C
Anderson John 5025559980 20 15 12152013 M
Baker-Brown Angelica 9021329944 0 3 02112014 C
Note:
You can use any whitespace character to separate each data-segment in the input file, so even spaces are okay or tabs(or crazy mix with empty lines and so on).
For farther use of data:
you could add your data in an arraylist since buffer.split("#") gives you an array of the data-segment, so you can output it easier with your desired changes to each value.(or in a different order)
You can use the .split(String regex) to split the line you are reading by throwing in the # as a delimeter. This will yield an array which you can traverse and print the contents accordingly. Also, it would seem that besides the initial line, all call data starts with a , so you could use split twice to parse your text file:
EDIT: Seeing your comment, I also realized that afterwards, however, since the .split() method takes a regex as parameter, it means that this problem can be walked around without much hassle. My example is as follows:
String str = "Adams#Marilyn#8233331109#0#0#01012014#C Anderson#John#5025559980#20#15#12152013#M Baker-Brown#Angelica#9021329944#0#3#02112014#C";
String[] lines = str.split(" ");
System.out.println("Name\tPhone\tCalls\tOut\tCalls In\tLast Call");
for(String line : lines)
{
String[] lineInfo = line.split("#");
for(String info : lineInfo)
{
System.out.print(info + "\t");
}
System.out.println();
}
I had to add some extra tabs manually, but this is the output:
Name Phone Calls Out Calls In Last Call
Adams Marilyn 8233331109 0 0 01012014 C
Anderson John 5025559980 20 15 12152013 M
Baker-Brown Angelica 9021329944 0 3 02112014 C
Something like this should work. You still need to do all the manipulation of the phone number yourself, but all the data will be in the call info so you should be able to work from that!
while (fileScan.hasNext())
{
//Get a single record
Phonefile = fileScan.nextLine();
//Seperate the elements of the record
String[] callInfo = Phonefile.split("#");
for(String infoPart: callInfo)
{
System.out.print(infoPart+ "\t");
}
System.out.println()
}
Getting indexOutOfBound. After sucessfull file save. I open the file to perform the following tasks: find the total costPerItem, totalCount and sum. Don't know why I'm getting out of bound. Error points out to
totalCost =+ Double.parseDouble(index[1].trim());
Source code:
Scanner data = new Scanner(new File("registryCopy.txt"));
while(data.hasNext()){
String line = data.nextLine();
System.out.println(line);
String[] index = line.split("\\t");
totalCost += Double.parseDouble(index[1].trim());
totalCount += Integer.parseInt(index[2].trim());
sum = totalCost/totalCount;
}
Error
Error: "main" java.lang.ArrayIndexOutOfBoundsException: 1
which is: totalCost += Double.parseDouble(index[1].trim());
For some reasons it doesn't split the line. Text file is as follow:
item1 15.00 3
item2 15 2
item3 14 3
File was created before and promted the user to input that data. Source for that:
while(userInput.charAt(0)!='n'){
/**
* Need only one PrintWriter out0 i.e, then I either use the userbased name OR fixed name creation i.e registryCopy.txt(true will
* not overwrite the file but go to the next).
*/
PrintWriter out0 = new PrintWriter(new BufferedWriter(new FileWriter("registryCopy.txt", true)));
System.out.print("Enter item name: ");
itemName = kybd.next();//user String
out0.print(itemName + " ");
System.out.print("Enter item price: $");
price = kybd.next();
out0.print(price + " ");
System.out.print("Enter item quantity: ");
quantity = kybd.next();
out0.println(quantity);
System.out.print("Do you have another item to scan? Yes(y) or No(n): ");
userInput= kybd.next();
out0.flush();
}//end of whileLoop
Update1: You guys are awesome, \s did the trick. Beer on me when you're in Chicago. Thanks
OUTPUT sourceCode:
sum = totalCost*totalCount;
System.out.println("");
System.out.println(totalCost);
System.out.println(totalCount);
System.out.println(sum);
SOP:
item1 15.00 3
item2 15 2
item3 14 3
item5 10.00 3
item 5 12
bla 11 5
bread 1 15
item14 5 3
76.0
46
3496.0
Your call of split splits on tabs; it appears that your input has some other kind of whitespace separating the tokens.
To split on any kind of whitespace, use "\\s":
String[] index = line.split("\\s");
\s is a regular expression that stands for any of the following: ' ', '\t', '\n', '\x0B', '\f', '\r'.
Your program that writes the file puts space characters between the fields. Your program that reads the file is trying to split the lines on tab characters, instead of spaces. You have two choices.
Change the program that writes the file, so that it inserts tab characters - that is, wherever you have + " " in the source, change it to + "\t"
Change the program that reads the file, so that it splits the line on space characters. That is, make it line.split(" "). Alternatively, you can use line.split("\\s") which will split on any kind of white space.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
edit: Write a program to read in 100 words from a file. Then, have the user search for a word until they enter 'quit'.
The program will read in up to 100 words from a file. The file may or may not contain 100 words but the array should hold up to 100 (if the list does not contain enough words, fill the rest of the array with empty strings).
After the file is read in, the program will prompt the user for a search string. The program will then search for the string and tell the user if the word was found or not. The program will continue to get search strings from the user until the user enters 'quit'
Hello I need help write a program to find a word from text file
the result should look like:
Enter a word to search for: taco
Word 'taco' was found.
Enter a word to search for: asd
Word 'asd' was NOT found.
and when user enter the word "quit" the program will quit
below is what I have so far and need help to complete
import java.io.*;
import java.util.Scanner;
public class project2 {
public static void main( String[] args ) throws IOException {
String[] list;
String search;
list = load_list( "words.txt" );
search = prompt_user( "\nEnter a word to search for: " );
while ( ! search.equals( "quit" ) ) {
System.out.println( "Word '" + search + "' was" +
( ( find_word( search, list ) ) ? "" : " NOT" ) +
" found." );
search = prompt_user( "\nEnter a word to search for: " );
}
System.out.println();
}
for(String s: list){
if(s.equals(search)){
//do whatever
}
}
Use this:
Scanner txtscan = new Scanner(new File("filename.txt"));
while(txtscan.hasNextLine()){
String str = txtscan.nextLine();
if(str.indexOf("word") != -1){
System.out.println("EXISTS");
}
}
The below code answers this question perfectly:
String word = ""; int val = 0;
while(!word.matches("quit"))
{
System.out.println("Enter the word to be searched for");
Scanner input = new Scanner(System.in);
word = input.next();
Scanner file = new Scanner(new File("newFile.txt"));
while(file.hasNextLine())
{
String line = file.nextLine();
if(line.indexOf(word) != -1)
{
System.out.println("Word EXISTS in the file");
val = 1;
break;
}
else
{
val = 0;
continue;
}
}
if(val == 0)
{
System.out.println("Word does not exist");
}
System.out.println("-------continue or quit--- enter continue or quit");
word = input.next();
}
I am writing a program that will read two files in from the command line and find how many words in each .txt file. I have got my code to work, however I need it to count each apostrophe as 2 words and each hyphen as two words. Something like this e.g., John's will become two separate words "john" and "s" and Tick-Tock will become "tick" and "tock"). I cannot use util.regex also. Here is my code"
private void readFile(){
Scanner scanner = null;
try {
scanner = new Scanner(file);
while (scanner.hasNextLine()){
String line = scanner.nextLine();
String[] wordOfLine = line.split("\\W");
lineNum++;//accumulate line
//counts all words
for (String word : wordOfLine){
if (word.equals("") || !(isWord(word)))
continue;
words.add(word.toLowerCase());
if (wordNum + 1 > wordNum){//in case of overflow
wordNum++;//accumulate word
}
else
throw new Error(" word number overflow!");
}
}
} catch (FileNotFoundException e) {
throw new Error("cannot find file!");
} finally{
if (scanner != null)
scanner.close();
}
System.out.println("Test for wordNum "+wordNum);
}
//check if text is word
private boolean isWord(String text) {
return text.matches("[a-zA-Z]+");
}
For example here is my test end results:
Test for wordNum 54
Test for wordNum 49516
File f1: 9 lines, 54 words, 38 distinct words
File f2: 4666 lines, 49516 words, 5103 distinct words
f1 matches fine with results expected
but f2 needs to be like this
File f2: 4666 lines, 49714 words, 5216 distinct words
here is some of the document for f2
THE PRINCE
by Nicolo Machiavelli
Translated by W. K. Marriott
Nicolo Machiavelli, born at Florence on 3rd May 1469. From 1494 to 1512
held an official post at Florence which included diplomatic missions to
various European courts. Imprisoned in Florence, 1512; later exiled and
returned to San Casciano. Died at Florence on 22nd June 1527.
INTRODUCTION
Nicolo Machiavelli was born at Florence on 3rd May 1469. He was the
second son of Bernardo di Nicolo Machiavelli, a lawyer of some repute,
and of Bartolommea di Stefano Nelli, his wife. Both parents were members
of the old Florentine nobility.
String[] tokens = "abc-def".split("((?<=-)|(?=-))");
System.out.println(Arrays.toString(tokens));
OUTPUT:
[abc, -, def]