Hello everyone!
This is my first post on this site that I heard of recently. Heard good things of this site :)
My assignment asks me to create an application that performs a loop statement that would generate a report (please see attached picture)
the app starts at 1, the first column, which is defined as "int value" which is then multiplied by 10, then by 100, then by 1000.
the app then goes to column 2 and repeats
it would terminate until the statement int N >= int value is no longer valid.
the tricky part is when th user inputs 200 or something when they're asked to at the beginning of the program and the table output would look bad, meaning no spaces in between
them or all values are stuck together and you are not able to distinguish the columns anymore.
My problem is how to tie in the other things into this program and making it look nice.
I simply do not unerstand how the hints the instructor gave out would tie into this program.
My java source file is below
---------------------------------------------------START OF .JAVA FILE-------------------------------
-
import java.util.Scanner;
-
-
public class Multiples
-
{
-
-
public static void main (String[]args)//Main method
-
{
-
Scanner input = new Scanner(System.in);
-
-
int N;//This will be the number that the user will input
-
int value = 1;//intial starting point for the first column number
-
-
-
System.out.print("Please enter the value of N: ");//prompts the user to input the value of "N"
-
N = input.nextInt();//reads the number from the user and assigns it to field "valueN"
-
-
System.out.printf(" X 10 100 1000\n");//prints the headers with spaces in between each one
-
System.out.printf("------------------\n");//prints out the lines below the headers
-
-
while (N >= value)
-
{
-
-
System.out.printf("%2d", value * 1," ");
-
System.out.printf("%4d", value * 10," ");
-
System.out.printf("%5d", value * 100," ");
-
System.out.printf("%6d", value * 1000);
-
System.out.printf("\n");
-
-
value++;
-
-
}//end of while loop
-
-
}//end of method main
-
-
}//end of class Multiples
---------------------------------------------------END OF .JAVA FILE-------------------------------
THESE ARE SOME HINTS FROM THE INSTRUCTOR THAT I HAVE
/*
1. The column width for the data in the first column must then be able to hold the String:
String largestFirstValue = "" + size;
2. And the column width for the data in the second column must be able to hold the String:
String largestSecondValue = " " + 10 * size; including the separating space
(notice that we are doing String concatination to generate the Strings, just as we did in the earlier chapter examples.
3. And therefore the column width is the length of the String you just created, above, which can be determined by asking
the String its length, as follows:
int firstColumnWidth = largestFirstValue.length();
4. And the format string for the printf method could be constructed as:
String dataFormat = "%" + firstColumnWidth + "d";
5. You can use a String variable just like a String literal in a printf method, so just append the other widths to the String
in a similar manner and you will end up with a String that contains "%2d%4d%5d%6d"
(in this case) which is what you want - maybe add a newline at the end - and your ready to roll.
END HINTS OF INSTRUCTOR
*/
I would greatly appreciate your help if you are able to. I've been working on this assignment for about 6 hours now.
Thank You :D