Write a program in Java that converts meters, to feet without using any types of loops. What is did was create to methods, one for converting feet to meters, the other converting meters to feet. Then I used the incremental operator, and then printed out the variable each time. However the program did not format the variables to my liking how do I fix this?
I should mention I cannot use DecimalFormat, and the instructor want me to use something of "%4.3f" whatever that is.
You shouldn't need loops for converting units. All you need is numbers and multiplication. When your instructor is talking about %4.3f, they are talking about representing the decimal in a string. This is called string formatting. Here's a page from the documentation on it: https://docs.oracle.com/javase/tutorial/java/data/numberformat.html. You should definitely read through it. Let's say you wanted to represent a float variable in a string, you would do the following. float pi = 3.1415927f; String output = String.format("Pi: %f", pi); Now let's say you wanted to set the precision to 3, you would do: String output = String.format("Pi: %.3f", pi); Everything else you need to know about formatting in Java is in that documentation. But here's a working solution for your problem: https://gist.github.com/AlexeyNosych/cb2fbd38d320219a8b47
Exactly what I was looking for, thank you.
Join our real-time social learning platform and learn together with your friends!