Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

cant figure out how to do this loop

 
Patrick Mallahan
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is my situation, I have a Java program that prints out a table and sends it to a jsp. The table prints out how many hours someone works from Date-Date. I need to do columns for each day. So it looks something like:

[Name][Company][Hours][15][16[17][18]

I aleady have the name company hours done, problem is, is that the days are dynamic. I need to be able to select from a different range of dates not always the 15-18. So how can I add a column to a table with a loop? Here is what the current code looks like:

retval = "<th>Employee</th><th>Company</th><th>Account</th><th>Billable Hours</th>"

retval is a string that is passed to the jsp to put out the column header.
 
Tad Dicks
Ranch Hand
Posts: 264
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Then do something similiar inside your main loop for getting the hours...
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, the table header is pretty easy:


Now, to output the table, you have to nest one loop inside another. In pseudocode:

 
Patrick Mallahan
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay that makes sense, i havnt had to add to a variable ever, so if you just do variable + " " it will add to any variable?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's a little bit tricky sometimes, but yes. The "+" operator can catenate Strings together. It can also catenate Strings to other things -- for example, a String and an Object. It does this by converting the other thing to a String first (for an object, by using the toString() method; for primitives like ints, by using static methods like String.valueOf(int).)

But it can't catenate (for example) two Object objects, and it adds numbers rather than catenating them. One operand of "+" must be a String for this operator to imply String catenation, and remember that Java expressions are always evaluated left-to-right.

So be careful. This prints "foo12":

System.out.println("foo" + 1 + 2);

while this prints "3foo":

System.out.println(1 + 2 + foo);

This compiles:

System.out.println("foo" + new Object() + new Object());

while this doesn't:

System.out.println(new Object() + new Object() + "foo");
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic