The Artima Developer Community
Sponsored Link

Java Answers Forum
how to insert symbols

2 replies on 1 page. Most recent reply: Jul 8, 2008 4:16 AM by Alok Ranjan Meher

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
saidon conteh

Posts: 8
Nickname: conteh
Registered: Jun, 2008

how to insert symbols Posted: Jul 2, 2008 4:57 AM
Reply to this message Reply
Advertisement
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;
}
}


saidon conteh

Posts: 8
Nickname: conteh
Registered: Jun, 2008

Re: how to insert symbols Posted: Jul 2, 2008 7:47 AM
Reply to this message Reply
Here is the code with comments for easy clarification.


public class LatinNumbers
{
public static void main (String[] args) {
/**
* Make table size a variable
*/
final int N = 10;
/**
* print header row
*/
System.out.print(" |");
for (int i = 1; i <= N; i++)
System.out.print(pad(i) + "|");
System.out.println();
/**
* print separator
*/
System.out.print("---+");
for (int i = 1; i <= N; i++)
System.out.print("----");
System.out.println();
/**
* print main table
*/
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();
}
} /**
* end of main class
*/

/**
* pad: add blanks to make it 3 long
*/
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;
}
}

Alok Ranjan Meher

Posts: 2
Nickname: aloka
Registered: Jul, 2008

Re: how to insert symbols Posted: Jul 8, 2008 4:16 AM
Reply to this message Reply
Try in this way

public class Latin {

public static void main(String [] str) {

final int N = 10;

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

for (int i = 0; i <= N; i++) {
System.out.print(String.format("%1$5s",i)+"|");
}
System.out.println();

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

for (int i = 1; i <= N; i++) {
System.out.print(String.format("%1$5s",pad(i)) + "|");

for (int j = 1; j <= N; j++) {
System.out.print(String.format("%1$5s",pad(i * j)) + " ");
}

System.out.println();
}

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

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;
}
}

Flat View: This topic has 2 replies on 1 page
Topic: Applet Memory Use Previous Topic   Next Topic Topic: java

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use