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 3 of 3

Thread: about printf please help having errors

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

    Default about printf please help having errors

    System.out.print("\nList 2 : ");
    for (i = 0 ; i<list.length ; i++){
    list2 [i] =(int)(99*Math.random()+1);
    System.out.printf("\n%2d",list2[i] + " ");
    }

    i want to display random nos. with a value of 1-99but everytime a single digit number comes up it ruins the alignment so i figured using printf but still im having errors.. what am i doing wrong? i want to cast each number into 2 places so that alignment will be no problem...


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: about printf please help having errors

    Do you have a compiler? Because there are some errors that should come up.

    First off, System.out.printf("\n%2d",list2[i] + " ");. You are sending the printf statement a string, not an int. You are doing this by saying: list2[i] + " ".

    Second, why are you incrementing the loop based on the size of list, but using list2 inside your loop instead? That is a dangerous practice because those loops might not have the same length.

    Third, if you are wanting to just make it all left-justified,
    for (int i = 0 ; i<list.length ; i++)
    		{
    			list [i] =(int)(99*Math.random()+1);
    			System.out.printf("\n%2d",list[i]);
    		}
    works.
    If you are wanting leading zeros however, this:
    for (int i = 0 ; i<list.length ; i++)
    		{
    			list [i] =(int)(99*Math.random()+1);
    			System.out.printf("\n%02d",list[i]);
    		}
    works.


    Based on your style, I can only assume you know C. If that is true, then all of these issues are Syntax Issues that are the exact same in C and you might want to review some of the basic programming syntax that is used in most languages.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Junior Member
    Join Date
    Oct 2010
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: about printf please help having errors

    oh thanks its working now...
    so all i needed to do is get rid of the +" "; after the array
    thank you and yeah thanks again for the advice ill be more careful next time with my practices..

    and nope i dont know C actually this is my 1st java course so yeah

Similar Threads

  1. Many Errors
    By Woody619 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: July 16th, 2010, 09:36 PM
  2. Getting errors
    By Nonire in forum What's Wrong With My Code?
    Replies: 7
    Last Post: July 4th, 2010, 12:21 PM
  3. Why am I getting 62 errors?
    By DestinyChick1225 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: July 1st, 2010, 04:41 AM
  4. The printf() method explanation needed
    By darek9576 in forum Object Oriented Programming
    Replies: 1
    Last Post: March 14th, 2010, 12:11 AM
  5. Ambiguity and non-static variable reference error in java
    By jenseits in forum AWT / Java Swing
    Replies: 5
    Last Post: December 8th, 2008, 07:04 PM