'Print loop output horizontally?
Hey guys I have to write a program for class that asks me to produce a bar graph based on how many cars a salesperson sold for the month. An example is this:
Pam XXXX
Leo XXXXXX
I'm almost there but not quite. As of right now I can get one X next to the salespersons name but any others are printed underneath on separate lines.
Pam X
X
X
If you can help me get those other X's on the same line I would really appreciate it. Thank you for your input!
import java.util.Scanner;
public class BarGraph
{
public static int Pam = 0;
public static int Leo = 0;
public static int Kim = 0;
public static int Bob = 0;
public static Scanner kb = new Scanner(System.in);
public static void main(String args[])
{
System.out.println("Enter number of cars sold by Pam ");
Pam = kb.nextInt();
kb.nextLine();
System.out.println("Enter number of cars sold by Leo ");
Leo = kb.nextInt();
kb.nextLine();
System.out.println("Enter number of cars sold by Kim ");
Kim = kb.nextInt();
kb.nextLine();
System.out.println("Enter number of cars sold by Bob ");
Bob = kb.nextInt();
kb.nextLine();
System.out.print("Pam "); pamsCars();
System.out.print("Leo "); leosCars();
System.out.print("Kim "); kimsCars();
System.out.print("Bob "); bobsCars();
}
public static void leosCars()
{
for(int i=0; i < Leo; i++)
{
printX();
}
}
public static void kimsCars()
{
for(int i=0; i < Kim; i++)
{
printX();
}
}
public static void pamsCars()
{
for(int i=0; i < Pam; i++)
{
printX();
}
}
public static void bobsCars()
{
for(int i=0; i < Bob; i++)
{
printX();
}
}
public static void printX()
{
System.out.println("X");
}
}
Solution 1:[1]
You are using System.out.println("X");
This automatically appends a newline character to the end of the string.
Instead use System.out.print("X");
to print the X's next to each other.
Solution 2:[2]
couldn't you use a
System.out.printf("%s",x);
as well?
or
System.out.printf("%n%s",x);
i didn't try the loop myself but i'm pretty sure one of the two would also be an alternative... the %n will give you the same result as println as long as you go with printf and %n... its the same escape sequence as \n when you're using println.
i just learned that a month ago in class, so i figured i'd share what basic knowledge i have... mid-terms next week so i have programming fever.
Solution 3:[3]
use print
instead of println
in your printX function to be specific.
since println prints and adds break (newline)
Solution 4:[4]
public static void printX()
{
System.out.println("X"); // println() puts newline character.. use print()
}
Solution 5:[5]
Change your printX() method not to print newline and change the printCar() signature to accept name and number of X to print.
public static void printX()
{
System.out.print("X");
}
public static void printCars(String name, int num)
{
System.out.print(name);
for(int i=0; i < num; i++)
{
printX();
}
System.out.println();
}
Now you can call them like
printCars("Pam ",Pam));
printCars("Bob ",Bob));
Solution 6:[6]
make these changes:
System.out.print("Pam "); pamsCars();System.out.println("");
and
public static void printX()
{
System.out.print("X");
}
As the others have said, the "println()" method adds a "\n" to the end of the line.
Solution 7:[7]
How about the following (sorry not tested, so in principle only and more of a meansd of shjowing an alternative to loops).
Of course a flaw is that a user could input more than 10.
There again what if the user input 10,000? (would you really want to print 10,000 X's). Perhaps something to think about.
import java.util.Scanner;
public class BarGraph
{
public static int Pam = 0;
public static int Leo = 0;
public static int Kim = 0;
public static int Bob = 0;
public static Scanner kb = new Scanner(System.in);
//Assume max sales of 10
public static final String maxsales = "XXXXXXXXXX"; //+++++NEW
public static void main(String args[])
{
System.out.println("Enter number of cars sold by Pam ");
Pam = kb.nextInt();
kb.nextLine();
System.out.println("Enter number of cars sold by Leo ");
Leo = kb.nextInt();
kb.nextLine();
System.out.println("Enter number of cars sold by Kim ");
Kim = kb.nextInt();
kb.nextLine();
System.out.println("Enter number of cars sold by Bob ");
Bob = kb.nextInt();
kb.nextLine();
printCars("Pam",pam); //+++++NEW
printCars("Leo",leo); //+++++NEW
printCars("Kim",kim); //+++++NEW
printcars("Bob",bob); //+++++NEW
}
//+++++NEW All other methods/functions replaced by this one
public static void printCars(String person, int sales) {
system.out.println(person + " " + maxsales.substr(sales));
}
}
Solution 8:[8]
// Incrementing by one in Java
for (int i=0; i<10; i++) {
System.out.println(i);
}
// Incrementing by one
// Printing Horizontally
for (int i=0; i<10; i++) {
System.out.print(" " + i);
}
Solution 9:[9]
int n = 20;
for(int i = 0; i <= n; i++) {
System.out.print(" " + i);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Phil Schwartz |
Solution 2 | sneufeld |
Solution 3 | Saher Ahwal |
Solution 4 | TheLostMind |
Solution 5 | anonymous |
Solution 6 | |
Solution 7 | MikeT |
Solution 8 | Tyler2P |
Solution 9 | Cerbrus |