Solved

Length

Posted on 2003-11-19
9
249 Views
Last Modified: 2010-03-31
hey,

i'ver develop a programme using a while and for loop to diplay in a table the results of 2^n where 1<=n<64.

However i don't know how to modify the loop to limit the resuts to 10 digits. Also i wish to right align the numbers displayed in the n column.

If anyone knows how to do this, please let me know. If ye want to jazz up the presentation too fire away! thanks!

Here is my code:

/*Computes 2^n where 1<=n<64 and displays in a table*/

class Numbers
{
      public static void main(String[] args)
      {
            byte n = 1;      //declare n, the power, as a byte. initialised at 1 and will not excced 64
            double answer;      //solution of 2^n

            System.out.println("n \t2^n"); //print header of table output

            while (n<64) //iterate loop while n is less than 64
            {
                  answer = Math.pow(2,n); // set answer to 2^n
                  System.out.println(n + "\t" + answer); //print answer
                  n++; //increment n by 1
            } // end of while loop
            

            /*for (n=1; n<64; n++)
            {
                  answer = Math.pow(2,n); // set answer to 2^n
                  System.out.println(n + "\t" + answer); //print answer
                  n=n++; //increment n by 1
            }//end of for loop*/

            
      }//end of main
}//end of class



Thanks as ever

Dinny
0
Comment
Question by:dirtydinny
  • 4
  • 3
  • 2
9 Comments
 
LVL 35

Accepted Solution

by:
TimYates earned 50 total points
ID: 9779140
something like:

-----------

import java.text.* ;

class Numbers
{
  public static void main( String[] args )
  {
    byte n = 1 ; //declare n, the power, as a byte. initialised at 1 and will not excced 64
    double answer ; //solution of 2^n

    DecimalFormat df = new DecimalFormat( "0.##########E00" ) ;
    System.out.println( "n \t2^n" ) ; //print header of table output

    while( n < 64 ) //iterate loop while n is less than 64
    {
      answer = Math.pow( 2, n ) ; // set answer to 2^n
      String sAnswer = df.format( answer ) ;
      while( sAnswer.length() < 15 )
        sAnswer = " " + sAnswer ;
      System.out.println( n + "\t" + sAnswer ) ; //print answer
      n++ ; //increment n by 1
    } // end of while loop

    /*for (n=1; n<64; n++)
               {
         answer = Math.pow(2,n); // set answer to 2^n
         System.out.println(n + "\t" + answer); //print answer
         n=n++; //increment n by 1
               }//end of for loop*/

  } //end of main
} //end of class
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9779260
Hope this is sort of what you wanted...

btw, the DecimalFormat bit limits it to 10 decimal places, and the while loop pads the strings out to a max of 15 chars :-)

Tim
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9779600
You'll get better output with a BigInteger:

import java.text.* ;
import java.math.BigInteger;

class Numbers
{
      public static void main( String[] args )
      {
            BigInteger bi = new BigInteger("2");
            System.out.println( "n \t\t\t2^n" ) ; //print header of table output
            System.out.println("---------------------------");
            for(int i = 1;i < 64;i++) {
                  String current = bi.pow(i).toString();
                  while(current.length() < 19)
                        current = " " + current;
                  System.out.println(i + "\t" + current);

            }
      } //end of main
} //end of class
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9779644
you and your BigIntegers ;-)
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 86

Expert Comment

by:CEHJ
ID: 9779948
Yes, maybe i should create a subclass called 'BigPal' - i'm surprised it isn't final
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9780028
Ooooh...me too >_<
0
 
LVL 92

Expert Comment

by:objects
ID: 9782141
long answer = 0;
for (byte n=1; n<64 && (answer=(long) Math.pow(2, n))<10000000000L; n++)
{
0
 
LVL 92

Expert Comment

by:objects
ID: 9782226
This'll right align your numbers:

StringBuffer out = new StringBuffer();
out.setLength(10 - s.length());
out.append(s);

I'd also suggest not using tabs to align your table and instead add the number of spaces you require.
0
 
LVL 92

Expert Comment

by:objects
ID: 9782450
public static void main(String[] args)
{
   long answer = 0;
   for (byte n=1; n<64 && (answer=(long) Math.pow(2, n))<10000000000L; n++)
   {
      rightAlignValue(n, 2);
      rightAlignValue(answer, 15);
      System.out.println();
   }
}
      
private static void rightAlignValue(long value, int len)
{
   String s = Long.toString(value);
   StringBuffer out = new StringBuffer();
   out.setLength(len - s.length());
   out.append(s);
   System.out.print(out);
}
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
get weblogic logged in user in java 2 58
Java - Why doesn't this JFrame work 3 43
HSSFWorkbook cannot be resolved error 10 50
Java Loop 6 49
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now