Printing for value of ArrayList Integer - java

I'm working on a small game that essentially has piles of coins, and you must take some coins from a pile then the program prints out the resulting piles in the format:
Pile 1: ****
Pile 2: *****
Pile 3: **
I have an array list that store all these values like so:
List<Integer> coins = new ArrayList<>();
[4,5,2]
But I can't figure out how to get it to properly print the *'s.
How can I write this code to print out a * for each value in an element. IE 4 *'s if the element value is 4?
Here is my current method:
static void printGameState(){
for(int i = 0; i <= coins.size()-1; i++){
int k = i+1;
System.out.print("Pile " + k + ": ");
for(int j = 0; j <= coins.indexOf(i); j++){
System.out.print("*");
}
}
}

Instead of using this condition:
j <= coins.indexOf(i);
Use this condition:
j < coins.get(i);
Try it:
for(int i = 0; i <= coins.size()-1; i++) {
int k = i+1;
System.out.print("Pile " + k + ": ");
for(int j = 0; j < coins.get(i); j++) {
System.out.print("*");
}
System.out.println();
}
You'll get:
Pile 1: ****
Pile 2: *****
Pile 3: **

You should be using < instead of <=. Also, you should be able to use get(i) to take the value at index i.
static void printGameState(){
for(int i = 0; i < coins.size(); i++){
int k = i+1;
System.out.print("Pile " + k + ": ");
for(int j = 0; j < coins.get(i); j++){
System.out.print("*");
}
}
}
You could also make it a bit cleaner by forming another method to print * such as:
public void ast(int n){
for(int i=0; i<n; i++){
System.out.print("*");
}
}
Then the contents of printGameState loop would be
int k = i+1;
System.out.print("Pile " + k + ": ");
ast(coins.get(i));

You have to look at the values of the different stacks by accessing the array coins[i] instead of using the number of stacks as stack height:
static void printGameState(){
for(int i = 0; i < coins.size(); i++) {
// Build the coin stack
String coinStack = "";
for(int j = 0; j < coins.get(i); j++) {
coinStack += "*";
}
// And output it
System.out.println("Pile " + (i + 1) + ": " + coinStack);
}
}

Related

Fibonacci Sequence using Array in Java

I want to get the fibonacci sequence entered by the user in array. The task given to me was "Ask the user for 2 integer input which will be taken for first and second array elements of size 10 array."
Here is my code.
int limit = 10;
int[] fib = new int[limit];
fib[0] = 0;
fib[1] = 1;
for (int j = 1; j < 2; j++)
{
System.out.print("Enter number " + "[" + j + "]: ");
num[j] = reader.nextInt();
num[j] = fib[j+1] + fib[j+2];
System.out.println("");
}
System.out.print("Result: ");
for(int j = 0; j < limit; j++ )
{
System.out.print(fib[j] + " ");
System.out.print("");
}
I badly need help for this one, been searching for solution for hours and still don't get it.
I'll just make some corrections to your code and explain them:
int limit = 10;
int[] fib = new int[limit];
// fib[0] = 0;
// fib[1] = 1;
// The two lines above are wrong. Even though the real fibonacci sequence starts
// with 0 and 1, the question asks for the first two terms to come from user
// inputs. Instead, you can initialize them below:
// In your old code, you had "j = 1; j < 2; j++". However, that only loops once.
// So, have your condition to be j <= 2 instead: (I'm assuming that you want 1
// and 2 and not zero-based because it should print out "Enter number [1]:" and
// "Enter number [2]:"
for (int j = 0; j < 2; j++) // Not "j < 2"
{
System.out.print("Enter number " + "[" + j + "]: ");
fib[j] = reader.nextInt(); // not num[j] = ..., it's fib[j] = ...
// num[j] = fib[j+1] + fib[j+2];
// You don't need this ^^^
System.out.println("");
}
// Now you need to fill in the array:
for (int j = 2; j < limit; j++)
{
fib[j] = fib[j - 1] + fib[j - 2];
}
System.out.print("Result: ");
for(int j = 0; j < limit; j++)
{
System.out.print(fib[j] + " ");
System.out.print("");
}
You have a number of mistakes here. Try to answer the following things:
What is the purpose of num variable here? You are using it to take input (num[j] = reader.nextInt();), then you are using it to store the Fibonacci sequence as well (num[j] = fib[j+1] + fib[j+2];)!!!
The first loop is only running one time. Assuming that your calculations are correct (which is not in this case!), it will have values for the first fibonacci number only.
Finally when you are printing the Fibonacci numbers, you are not using the num variable! Why?
Anyway, here is a solution to your problem. But, it will not help you unless you understand the logic behind it and you know which line is doing what!!!
int limit = 10;
int[] fib = new int[limit];
for (int j = 0; j < 2; j++)
{
System.out.print("Enter number " + "[" + j + 1 + "]: ");
fib[j] = reader.nextInt(); // These are the first two fibonacci numbers provided by the user
System.out.println("");
}
System.out.print("Result: ");
for(int j = 0; j < limit; j++ )
{
if( j > 1 ) // You only calculate from the third Fibonacci, as the first two were given by user
{
fib[j] = fib[j-1] + fib[j-2];
}
System.out.print(fib[j] + " ");
System.out.print("");
}

replace every second line of my triangle with a blank line for loops

instead of getting a full triangle with 5 lines of "*" how do I replace every second line with a blank line, e.g. to look like this
5
555
55555
This is my code:
for(int i = 0; i < 5; i++) {
for(int j = i; j < 5; j++) {
System.out.print(" ");
}
for(int k = 0; k <= (i*2); k++) {
System.out.print("5");
}
System.out.println();
}
you need small modification in your code and you can get what you want.
for(int i = 0; i < 5; i++) {
for(int j = i; j < 5; j++) {
System.out.print(" ");
}
for(int k = 0; k <= (i*2); k++) {
if(i%2==0)
System.out.print("*");
}
System.out.println();
}
check it out.
Just test for i % 2
for(int i = 0; i < 5; i++) {
for(int j = i; j < 5; j++) {
System.out.print(" ");
}
for(int k = 0; k <= (i*2); k++) {
System.out.print(i % 2 == 0 ? "*" : " ");
}
System.out.println();
}
edit
If you want to print 5 instead or * then modify the code to use 5
Check this:
int num= 5; // 'num' denotes the number of lines
int numOfSpaces;
for(int i = 1; i <= num; i++) {
if( i%2 != 0) // checks whether the line number is odd. If odd, prints '*'
{
numOfSpaces = (num-i) / 2; // find the number of blank spaces
for(int j = 1; j <= numOfSpaces; j++) {
System.out.print(" ");
}
for(int k = 1; k <= i; k++) { // number of '*'s to be printed = current line number = i
System.out.print("*");
}
System.out.println();
}
else{
System.out.println(); // leaves every second line blank (ie.,if line number is even.)
}
}
//try this code. It may help you to achieve your output
[Check screen shot for the output of the below code][1]
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5 - i; j++)
{
System.out.print(" ");
}
for (int k = 0; k <= i; k++)
{
if(i%2!=0)
{
System.out.print(" ");
}
else{
System.out.print("* ");
}
}
System.out.println();
}
The Code below is Working now.
A simple if condition will solve your problem. Just check if the
value of i is divisible by 2. If it is true then print a
blank line. Else Print * and you are done!
* will be printed on 1st, 3rd and 5th lines.
Blank Space will be printed on 2nd and 4th lines.
public class PrintPattern{
public static void main(String []args){
int lineNo=0; // New Variable for Line Number (Can Use variable 'i' also)
for(int i = 0; i < 5; i++) {
lineNo++;
for(int j = i; j < 5; j++) {
System.out.print(" ");
}
if(lineNo%2!=0){
for(int k = 0; k <= (i*2); k++)
System.out.print('*'); // Will print '*' (or '5') Sequence for Odd Lines - 1st, 3rd, 5th Lines
System.out.println();
}
else
System.out.println();// Will print Blank Line for Even Lines - 2nd and 4th Lines
}
}
}
Try this
for(int i = 0; i < 5; i++) {
for(int j = i; j < 5; j++) {
System.out.print(" ");
}
for(int k = 0; k <= (i*2); k++) {
System.out.print("5"); //Use 5 instaed of * to draw
}
System.out.println();
System.out.println(); //This one will solve your problem
}
Output will be:
5
555
55555
5555555
555555555
I think that you can just enter one more print statement inside the loop:
for(int i = 0; i < 5; i++) {
System.out.println();
for(int j = i; j < 5; j++) {
System.out.print(" ");
}
for(int k = 0; k <= (i*2); k++) {
System.out.print("*");
}
System.out.println();
}
Output
*
***
*****
*******
*********

How do I make my program show the reverse order of the integers that was entered by the user?

import java.util.Scanner;
public class Assign58{
public static void main(String[] args){
Scanner keyboard = new Scanner(System.in);
int numintegers, j;
System.out.print("Enter a positive integer: ");
numintegers = keyboard.nextInt();
System.out.print("Now enter " + numintegers + " integers: ");
int[] integers = new int[numintegers];
for (int i=0; i < integers.length; i++){
integers[i] = keyboard.nextInt();
int[] reverse = new int[integers.length];
for (i = 0, j = reverse.length - 1; i < reverse.length; i++, j--){
reverse[j] = integers[i];
}
}
System.out.print("In reverse: ");
for (int i=0; i < integers.length; i++)
System.out.print(integers[i] + " ");
}
}
So if the integers were 3 4 6 2 9, how would I change the following from the code above:
for (int i=0; i < integers.length; i++)
System.out.print(integers[i] + " ");
to make the output come out as:
9 - 2 - 6 - 4 - 3
REMEMBER I need it to come out in reverse order and I want those dashes in between those numbers and there should be no dash after the last number.
I hope someone can help me! Thanks!
Use this code for printing the array in reverse order.
int j = integers.length; //integers is the array in which the values are stored.
for(int k=j-1; k>=0; k--)
System.out.print(integers[k]+"-");
reverse the forloop to get the reverse order
for (int i=integers.length-1; i>=0 i--)
System.out.print(integers[i] + "-");
for (int i = integers.length; i > 0; i--) {
System.out.print(integers[i] + " - ");
}
System.out.print(integers[0]);
This should work
for (int i=0; i < integers.length; i++){
integers[i] = keyboard.nextInt();
int[] reverse = new int[integers.length];
for (i = 0, j = reverse.length - 1; i < reverse.length; i++, j--){
reverse[i] = integers[j]+" ";
}
}
System.out.print(reverse[0]);
for (int i=1; i < integers.length; i++)
System.out.print(" - " + reverse[i]);
the other proposed solutions would print one - too much.
Either you could traverse the Array from the end
for(int i = integers.length-1; i >= 0; i--) {
System.out.print(integers[i] + " ");
}
or you could use a Stack<Integer> to push all entered numbers and pop them in the reverse order.
Stack<Integer> integers = new Stack<Integer>();
for (int i=0; i < numIntegers; i++){
integers.push(keyboard.nextInt());
}
System.out.print("In reverse: ");
while(!integers.isEmpty()) {
System.out.print(integers.pop() + " ");
}

How to print 1 to 10 with following format first line 1 2nd line 2 & 3 3rd line 4,5 & 6 4th line 7,8,9,10?

I tried like this,
for (int j = 1; j <= 4; j++) {
for (int k = 1; k < 10; k++) {
if (k <= j) {
System.out.print(k + " ");
} else
break;
}
System.out.println();
}
but it prints like
1
1 2
1 2 3
1 2 3 4
This question is asked in one interview i had attended, my mind is breaking to find solution.. i can't think..
init a variable before
int printed = 1;
and change your
System.out.print(k+" ");
with
System.out.print(printed+" ");
printed++;
This will do it :
public static void main(String[] args)
{
int i, j, k;
for (i = 1, j = 1; i < 11; j++)
{
for (k = 0; k < j; k++)
{
System.out.print(i++ + " ");
}
System.out.println();
}
}
This code will do that:
int i, j, k;
for (i = 1, j = 1; i < 11; j++) {
for (k = 0; k < j; k++)
System.out.print(i++ + " ");
System.out.print("\n");
}
Basically, i controls the next number to print, and j controls how many numbers you will print in the current line. k is just a counter for j.
You can also try this with some mathematic magic (sequences).
for (int j = 1; j <= 4; j++) {
for (int k=(int) (0.5*(Math.pow(j,2)-j+2));k<=(int) (0.5*j*(j+1));k++) {
System.out.print(k + " ");
}
System.out.println();
}

How to add each element into a queue

How do I add each element into an arrayqueue? Basically, if I have an array of queues where each index is an arrayqueue that holds the 1s, 10s, 100s, etc. place of a corresponding 6 digit number in another index of array a. For example, if a[1] is 123456 then how can I make the code below hold arr[1] 654321? I've posted a question similar to this before but I'm just trying to get this right.
public static void radixSort(int[] a) {
//Create an array of 10 empty array queues
Queue[] arr = new Queue[a.length];
for (int i = 0; i < arr.length; i++)
arr[i] = new ArrayQueue();
for (int place = 1; place <= 100000; place *= 10) {
for (int i = 0; i < a.length; i++) {
arr[i].add(selectDigit(a[i],place));
// System.out.println("i: " + i + " a[i]: " + a[i] + " place: " + place + " digit: " + selectDigit(a[i],place));
}
}
// for (int i = 0; i < arr.length; i++)
// System.out.print(arr[i].remove()+ " ");
//for (int j = 0; j < arr.length; j++)
// a[j] = (Integer) arr[j].remove();
}
This tutorial might help:
http://www.sourcecodesworld.com/articles/java/java-data-structures/Radix_sort.asp
It seems a good walk through.

Categories

Resources