Testing Function Output In Java

Testing Function Output In Java
0.0 0

#1

Hi. I’ve just gone through appendix A.4 in Think Java, a book that @P1xt recommended, and I’m trying to test the output of the Convert function as the text directs.

Towards that goal, I cloned the ThinkJavaCode repo into my computer, entered the ch03 folder, and compiled the Convert file, all successfully. I then used the command line to create test.in and test.exp, as the book instructs.

I gave test.in the contents “193.04” and test.exp the contents “193.04 cm = 6 ft, 4 in”, again as instructed.

Finally, I typed the command
java Convert < test.in > test.out,
again as instructed.

At this point, I should have a test.out file that has exactly the same contents as my test.exp file. But that’s not what I’m getting. For some reason, test.out is giving me the content “Exactly how many cm? 193.04 cm = 6 ft, 4 in”.

In other words, it’s adding the prompt to the output, which is incorrect. Why is this? I would much appreciate any help that can be offered.


#2

Hello? Is anybody out able and willing to help me with this question?


#3

I don’t believe anyone get’s what you are doing. You explained your process well, but most people aren’t going to go get the book, download the source code you are speaking of, and try it out. It works better if you post the code you are dealing with so we can help. Because we can’t see the code, this part doesn’t make much sense at all:

I gave test.in the contents “193.04” and test.exp the contents “193.04 cm = 6 ft, 4 in”, again as instructed.

Another resource you might try out is Stack Overflow.


#4

You’re right, @Google-Mac. Let me start over, then, giving all the code–and thank you.

The Convert function has the following code:

import java.util.Scanner;

/**
 * Converts centimeters to feet and inches.
 */
public class Convert {

    public static void main(String[] args) {
        double cm;
        int feet, inches, remainder;
        final double CM_PER_INCH = 2.54;
        final int IN_PER_FOOT = 12;
        Scanner in = new Scanner(System.in);

        // prompt the user and get the value
        System.out.print("Exactly how many cm? ");
        cm = in.nextDouble();

        // convert and output the result
        inches = (int) (cm / CM_PER_INCH);
        feet = inches / IN_PER_FOOT;
        remainder = inches % IN_PER_FOOT;
        System.out.printf("%.2f cm = %d ft, %d in\n",
                          cm, feet, remainder);
    }

}

So this code is meant to take some number n, put it through the centimeters to imperial system conversion formula, and spit out a sentence that says “n cm = a ft, b in”.

In the case of my automated test, with test.in having a text of 193.04, test.out is expected to have the text “193.04 cm = 6 ft, 4 in”. Well, it’s got that, but it’s got more than that. To be precise, it’s got the prompt added in, for a total output of “Exactly how many cm? 193.04 cm = 6 ft, 4 in”.

Again, the automated test consists of a text file called test.in, with text as above, a text file called text.exp with text “193.04 cm = 6 ft, 4 in”, and the following command line command:

java Convert < test.in > test.out

Does anyone have any idea for the reason of this anomaly?


#5

The commands < and > redirect input and output respectively.
Your command line command reads:

run java program Convert, when you need input look into test.in, when you want to output something write it to test.out.

That’s why you get both prompt and output in your file.

Nothing to do with Java.


#6

Sorry, @jenovs, I don’t understand. It seems that you’re saying that the problem is in my command line command. Is that right? If so, how should the command line command look in order to get the proper output?


#7

I don’t know how you can swallow first line in output, but you can use println instead of print here:

System.out.print("Exactly how many cm? ");

and you should get two separate lines in your output file.


#8

Ok, sorry, I’m really confused now. In your first comment, you said that it was nothing to do with Java. Now you’re telling me to change the Java code, altering print to println. I don’t get it. Unless, when you say that you don’t know how to “swallow first line in output,” you mean that you don’t know how to make the prompt not be part of the output?


#9

Unfortunately I don’t :unamused:


#10

How about modifying your code so it accepts command line arguments? Like

if (args[0] != "--no-prompt") {
  System.out.print("Exactly how many cm? ");
}
cm = in.nextDouble();

then run it like this:

java Convert --no-prompt < test.in > test.out

I can’t remember if that’s how you work with command line args.
For some reason I can’t get my Java compiler to work, so I haven’t tested this yet.


#11

I think you have 3 possibilities

  1. You can use @kevcomedia --noo-prompt answer

  2. Get rid of System.out.print("Exactly how many cm? ");

  3. Works with file inside your program (if know how to use files, it’s quite simple and easy)

The main reason is :

  • When you are doing ‘< test.in’, your program is getting the data to shell as stdin.

  • When you are doing ‘>test.out’, your shell is getting all program stdout to a file.

Since your program is doing stdout on that line you will always get the result that you are not expecting [Exactly how many cm?] because the shell will get all the stdout and put inside the file, not only the part that you want.

I hope this helps, :slight_smile:


#12

@jenovs Ok. Thanks anyways–I appreciate your time. :smile:
@kevcomedia Great–I’m looking forward to trying that tomorrow, when I’ll have access to the work computer. (I haven’t managed to get Java to work at home yet.) Thank you.
@felipemoura I see. I appreciate your explanations, and look forward to playing with that tomorrow.

In any event, what I’m gathering from all three of you is that the code as written (which is a cut-n-paste from what the textbook gave me) should be doing this, so the text was probably just a bit unclear in implying that I would only get the shorter version in text.out. I really appreciate your time and explanations, I appreciate all you’ve explained to me, and tomorrow I’m going to play with it to make sure I understand it–then move on. Thanks again.