• 0

[Java: Arrays] Using printf


Question

I am trying to using the prinf method while printing values of an array. Below is an example of what I'm trying to obtain in formatting output:

post-67678-0-89881600-1299211348.jpg

Here is a snippet of code I'm trying to use the printf method with:

     		for (int row=0; row < payScaleTable.length; row++) {											 
						 System.out.print("Person printPerson);					
						 printPerson = printPerson + 1;

         		for (int col=0; col < payScaleTable[row].length; col++)

						System.out.print("\tayScaleTable[row][col] + "\t");	
						System.out.println();
                         }

Anytime I start adding the prinf method to that statement inside the for loop, I start getting "Array out of bounds" errors.

Link to comment
https://www.neowin.net/forum/topic/980160-java-arrays-using-printf/
Share on other sites

7 answers to this question

Recommended Posts

  • 0

The code you posted is correct. Out-of-bounds error would have nothing to do with the use of printf. Could you show how you use printf, i.e. the code that causes the out-of-bounds error?

A possible source of bugs here is the variable printPerson, which duplicates row as a loop counter. Consider eliminating printPerson and using row instead.

  • 0

Ok, I'm off to a slightly better start now but I'm still having trouble keeping everything aligned in the long run.

For example if you look at the very top of the first picture I posted (what the output should actually be), I can't seem to keep things aligned in the long run. For example here is what I have so far when trying to get that first block.

			
 System.out.println("Summerrnship Salary Information: "); 
			  System.out.print("\t\t		  while (numberOfYears >= printYears) {		

			  		System.out.printf("Year\t", printYears);
					printYears = printYears + 1;
				}

			  System.out.println("");

			   for (int row=0; row < payScaleTable.length; row++){
        			for (int col=0; col < payScaleTable[row].length; col++)

           			 payScaleTable[row][col] = 1000 + (int)(Math.random() * ((20000 - 1000) + 1));			}	 																											 

     			for (int row=0; row < payScaleTable.length; row++) {											 
						 System.out.print("Person printPerson + "\t");					
						 printPerson = printPerson + 1;

         		for (int col=0; col < payScaleTable[row].length; col++)

						System.out.printf("$%,6d\t\tayScaleTable[row][col]);	
						System.out.println();
      		}

If that code is run, over time the year and the numbers start to get slowly separated. Like this:

post-67678-0-60715600-1299354142.jpg

  • 0

It is amazing how easily I have been missing tiny little things like that which end up making a huge difference. Can I get rid of the extra space between the $ and the numbers if it is a number like $ 1,799? I know I have a 6 built into the printf method, but I just thought I would ask because it would look cleaner if for example I had $1,799 instead of $ 1,799.

  • 0

The '$' is not part of the number so it's not right-aligned like the number is. Off the top of my head, I'd say you could pre-convert the number to a string, insert the '$' at the beginning and then output the whole thing right-aligned, as a string.

See http://download.oracle.com/javase/1.5.0/docs/api/java/util/Formatter.html for all the details, maybe there's an even better solution there.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • I really miss adding folders as toolbars to the task bar. It was my major productivity loss.
    • This will just encourage China to take over Taiwan.
    • Microsoft locks Windows 11 user out, shows how easy losing data from forced encryption is by Sayan Sen Back in March earlier this year, a new redesigned Microsoft Account sign-in was released with the intention to make it "more modern, simple, and secure." Microsoft also probably hopes that the revamp will help win some hearts since many dislike the Microsoft Account (MSA) quite a bit as they are forced to use the service during Windows 11 installation. Yes, signing in to the MSA is one of the several system requirements for Windows 11, and it is also the recommended way and it clearly does not like it when users opt for a Local account instead. Microsoft often highlights the benefits of an MSA as it points out the unified access users get across devices and services like Windows, Office, OneDrive, and Xbox, which can help in synchronization of files and settings for convenience. A Microsoft Account also stores the BitLocker encryption key which is crucial thing that all users who have encryption need to store securely. Back in May this year, we covered reports of users losing their data as a consequence of BitLocker key loss, and this is a real danger for many, given that Microsoft now enables automatic BitLocker encryption on Windows 11 24H2, that most users won't even be aware of. So in the case of loss of access to a Microsoft Account, an affected user can suddenly find that they have lost all their data and there may be no way to recover it according to Microsoft's terms. Such account lock-outs can happen as a Reddit user deus03690 found out. The frustrated user claims that Microsoft apparently "randomly" locked their account when they were dealing with multiple data drives. They explain: The user has good reason to be annoyed and frustrated at this, Microsoft's own official guidance about the Account lock says: "If you tried to sign in to your account and received a message that it's been locked, it's because activity associated with your account might violate our Terms of Use." The Terms of Use for MSA explain how Microsoft deals with a closed account. It states: Thus, this shows how users can be pretty much helpless if they get locked out of MSA or lose access to it. It also shows how over-reliance on cloud services on Windows 11, something which LibreOffice recently pointed out, can lead to additional data nightmares like losing all of your data due to forced BitLocker encryption that you may not even be aware of was there in the first place. The solution? Consider keeping your important data backed up locally on internal or external HDDs and SSDs or NAS solution, as only cloud storage is probably not the best decision.
  • Recent Achievements

    • Week One Done
      rozermack875 earned a badge
      Week One Done
    • Week One Done
      oneworldtechnologies earned a badge
      Week One Done
    • Veteran
      matthiew went up a rank
      Veteran
    • Enthusiast
      Motoman26 went up a rank
      Enthusiast
    • Mentor
      M. Murcek went up a rank
      Mentor
  • Popular Contributors

    1. 1
      +primortal
      675
    2. 2
      ATLien_0
      264
    3. 3
      Michael Scrip
      184
    4. 4
      +FloatingFatMan
      177
    5. 5
      Steven P.
      140
  • Tell a friend

    Love Neowin? Tell a friend!