Reputation: 833
I'm trying to print this:
Sun Mon Tue Wed Thur Fri Sat
4 3 8 5 4 4 8
With a user's input of the hours that they worked each day on the same line so then the total hours worked can be calculated, but I'm not even sure if that's possible. Here's a snippet of my current code:
System.out.println("\t"+"\t"+"\t"+"\t"+"\t" + "Sun" + "\t" + "Mon" + "\t" + "Tue" + "\t" + "Wed" + "\t" + "Thur" + "\t" + "Fri" + "\t" + "Sat");
for (int j = 0; j < hoursWorkedPerDay.length; j++) {
System.out.print("Enter hours worked for Employee " + (j+1) + ":"+ "\t" + " " + " ");
for (int k = 0; k < hoursWorkedPerDay[0].length; k++) {
hoursWorkedPerDay[j][k] = scan.nextInt();
}
} // End of for loop
And my current output:
Enter the number of Employee: 2
Enter the name of the employee 1: jeff
Enter the name of the employee 2: lita
Sun Mon Tue Wed Thur Fri Sat
Enter hours worked for Employee 1: 4
6
7
5
8
7
9
Enter hours worked for Employee 2: 3
4
......
My question is, is this possible or would I have to just print each vertically? Thanks!
Upvotes: 0
Views: 2311
Reputation: 27976
If I understand your question, it is 'how do I print an array of numbers on a single line'? If so, there are two main ways:
Use print
for each number (with a tab character between) and then println
to end the line.
Join all the numbers into a single string and then use println
to output the whole string.
Here's a simple Java 8 idiom for printing all the hours in a single statement by joining them into a string with tab delimiters:
IntStream.range(0, employeeCount)
.mapToObj(emp -> Arrays.stream(hoursWorked[emp]).collect(Collectors.joining("\t")))
.forEach(System.out::println);
Upvotes: 1
Reputation: 4465
You have to print the week days before get the value. Like this:
//Code before
String[] weekDays = {"Sun","Mon","Tue", "Wed", "Thur", "Fri", "Sat"};
for (int k = 0; k < hoursWorkedPerDay[0].length; k++) {
System.out.print(weekDays[k] + ": ");
hoursWorkedPerDay[j][k] = scan.nextInt();
System.out.println(); //New line
}
}
Upvotes: 1