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");
}
}
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.
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.
use print instead of println in your printX function to be specific.
since println prints and adds break (newline)
public static void printX()
{
System.out.println("X"); // println() puts newline character.. use print()
}
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));
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.
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));
}
}
// 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);
}
int n = 20;
for(int i = 0; i <= n; i++) {
System.out.print(" " + i);
}
Related
I am new to Java and have a task: Scanner a number of "strangers' " names, then read these names and print "Hello+name" to the console. If number of strangers is zero, then print "Looks empty", if the number is negative, then print "Looks negative to me".
So the input and output to console should look like this:
3
Den
Ken
Mel
Hello, Den
Hello, Ken
Hello, Mel
So I have this code edited from someone with some related task, but it seems I miss something as I am new to Java...
Scanner input = new Scanner(System.in);
System.out.println("Enter the size of an Array");
int num = input.nextInt();
while (num==0) {
System.out.println("Oh, it looks like there is no one here");
break;
} while (num<0) {
System.out.println("Seriously? Why so negative?");
break;
}
String[] numbers = new String[num];
for (int i=0; i< numbers.length;i++) {
System.out.println("Hello, " +input.nextLine());
}
With using do while loop you can ask the number to the user again and again if it is negative number.
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
int num;
String name;
Scanner scan = new Scanner(System.in);
//The loop asks a number till the number is nonnegative
do{
System.out.print("Please enter the number of strangers: ");
num = scan.nextInt();
if(num<0) {
System.out.println("It cannot be a negative number try again.");
}else if(num==0) {
System.out.println("Oh, it looks like there is no one here");
break;
}else{
String[] strangers = new String[num];
//Takes the names and puts them to the strangers array
for(int i=0;i<num;i++) {
System.out.print("Name " + (i+1) + " : ");
name = scan.next();
strangers[i] = name;
}
//Printing the array
for(int j=0; j<num; j++) {
System.out.println("Hello, " + strangers[j]);
}
break;
}
}while(num<0);
}
}
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter the size of an Array");
int num = input.nextInt();
if(num==0) {
System.out.println("Oh, it looks like there is no one here");
}
else if(num<0) {
System.out.println("Seriously? Why so negative?");
}
else {
String numbers[] = new String[num];
input.nextLine();
for (int i=0; i< num;i++) {
numbers[i]=input.nextLine();
}
for (int i=0; i< numbers.length;i++) {
System.out.println("Hello, " +numbers[i]);
}
}
}
}
This is how your code will look and you'll need to add member function input.nextLine(); to read newline character, so there can't be problem regarding input
here is the question:
Write an application that reads five numbers between 1 and 30. For
each number that’s read, your program should display the same number of adjacent asterisks. For
example, if your program reads the number 7, it should display *******. Display the bars of asterisks
after you read all five numbers.
here is my code:
package Assignment.Q034;
import java.util.Scanner;
public class Q034_trial
{
public static void main (String[] args)
{
Scanner input = new Scanner (System.in);
int num;
num = 1-30;
for (int i = 0; i < 5; i++)// system asks for no more than 5 numbers
{
System.out.printf("Enter a number: ");
num = input.nextInt();
}
for (int j = 0; j < num; j++)
{
System.out.printf("*");
}
System.out.println();
}
}
program IDe used: Apache Netbeans IDE 12.4
the code does not sure any error but when I run and debug it, the output shows like this:
Enter a number: 1
Enter a number: 2
Enter a number: 3
Enter a number: 4
Enter a number: 5
*****
but the output I need is:
Enter a number: 1
Enter a number: 2
Enter a number: 3
Enter a number: 4
Enter a number: 5
*
**
***
****
*****
I am new to java programming. please help me t find the solution.
You can try to break them down individually and try to incorporate an approach like this or use these ideas for your project:
import java.util.Scanner;
public class Array {
public static void main(String[] args){
Array asteriskGenerator = new Array();
int nb[]=new int[5];
Scanner input = new Scanner (System.in);
for(int i=0;i<5;i++)
{
System.out.print("Please, Enter a number between 1 - 30 ");
nb[i]=input.nextInt();
}
input.close();
asteriskGenerator.asteriskGenerator(nb);
}
void asteriskGenerator(int nb[])
{
for(int i = 0; i < nb.length; i++)
{
for(int j=1;j<=nb[i];j++)
{
System.out.print("*");
}
System.out.println();
}
}
}
I hope this helps in what you are trying to accomplish!
You need to read in five integers, and then when you are done, do something with them. This means you need some way to store all five integers.
The obvious solution is to store them in an array.
public class Q034_trial
{
public static void main (String[] args)
{
Scanner input = new Scanner (System.in);
int[] nums = new int[5];
for (int i = 0; i < 5; i++)
{
System.out.printf("Enter a number: ");
int num = input.nextInt();
nums[i] = num;
}
}
}
Having done that you merely need to iterate over each number in the array to print the correct number of asterisks.
public class Q034_trial
{
public static void main (String[] args)
{
Scanner input = new Scanner (System.in);
int[] nums = new int[5];
for (int i = 0; i < 5; i++)
{
System.out.printf("Enter a number: ");
int num = input.nextInt();
nums[i] = num;
}
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < nums[i]; j++)
System.out.printf("*");
System.out.println();
}
}
}
RightTriangle.java: Write code that reads in a number R from the user, and displays a figure with R rows of "$" characters as the following pattern. For instance, if the user enters a 4 for R, your program should display:
$$$$
$$$
$$
$
Heres my code currently.
import java.util.Scanner;
public class RightTriangle {
public static void main(String[] args) {
int R;
Scanner sc = new Scanner(System.in);
System.out.println("Please enter a number: ");
R = sc.nextInt($);
System.out.println(R);
}
}
You could solve this task like so:
import java.util.Scanner;
public class triangle{
public static void main(String[] args){
int R;
Scanner sc = new Scanner(System.in);
System.out.println("Please enter a number: ");
R = sc.nextInt();
int k = R;
for(int i=0; i<R; i++){
for(int j=k; j>0; j--){
System.out.print('$');
}
k = k - 1;
System.out.print('\n');
}
}
}
We use two for loops. The first for loop is used to print a newline after the nested for loop printed the correct amount of $ for that line. Note how we decrease the value of the inner loop counter inside the outer for loop to decrease the amount of $ printed each line.
Use a descending for-loop with the input as the index.
In each iteration, print the $ symbol i times. You could do this using a loop or using another way.
EDIT:
import java.util.Scanner;
public class RightTriangle {
public static void main(String[] args) {
int R;
Scanner sc = new Scanner(System.in);
System.out.println("Please enter a number: ");
R = sc.nextInt(10);
for (int i = R; i >0; i--) {
StringBuilder sb = new StringBuilder();
for (int j = 0; j < i; j++) {
sb.append("$");
}
System.out.println(sb.toString());
}
}
}
A little late, but here it is anyway :)
Currently working on example from a book loop chapter. Basically, there are 4 car sellers and each should have input how many cars they sold. Below inputs, their names should be printed out and in same line number of cars they sold(from input), but not in numbers, using X or star *. x or star represents that number input.
Here is my code so far. I am stuck on printing that X or star next to a name. It prints out next to wrong name. Any way using loops only without array?
import java.io.*;
public class BarGraphCarSold {
public static void main(String[] args)throws IOException {
BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter no of cars Bob sold >> ");
int bobSold = Integer.parseInt(buf.readLine());
System.out.print("Enter no of cars Pam sold >> ");
int pamSold = Integer.parseInt(buf.readLine());
System.out.print("Enter no of cars John sold >> ");
int johnSold = Integer.parseInt(buf.readLine());
System.out.print("Enter no of cars Kim sold >> ");
int kimSold = Integer.parseInt(buf.readLine());
System.out.println();
System.out.println("Car Sales for Month");
System.out.print("Bob \n");
System.out.print("Pam \n");
System.out.print("John \n");
System.out.print("Kim ");
for(int i = 1; i<=bobSold;i++){
System.out.print("X");
}
}
}
OUTPUT
Enter no of cars Bob sold >> 5
Enter no of cars Pam sold >> 7
Enter no of cars John sold >> 4
Enter no of cars Kim sold >> 6
Car Sales for Month
Bob
Pam
John
Kim XXXXX
You are close.
try something like this.
System.out.print("Bob ");
for(int i = 0; i < bobSold; i++){
System.out.print("X");
}
System.out.print("\n");
You are printing out each of the names then have newline (\n) then you try to print the X's. So you will need to print their name then the X's then the new line. You will need a loop like that for each of the names you want to print out with their X's.
There are many different ways to do this. You can have a String with their name then loop through and add the X's to the string then print it all out at one time.
String dealer = "Bob ";
for(int i = 0; i < bobSold; i++){
dealer += "X";
}
dealer += "\n";
System.out.print(dealer);
With Arrays
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) {
BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
String[] dealers = {"Bob","Pam","John","Kim"};
int[] sales = new int[4];
try {
for(int i=0; i < dealers.length; i++){
System.out.print("Enter number of cars "+dealers[i]+" sold: ");
sales[i] = Integer.parseInt(buf.readLine());
}
}catch(IOException e){
System.err.println("Error Reading input");
System.err.println(e.getMessage());
}
System.out.println();
System.out.println("Car Sales for Month");
for(int i = 0; i < dealers.length; i++) {
System.out.print(dealers[i]+" ");
for(int j = 0; j < sales[i]; j++) {
if(j == (sales[i]-1)) {
System.out.println("X");
}else {
System.out.print("X");
}
}
}
}
}
Have fun. Code On.
You need to print the X's for each person.
System.out.print("Bob - ");
for(int i = 1; i<=bobSold;i++){
System.out.print("X");
}
System.out.print("\nPam - ");
for(int i = 1; i<=pamSold;i++){
System.out.print("X");
}
System.out.print("\nSame - ");
for(int i = 1; i<=samSold;i++){
System.out.print("X");
}
System.out.print("\nKim - ");
for(int i = 1; i<=kimSold;i++){
System.out.print("X");
}
Alright, so here is my dilemma. Unfortunately my Java teacher does not teach how to start the projects in class, nor does he tell us where to find the information to start the program. This is my first programming course, so I'm still trying to learn a few things.
So, lets get to the point, he wants it to look like this:
This program is written by Ben Barcomb
Enter a String: Alaska
Number of A: 3
//End of program
That is it. I just don't know where to start. Any help would be greatly appreciated!
This is what I have right now. The program ends after I enter Alaska.
import java.util.Scanner;
public class P4_BenjaminBarcomb
{
public static void main(String[] args)
{
System.out.println("This program is written by Benjamin Barcomb\n");
Scanner kb = new Scanner(System.in);
int counter = 0;
System.out.print("Enter a String: ");
String word = kb.next();
for (int i = 0; i < word.length(); i++) {
if (word.substring(i, i+1).equalsIgnoreCase("a"))
counter++;
}
}
}
If you loop through the length of the string, you can check to see whether each letter is an "A".
For example,
for (int i = 0; i < s.length(); i++) {
if (s.substring(i, i+1).equalsIgnoreCase("a")) counter++;
}
you can also do like this:
int count=0;
String s ="Alaska";
for(char ch : s.toCharArray()){
if(ch=='A' || ch=='a'){
count++;
}
}
System.out.println("Number of A: "+count);
An example is this:
import java.util.Scanner;
public class Junk {
public static void main(String[] args) {
Scanner Keyboard = new Scanner(System.in);
System.out.print("Enter a String: ");
String str = Keyboard.nextLine();
for (int i = 0; i < str.length(); i++){
if (str.substring(i, i+1).equalsIgnoreCase("a")){
count++;
}
}
System.out.println("Number of A: "+count);