Kylian Murphy Kylian Murphy - 1 month ago 8
Java Question

Having a method print all output at the end of inputs

What I am trying to do is basically make a program that displays a pipe chart of the population for Canadian provinces based on input.

Right now, the program that I've written only takes one province and it's population before printing the result and then does the same for every following province/territory.

I want to have it take the data and then print the entire chart at the end instead of individually as the information is printed.

I've reviewed the whole chapter and I have also checked around the Stackexchange site. Though I cannot find anything regarding this. Is it not possible?

here is my code:

import java.util.Scanner;

public class printPopChart {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Please enter the number of provinces");
int pNum = in.nextInt();
for (int i = pNum; i > 0; i--){
System.out.println("Province: ");
String province = in.next();
System.out.println("Population: ");
double pPop = in.nextDouble();
double pipes = numberOfPipes(pPop);
printBar(province, pipes);
}
}
public static double numberOfPipes(double pop) {
return Math.ceil(pop / 250000);
}

public static void printBar(String province, double pop) {
System.out.println("Province/Territory Population "); // 32 between Start and word Population
System.out.print(province);
spaceBetween(province);
for (int i = 0; i <= pop; i++) {
System.out.print("|");
}
System.out.println();
}

public static void spaceBetween(String province)
{
for (int i = 1; i <= (32 - province.length()); i++) {
System.out.print(" ");
}
}
}


I am relatively new to methods and Java in general (this is only my 5th week in the course) so I am not sure how to hold the outputs until the end so as to print just one big chart like:

Province/Territory Population
Alberta |||||||||||||||||
British Columbia |||||||||||||||||||
Manitoba ||||||


Here is what I did after using one of the answers if you're interested:

import java.util.Scanner;

public class printPopChart {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String temp = "";
System.out.println("Please enter the number of provinces");
int pNum = in.nextInt();
for (int i = pNum; i > 0; i--){
System.out.println("Province: ");
String province = in.next();
System.out.println("Population: ");
double pPop = in.nextDouble();
double pipes = numberOfPipes(pPop);
temp += printBar(province, pipes);
}
System.out.println("Province/Territory Population "); // 32 between Start and word Population
System.out.print(temp);
}
public static double numberOfPipes(double pop) {
return Math.ceil(pop / 250000);
}

public static String printBar(String province, double pop) {
String printB = "";
printB += province;
printB += spaceBetween(province);
spaceBetween(province);
for (int i = 1; i <= pop; i++) {
printB += ("|");
}
return printB + "\n";
}

public static String spaceBetween(String province)
{
String spaceB = "";
for (int i = 1; i <= (32 - province.length()); i++) {
spaceB +=" ";
}
return spaceB;
}
}

Answer

You can create a String called temp and concatenate a new String with that and print everything at the end.

For example:

String temp = "";

Then add all the new Strings to this temp String.

temp + = "Province/Territory";
temp += "\t Population \n"

\t is a special character for tab and \n is special character for a new line. If you keep doing that you will get just one String and you can print that string after you are done with everything.

You can also learn about printf which will help you to format the display properly. You can learn more about that here: How to use formatting with printf correctly in Java

Comments