Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Format Problems

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Format Problems

    I am trying to make it say feet meters meters feet, but instead it just says feet meters then enters the data and then starts the meter to feet. There are only two columns and I am trying to have four.

    I have to keep both methods and I just can't figure it out I know you can do a for loop within a for loop, but I can't get that to work either.

    public static void main(String[] args)
        {
            // Assign Variables
            double meter = 0;
            double foot =  0;
            double x;
            double y;
     
            //Header for meters to foot
            System.out.println(" Feet              Meters");
     
            //for loop to call meter method and print out table
     
            for(x = 1; x <=10; x++)
            {
                meter = footToMeter(x);
                System.out.printf("%5.1f%20.3f" , x, meter);
                System.out.println();
            }
     
            //Header for foot to meters
            System.out.println("Meters               Feet");
     
            //for loop to call foot method and print out table
            for(y = 20; y <= 65; y = y+5)
            {
                foot = meterToFoot(y);
                System.out.printf("%5.1f%20.3f" , y, foot);
                System.out.println();
            }
        }
     
        //Converts from feet to meters
     
        public static double footToMeter(double foot)
        {
          return 0.305 * foot;
        }
     
        //Converts from meters to feet
        public static double meterToFoot(double meter)
        {
           return 3.281 * meter;
        }
    }


  2. #2
    Junior Member
    Join Date
    Aug 2010
    Posts
    7
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Format Problems

    public static void main(String[] args)
        {
            // Assign Variables
            double meter = 0;
            double foot =  0;
            double x;
            double y;
     
            //Header for meters to foot
            System.out.println(" Feet              Meters | Meters              Feet");
            System.out.println("----------------------------------------------------");
     
            //for loop to call meter method and print out table
           String formater = "%5.1f%20.3f |%5.1f%20.3f";
            for(x = 1; x <=10; x++)
            {
                meter = footToMeter(x);
                y= 25+5*x;
                foot = meterToFoot(y);
                System.out.printf(formater, x, meter, y, foot);
                System.out.println();
            }
     
     
        }
     
        //Converts from feet to meters
     
        public static double footToMeter(double foot)
        {
          return 0.305 * foot;
        }
     
        //Converts from meters to feet
        public static double meterToFoot(double meter)
        {
           return 3.281 * meter;
        }
    Last edited by American; October 27th, 2010 at 11:20 PM.

Similar Threads

  1. How to format?
    By maximus20895 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 15th, 2010, 02:07 PM
  2. [SOLVED] For loop-ing decimal format
    By Harj in forum Loops & Control Statements
    Replies: 10
    Last Post: October 1st, 2010, 06:47 PM
  3. [SOLVED] Help with code, Says number is NaN and i cant format it
    By NewGuy in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 4th, 2010, 08:28 PM
  4. Format some text with Java
    By vampire in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 18th, 2010, 11:30 AM
  5. help with text format codes
    By vanchan09 in forum Java Theory & Questions
    Replies: 1
    Last Post: November 15th, 2009, 04:12 PM