0

I've searched around and could not find a precedent here for this particular issue. Cited, I've looked at the following questions.

using printf for multiple formats

Formatting using printf and format

Formatting problems using printf in java

Below I'll outline the snippet of my code where I'm having problems. Please keep in mind I'm attempting to understand Efficiency outputs better and am exploring into why certain inputs cause there to be exponential, logarithmic, linear growth, etc etc. Anyways...

for (int i = 0; i < 5; i++) {
     time = System.nanoTime();
     tl.timeTrial(N);
     elapsedTime = (System.nanoTime() - time) / BILLION;
     System.out.print(N + "\t");
     System.out.printf("%4.3f\t", elapsedTime);
     if (i != 0) {
        r = elapsedTime / previousTime;
        System.out.printf("%3.2\t", r);
        k = Math.log(r);
        System.out.printf("%3.2\n", k);
     }
     else {
        System.out.print("\n");
     }  

     previousTime = elapsedTime;
     N *= 2;
  }

From here I'll point out a few things.

System.out.printf("%4.3\t", elapsedTime);

compiles and runs perfectly. However, when I get into the if statement with r and k, I'm getting a run-time error and I do not for the life of me understand why. The error is as follows:

Exception in thread "main" java.util.UnknownFormatConversionException: Conversion = '4'
at java.util.Formatter.checkText(Formatter.java:2579)
at java.util.Formatter.parse(Formatter.java:2565)
at java.util.Formatter.format(Formatter.java:2501)
at java.io.PrintStream.format(PrintStream.java:970)
at java.io.PrintStream.printf(PrintStream.java:871)
at TimingLabClient.main(TimingLabClient.java:61)

Thank you for the help. Hopefully I won't get banned for asking a question I couldn't find an answer to again.

Thank you. For those confused about my \t and \ns, it's helping with the output format, which doesn't necessarily have to be perfect.

N   Time(sec)   R   K
8   0.723   
16  5.760   7.964   2.075
32  47.098  8.176   2.101

This program is actually going to run for about 7 hours...

Community
  • 1
  • 1
Zach Thompson
  • 39
  • 2
  • 14
  • You've got no format specifier. Have you really read those links? Shouldn't you have `"%4.3f"`? And why have `\t`? What is that supposed to do? Please read a [decent tutorial](http://docs.oracle.com/javase/tutorial/java/data/numberformat.html). – Hovercraft Full Of Eels Feb 12 '15 at 17:14
  • Yes, I really read those links. I didn't notice the %4.3f on the first printf and that was legitimately my bad. This is my first time using printf in java and reading through those other links didn't give me much insight seeing as I didn't even realize that there was a format specifier in the original code. Thanks for pointing it out though that should fix it. – Zach Thompson Feb 12 '15 at 17:21
  • What confuses me is that you had it correct in one printf statement, other than that you shouldn't be using tabs. – Hovercraft Full Of Eels Feb 12 '15 at 17:22
  • That's because it wasn't my original code, it was given to me. This project is not so much about coding as it is about experimentation and data analysis. – Zach Thompson Feb 12 '15 at 17:27

1 Answers1

0

You've got lots of problems in that code:

  • Using printf without format specifiers is the main issue. For example, System.out.printf("%3.2\t", r); makes no sense. How is Java supposed to format r when you don't give it a char that directs it what format r should be. You must have a proper specifier after the % and number, here f would work meaning you've got a floating point number: "%4.3f".
  • \t and \n are not doing what you think they should be doing.
    • avoid tabs if you can with printf and instead use format widths. Tabs are unreliable separators since they don't take into account the width of the Strings near them while format widths do.
    • use %n for a new line in printf

Most important, read a decent formatting tutorial for the details.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • While you're correct that I needed to have something along the lines of %3.2f I do in fact need those \t and \n. It's helping keep my output format easier to read, Thank you though. – Zach Thompson Feb 12 '15 at 17:25
  • Actually, it's been said multiple times that I shouldn't be using tabs, perhaps that tutorial can help me understand why later but atm it's not really that important, it's doing what I need it to do now, and I simply don't have the time to read through something like that right now. – Zach Thompson Feb 12 '15 at 17:35