Ted Mazer
Ted Mazer

Reputation: 15

Problems Printing out a Concatenated String in Java

The user is prompted for values line by line for the employee data. I chose to scan the whole line and then separate each piece of data into a String array (separated by spaces). I created the variable fullName and concatenated the first and last names of the employees, but when I print out the code it only displays the last name. I've been troubleshooting this for like three hours and haven't found any syntax or logical errors, why won't the full name print out?

\

import java.util.Scanner;
import java.util.ArrayList;
import java.util.Collections;
/**
 * Employee Record Class
 * 
 * @Theodore Mazer
 * @version 9/8/15
 */
public class EmployeeRecord
{
    ArrayList<String> names = new ArrayList<String>();
    ArrayList<String> taxIDs = new ArrayList<String>();
    ArrayList<Double> wages = new ArrayList<Double>();

private String employeeId = "%03d";
private String taxID;
private double hourlyWage = 0.0;

public ArrayList<String> getNamesArrayList(){ //getter method for employee names
    return names;
}
public ArrayList<String> getTaxIdsArrayList(){ //getter method for tax IDs
    return taxIDs;
}
public ArrayList<Double> getWagesArrayList(){ //getter method for hourly wages
    return wages;
}
public void setEmployeeData(){ //setter method for employee data entry
    Scanner scan = new Scanner(System.in);
    String firstName = "";
    String lastName = "";
    String info = "";
    System.out.println("Enter each employees full name, tax ID, and hourly wage pressing enter each time.  (Enter the $ key to finish)");

    while(!(scan.next().equals("$"))){
        info = scan.nextLine();
        String[] splitString = info.split(" ");
        String fullName = "";
        firstName = splitString[0];
        lastName = splitString[1];
        fullName = firstName + " " + lastName;
        double hWage = Double.parseDouble(splitString[3]);
        names.add(fullName);
        taxIDs.add(splitString[2]);
        wages.add(hWage);
    }
    System.out.println("Employee ID  |  Employee Full Name  |  Tax ID  |  Wage  ");    
        for(int i = 0; i <= names.size() - 1; i++){
            System.out.printf(String.format(employeeId, i + 1) + "          | " + names.get(i) + "               |  " + taxIDs.get(i) + " |  " + wages.get(i));
            System.out.println();
        }
}

}

Upvotes: 0

Views: 57

Answers (1)

Nir Alfasi
Nir Alfasi

Reputation: 53535

In the while condition you're using next() which consumes the next token, in your case it's the first name.

I would make two modifications to the while loop:

while (scan.hasNext()) { // <-- check if there's a next token (without consuming it)
    info = scan.nextLine();
    if (info.trim().equals("$")){ // <-- break if the user wants to quit
        break;
    }
    String[] splitString = info.split("\\s+"); // split on any amount/kind of space using regex-split
    String fullName = "";
    firstName = splitString[0];
    lastName = splitString[1];
    System.out.println(Arrays.toString(splitString));
    fullName = firstName + " " + lastName;
    double hWage = Double.parseDouble(splitString[3]);
    names.add(fullName);
    taxIDs.add(splitString[2]);
    wages.add(hWage);
}

Upvotes: 2

Related Questions