Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Use printf to round numbers in an array?

 
Phil Campell
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to print an array using printf to round each index in the array. Heres the code:


This should create a new array "percent" using the counts at each index from the array "counts". It works fine if I do "System.out.println(Arrays.toString(percent));" however, I don't get the percent rounded. But when I use printf, I'm doing %.1fpercent[]\n because I want to round to one decimal place after each number in each index in the array. But I get a weird error "Exception in thread "main" java.util.IllegalFormatConversionException: f != java.lang.String". Before anyone says it, I don't want to round the percent[] array and have it be the number in the array, I need all the numbers later on for other calculations. I know I can do Math.round() but like I said, I need to keep the original numbers, that's why I'm doing printf, so I can just convert it when I need to round it.

While I'm at it, I'm trying to convert a string of characters into an array. Basically, there is a string of proteins, labeled by one letter (4 proteins - a, c, g, and t). The string can look like "ATCGATTCCAACTCTTAA" What I'm trying to do is turn that string into an array: [ATC, GAT, TCC, AAC, TCT, TAA] so they are separated by three at a time with a '" , " and a space. Then I would like to print the array using "Arrays.toString();"

Sorry for being confusing. And thanks in advance!
 
Phil Campell
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I managed to figure out the first part after a little trial and error, but I'm still needing help with the second part. Here's my revised code for the first part:


Fencepost problem :/

 
Martin Vajsar
Sheriff
Pie
Posts: 3751
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Corey,

you'll probably have to split the string into parts yourself. Basically create a loop that will go over every triplet of characters in your input string and will extract the triplet into a new String using the substring method. You'll have to compute the start and end index of each triplet, but it is trivial as all the substrings will have the same length. The length of the array can be also easily computed, or you could use List<String>, which is easier to work with, once you get to know the Java Collection Framework.

If you print the array using Arrays.toString, you won't have the quotes around every substring. You'll have to print it out using a for loop, as you did with the numbers.
 
Bobby Smallman
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seems you solved your first question, your other option for solving that could be using a DecimalFormat if you have to do this same rounding in multiple places throughout your code you might find it simpler, not positive what the scope of your project is.

To your second question (which in the future you should probably make a separate thread for so when someone else comes along having the same question they will be able to find the answer easier). Something like this
should work just fine for you. Good luck to you!
 
Phil Campell
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I ended up solving it a different way. but thanks for the help@
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic