Java ArrayList Number Classifier not spacing correctly - java

Ok, so I recently tried to create a java program in eclipse that basically takes an infinite amount of numbers that are 1-100, and stores them in an arraylist. Once this is done, it is supposed to print them out in a horizontal bar graph.
import java.util.ArrayList;
import java.util.Scanner;
/*
* 1-10
* 11-20
* 21-30
* 31-40
* 41-50
* 51-60
* 61-70
* 71-80
* 81-90
* 91-100
*
* Created by Peter browning, PBdeveloping, 2015
*/
public class Asterisks {
public static ArrayList<Integer> array = new ArrayList<Integer>();
public static Scanner reader = new Scanner(System.in);
public static void main(String[] args)
{
System.out.println("Enter some numbers, 1-100, 0 to stop.");
int input = reader.nextInt();
while (input != 0)
{
if ((input >= 1) && (input <= 100))
{
array.add(input);
input = reader.nextInt();
}
else
{
System.out.println("Error: Number must be 1-100");
input = reader.nextInt();
}
}
for (int i = 1; i <= 10; i++)
{
int firstNumber = ((i - 1) * 10) + 1;
int secondNumber = (i * 10);
int count = 0;
System.out.println(firstNumber + " - " + secondNumber + " |" );
for (int nInsideArray : array)
{
if ((nInsideArray >= firstNumber) && (nInsideArray <= secondNumber))
{
count++;
}
}
for (int x = 0; x < count; x++)
System.out.print("*");
System.out.println();
}
}
}
So the problem that I am having is, whenever I keep on trying to run this program, (lets use the user input "1", "21", then "0" as an example, it should print out:
1-10 | *
11-20 |
21-30 | *
31-40 |
41-50 |
51-60 |
61-70 |
71-80 |
81-90 |
91-100 |
but instead it prints out....
1 - 10 |
*
11 - 20 |
21 - 30 |
*
31 - 40 |
41 - 50 |
51 - 60 |
61 - 70 |
71 - 80 |
81 - 90 |
91 - 100 |
Note: the asterisks are not beside the |'s for some reason, any help would be appreciated. Also if you have a more efficient way to code something like this, I would love to hear your input. I usually learn alot on this website, so please help me with my issue, thanks!

This is because you are printing the pipe with a println(). Change that to print(). When you are done printing asterisks do a println for a new line.

Related

can you tell me a little change in my own written pascal logic

I have just take the format and spacing but can any one tell me a single change to get the values which print in actual pascal triangle program in java...
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package patterns;
/**
*
* #author Love Poet
*/
public class p11 {
public static void main(String args[]) {
int row, col, count = 0;
for (row = 1; row <= 5; row++) {
for (col = 1; col <= 9; col++) {
if (((row + col) % 2 == 0) && (row + col >= 6)) {
System.out.print("* ");
count++;
} else if (count == row) {
break;
} else {
System.out.print(" ");
}
}
count = 0;
System.out.println();
}
}
}
My Output:
*
* *
* * *
* * * *
* * * * *
What I want:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
I think you followed the second codes. Just follow the first codes from that site:
public class PascalTriangle {
public static void main(String[] args) {
int rows = 10;
for (int i = 0; i < rows; i++) {
int number = 1;
System.out.format("%" + (rows - i) * 2 + "s", "");
for (int j = 0; j <= i; j++) {
System.out.format("%4d", number);
number = number * (i - j) / (j + 1);
}
System.out.println();
}
}
}
Output:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1

Java cant seem to get one side of a table to print correctly

I'm very new to Java and am having a really tough time trying to get certain codes to work properly. Recently I've been having trouble with a table I have to design in the console. It's a conversion of kilos to pounds AND pounds to kilos. my problem is that for some reason when I go to print the table, it will run through and print 1,1,1,1,1,1,1,1,1 on the kilos to pounds side whereas on the other side it operates perfectly fine. My guess is that it was from running two loops at the same time, any advice helps, this has been bothering me for quite some time..... I'll list the code below:
import java.text.DecimalFormat;
public class mian55 {
public static void main(String[] args) {
System.out.println("Kilograms Pounds" + " | " + "Pounds Kilolograms");
for(int kilogram = 1; kilogram<200; kilogram = kilogram+2){
DecimalFormat decimal = new DecimalFormat("#.0");
for(int poundage = 20; poundage<516; poundage= poundage+5){
DecimalFormat decim = new DecimalFormat("#.0");
System.out.println(kilogram + " " + decimal.format(kilogram * 2.2) +" | " + (poundage + " " + decim.format(poundage / 2.2)));
}
}
}
}
The problem is that your kilogram variable from the outer loop will not change for every cycle of the inner loop, causing your first and second columns to be duplicated for as many times as the inner loop runs. Try incrementing both variables on each loop, like so (variables shortened for brevity):
import java.text.DecimalFormat;
public class mian55 {
public static void main(String[] args) {
System.out.println(" Kilograms Pounds | Pounds Kilograms");
int kgs = 1;
int lbs = 20;
for (; kgs < 200 && lbs < 516; kgs += 2, lbs += 5){
DecimalFormat decimal = new DecimalFormat("#.0");
System.out.println(String.format(" %7d %7s | %7d %7s",
kgs, decimal.format(kgs * 2.2), lbs, decimal.format(lbs / 2.2)));
}
}
}
Here's one way to create a conversion table.
Here's the results from one program.
Kilograms Pounds | Pounds Kilolograms
10 22.00 | 10 4.55
20 44.00 | 20 9.09
30 66.00 | 30 13.64
40 88.00 | 40 18.18
50 110.00 | 50 22.73
60 132.00 | 60 27.27
70 154.00 | 70 31.82
80 176.00 | 80 36.36
90 198.00 | 90 40.91
100 220.00 | 100 45.45
110 242.00 | 110 50.00
120 264.00 | 120 54.55
130 286.00 | 130 59.09
140 308.00 | 140 63.64
150 330.00 | 150 68.18
160 352.00 | 160 72.73
170 374.00 | 170 77.27
180 396.00 | 180 81.82
190 418.00 | 190 86.36
200 440.00 | 200 90.91
I used the same values for the left hand side of the tables. This allowed me to use one for loop.
Java class names start with a capital letter.
I used the format method of String, rather than DecimalFormat, so I could adjust the spacing to match the header.
Here's the code.
package com.ggl.testing;
public class Conversions {
public static void main(String[] args) {
System.out.println("Kilograms Pounds" + " | " + "Pounds Kilolograms");
for (int value = 10; value <= 200; value += 10) {
float pounds = (float) value * 2.2F;
float kilograms = (float) value / 2.2F;
String v = String.format("%9d", value);
String p = String.format("%10s", String.format("%5.2f", pounds));
String k = String.format("%14s", String.format("%5.2f", kilograms));
System.out.println(v + p + " | " + v + k);
}
}
}

Gambler's Ruin ASCII Plot in Java

| * |
| * |
| * |
| * |
| * |
| * |
| * |
| * |
| * |
| * |
| * |
| * |
|* |
I need to print something like the above ASCII plot for a Gambler's Ruin problem. Where the stake & goal are taken as args. The left most | represents 0 dollars, the right most | represents the goal and the * represents the cash on hand. My program is below:
public class RuinPath {
public static void main(String[] args) {
// TODO - Your solution
int stake = Integer.parseInt(args[0]); // gambler's stating bankroll
int goal = Integer.parseInt(args[1]); // gambler's desired bankroll
{
int i = 0;
if (i == 0) {
System.out.print("|");
i++;
while (i < stake) {
System.out.print(" ");
i++;
if (i == stake) {
System.out.print("*");
i++;
while (i > stake && i < goal) {
System.out.print(" ");
i++;
if (i == goal) {
System.out.print("|");
i = 0;
System.out.println();
{
if (Math.random() < 0.5) {
stake++; // win $1
} else {
stake--; // lose $1
}
if (stake == 1 || stake == goal - 1);
break;
}
}
}
}
}
}
}
}
}
what this program prints though is:
| * |
* |
* |
* |
* |
* |
* |
* |
* |
* |
* |
* |
* |
* |
*
Why does my program not loop so that i can get the left most | to appear to represent 0 dollars all the way through? I have it so i = 0; at the end of the loop so that when it goes back around it should re-loop until the stake is 1 or less than the goal. Instead it re-loops from the middle of the program.
Your logic is a little too complicated. Also, your indentation will make any sort of debugging extremely difficult.
Just take it one step at a time:
public class RuinPath {
public static void main(String[] args) {
int stake = Integer.parseInt(args[0]); // Starting bankroll
int goal = Integer.parseInt(args[1]); // Desired bankroll
while (stake > 0 && stake < goal) {
System.out.print("|");
// Print out the spaces. Using a for loop and
// printing a "*" if the counter variable
// is equal to stake is good way to do it.
// Flip a coin and increment/decrement the stake
System.out.print("|\n");
}
}
}
Here is a broken out solution that might be easier to reason about:
import java.io.PrintStream;
public class SO {
private static int gambleWithCaution(int stake) {
if (Math.random() < 0.5) {
return stake+1; // win $1
} else {
return stake-1; // lose $1
}
}
private static void renderStanding(int stake, int goal) {
System.out.print('|');
for(int dollar = 1; dollar< goal; dollar++) {
if(dollar == stake) {
System.out.print('*');
} else {
System.out.print(' ');
}
}
System.out.println('|');
}
public static void main(String ... args) {
int stake = Integer.parseInt(args[0]); // gambler's stating bankroll
int goal = Integer.parseInt(args[1]); // gambler's desired bankroll
while(stake > 0 && stake < goal) {
renderStanding(stake, goal);
stake = gambleWithCaution(stake);
}
System.out.println((stake > goal) ? "You Won!" : "You Lost");
}
}
With the values 3 and 5 you get this output:
| * |
| * |
|* |
| * |
| * |
| *|
| * |
| *|
You Won!
Now that this is seperated out you can have some fun with it like creating a gamble function like this:
private static int gambleWithReclessAbandon(int stake, int goal, double abandon) {
int onTheLine = (int)(Math.random() * (int)(stake * abandon));
if(stake < (0.5)*goal) {
//If you are at less than 50% of your goal then just put it all on the line
//and let it ride :)
onTheLine = stake;
}
if (Math.random() < 0.5) {
return stake+onTheLine; // win $
} else {
return stake-onTheLine; // lose $
}
}
Which can be invoked like this:
//Gamble up to 80% of your current stake
stake = gambleWithReclessAbandon(stake, goal, 0.80);
With the same two values this is what I saw on my first pass (I was sooo close :)):
| * |
| * |
| *|
| * |
| *|
You Lost

Aligning columns of numbers in a loop

I am trying to replicate a list of conversions from kg to lbs and vice versa. I've found my desired code for output and functionality, but I am missing something to align my values to the right of the column.
Here is my code:
import java.text.*;
public class KilosTwoColumn {
public static void main(String[] args) {
System.out.println("Kilograms" + "\t" + "Pounds" + "\t" + " | " + "\t" + "Pounds" + "\t" + "Kilograms");
int count = 0;
while (count < 100) {
int kilos = count * 2 + 1;
int pounds2 = (count + 4) * 5;
double pounds = kilos * 2.2;
double kilos2 = pounds2 * .453;
DecimalFormat df = new DecimalFormat("#.#");
//if (count > 1 && count < 98) {
//System.out.println("...");
//break;
//}
System.out.printf("%-17d %.1f | %7d %.2f%n", kilos, pounds, pounds2, kilos2);
count++;
}
}
}
I am also trying to create a break in the list three rows in and resume the last two.
The problem is that you are not specifying a width for the float values, just the number of decimal places.....
For example, consider "%-17d %.1f ..." which will set the second value to be a float value with 1 decimal place, but no indication of how much space to occupy. By changing that to "%-17d %12.1f it will occupy 12 characters, with 1 decimal.
Try something like:
public class KilosTwoColumn {
public static void main(String[] args) {
System.out.printf("%12s %12s | %7s %12s\n", "Kilograms", "Pounds", "Pounds", "Kilograms");
int count = 0;
while (count < 100) {
int kilos = count * 2 + 1;
int pounds2 = (count + 4) * 5;
double pounds = kilos * 2.2;
double kilos2 = pounds2 * .453;
DecimalFormat df = new DecimalFormat("#.#");
//if (count > 1 && count < 98) {
//System.out.println("...");
//break;
//}
System.out.printf("%12d %12.1f | %7d %12.1f\n", kilos, pounds, pounds2, kilos2);
count++;
}
}
}
For me, the above process outputs:
Kilograms Pounds | Pounds Kilograms
1 2.2 | 20 9.1
3 6.6 | 25 11.3
....
199 437.8 | 515 233.3

For loop output when scaling figure in java

I have been unable to get my for loop to run right number of times with my "figure" when scaling.
The LINES constant here is the scaling "number".
The problem i am facing lies here i think:
for(int k = 0; k < LINES; k++){
System.out.print("*******");
}
It is supposed to make a line of * at the bottom.
This is my whole code which produces a stairs figure of some kind
public class PP5 {
public static int j;
public static final int LINES = 5;
public static void main(String[] args) {
for(j = 0 ; j < LINES; j++){
fSpaces();
System.out.print(" o *******");
bSpaces();
System.out.println("*");
fSpaces();
System.out.print(" /|\\ *");
bbSpaces();
System.out.println("*");
fSpaces();
System.out.print(" / \\ *");
bbSpaces();
System.out.println("*");
}
for(int k = 0; k < LINES; k++){
System.out.print("*******");
}
}
public static void fSpaces(){
for(int i = (LINES-1); i > j; i--){
System.out.print(" ");
}
}
public static void bSpaces(){
for(int i = 0; i < j; i++){
System.out.print(" ");
}
}
public static void bbSpaces(){
for(int i = 0; i < j+1; i++){
System.out.print(" ");
}
}
}
Any optimizations is highly appreciated.
Thanks
you require 38 stars and you are printing 35
38=(6(Every increment) * 6 (No of times) )+2 (first increment is of 8[6+2])
No of times =6 Because indexing starts from 0 (0,1,2,3,4,5) so in actual counting is 6
so use
for(int k = 0; k <(LINES+1)*6; k++){
System.out.print("*");
}
System.out.print("**");// last star
Output:
o ********
/|\ * *
/ \ * *
o ******* *
/|\ * *
/ \ * *
o ******* *
/|\ * *
/ \ * *
o ******* *
/|\ * *
/ \ * *
o ******* *
/|\ * *
/ \ * *
**************************************
To get effect similar to this
o ********
/|\ * |*
/ \ * |*
o ******* |*
/|\ * | |*
/ \ * | |*
o ******* | |*
/|\ * | | |*
/ \ * | | |*
o ******* | | |*
/|\ * | | | |*
/ \ * | | | |*
o ******* | | | |*
/|\ * | | | | |*
/ \ * | | | | |*
**************************************
|_____|_____|_____|_____|_____|_____|_
you need to notice that each of this part |_____
has six characters so you will need to use six * and print them LINES + 1 times since there are LINES + 1 |_____ parts.
This will generate
************************************|_
from
|_____|_____|_____|_____|_____|_____|_
so you will need to add last two * manually so change your last loop to
for (int k = 0; k <= LINES; k++) {
System.out.print("******");//reduce star numbers by one
}
System.out.print("**");//and add this line
In your implementation just replace
for(int k = 0; k < LINES; k++){
System.out.print("*******");
}
with
for(int k = 0; k < STEPS+1; k++){
System.out.print("******");
}
System.out.print("**");
The motivation is that each step you add 7 * with one overlapping. This means that you need to add 6 * not 7. You add 1 time more to match the top part (but 2 * are missing: 1 because the top line is made of 7* and one for the column).

Categories

Resources