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: Get value before execute printf in Java

  1. #1
    Junior Member
    Join Date
    Nov 2019
    Posts
    22
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Get value before execute printf in Java

    Hello,

    In my code, I have the below output. but I expected to execute the first printf then get two integers from the console.
    (Run in Netbeans - Widnows 10)

    import java.util.Scanner;
     
    class square
    {
        protected int height;
        protected int width;
        protected int surfaceArea;
     
        public square(){}
        public square(int h, int w)
        {
            height = h;
            width = w;
        }
     
        public void ComputeSurfaceArea()
        {
            surfaceArea =  height*width;
            System.out.printf("Square Area is: %d\t",surfaceArea);
        }
     
    }
     
    class cube extends square
    {
        private int depth;
     
        public cube(int d)
        {
            depth = d;
        }
     
        @Override
            public void ComputeSurfaceArea()
        {
            surfaceArea =  6*(depth*depth);
            System.out.printf("Cube Area is: %d\t",surfaceArea);
        }
    }
     
    public class NewClass {
            public static void main(String[] args)
            {
                int w,h;
                Scanner sc = new Scanner(System.in);
     
                System.out.printf("\nWidth & Height: ");
                w = sc.nextInt();
                h = sc.nextInt();
     
                 square mySquare = new square(w,h);
                 mySquare.ComputeSurfaceArea();
     
                 System.out.printf("\nCube: ");
                 int d = sc.nextInt();
                 cube myCube = new cube(d);
                 myCube.ComputeSurfaceArea();
            }
    }


    2
    3
    Width & Height: Square Area is: 6
    8
    Cube: Cube Area is: 384
    Last edited by shervan360; July 1st, 2021 at 10:06 AM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Get value before execute printf in Java

    Can you explain what the problem is?

    Why are you using printf instead of print or println?
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    shervan360 (July 1st, 2021)

Similar Threads

  1. Unable to execute dex: java.nio.BufferOverflowException.
    By dsclovers in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 8th, 2014, 12:22 AM
  2. Java printf
    By maple1100 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 27th, 2013, 03:20 PM
  3. Java using printf to format.
    By maple1100 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: December 31st, 2012, 08:29 PM
  4. [SOLVED] How to modify core Java interface java.sql.Statement.execute(String sql)?
    By amughost in forum Java Theory & Questions
    Replies: 6
    Last Post: June 9th, 2012, 04:31 PM
  5. how to execute multiple different queries in one execute?
    By Sakina in forum JDBC & Databases
    Replies: 1
    Last Post: June 9th, 2012, 09:40 AM