wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes how to insert symbols Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to insert symbols" Watch "how to insert symbols" New topic
Author

how to insert symbols

saidon conteh
Greenhorn

Joined: Jun 30, 2008
Posts: 10
How could I insert hyphens (-) to separate the rows and pipe symbols (|) to separate the columns in this code? I have tried a little bit but im only able to insert for the first row only.

public class Latin
{
public static void main (String[] args) {
final int N = 10;
for (int i = 1; i <= N; i++)
System.out.print(pad(i) + "|");
System.out.println();
System.out.print("---+");

for (int i = 1; i <= N; i++)
System.out.print("----");
System.out.println();

for (int i = 1; i <= N; i++) {
System.out.print(pad(i) + "|");

for (int j = 1; j <= N; j++) {
System.out.print(pad(i*j) + " ");
}
System.out.println();
}
}

public static String pad(int x) {
String s = new String();
if (x < 10) s = " " + x;
else if (x < 100) s = " " + x;
else s = "" + x;
return s;
}
}
Bill Cruise
Ranch Hand

Joined: Jun 01, 2007
Posts: 148
If you want a row separator printed after each row you have to insert the code to print one in the loop that prints the rows. It would help a lot to comment your code so it's clear what each group of statements is doing.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38480
    
  23
Welcome to JavaRanch

Please use the code button (below the message box) when quoting code; it preserves indentation and makes it easier to read.

You could use the printf method of System.out. More details here, and look at the "format string syntax" link too. Try something like "%3d|" as the format string for printing a decimal whole number with a |.

Does print("----") work? It appears correct just after a 1-second look.
saidon conteh
Greenhorn

Joined: Jun 30, 2008
Posts: 10
My bad. Here's the code with comments

[edit]Add code tags. CR[/edit]
[ July 02, 2008: Message edited by: Campbell Ritchie ]
Bill Cruise
Ranch Hand

Joined: Jun 01, 2007
Posts: 148
Okay, now it's pretty clear that you want to insert some code in the section you have marked /** print main table */ in order to get separator characters in between every row and every column. Try the row separator first, since it should be pretty similar to the /** print separator */ below the column headings.

Also, please take Campbell's advice and enclose your code in tags so it's easier for us to read. Just highlight your code and hit the CODE button below.
Sachin Adat
Ranch Hand

Joined: Sep 03, 2007
Posts: 213
Hi All,

The Solution :



Any Suggestions for Improvements..........

Special thanks to Campbell...
Never knew till now about printf in java.......
Also didn't do much of programming in C.....

Enjoyed doing this ...........

[edit]removed some whitespace. CR[/edit]
[ July 03, 2008: Message edited by: Campbell Ritchie ]

SCJP 6
How To Ask Questions On Java Ranch - How To Answer Questions On Java Ranch
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38480
    
  23
I presume it is now working
The only thing I would suggest is use intArray.length in both for loops instead of creating the variable N.

I have shortened some of your lines so they would fit on screen more easily.

Are you using NotePad? Don't. It's not at all good for programming. Download NotePad++ or NotePad2, which have all sorts of nice features like bracket highlighting (so you can find errors caused by ()[] or {} not being properly paired off). Set its options to automatic indentation, replace tab by space and tab = 4 spaces. Those are the usual settings for Java programming (but some people use different settings).
Sachin Adat
Ranch Hand

Joined: Sep 03, 2007
Posts: 213
No,
I am not using notepad, Infact I am using eclipse.
Infact I removed many empty lines before and after the for loops(sun coding conventions) to put it in one screen vertically.

But I had tabbed for the comments, which displayed on my editor in one screen but went further right on the post.....

I think I get it now that the tab = more than 4spaces for the posts.
Will keep it in mind the next time.......

Thanks again.....
[ July 03, 2008: Message edited by: Sachin Adat ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38480
    
  23
There are options you can use on Eclipse to set the tabs, but I thought the default value was 4 anyway. Somebody has put strange configuration files on your Eclipse and added all that Whitespace!
saidon conteh
Greenhorn

Joined: Jun 30, 2008
Posts: 10
Hi all. Sachin, it worked fine. thanks alot
 
Don't get me started about those stupid light bulbs.
 
subject: how to insert symbols