Override previous Console Output - java

is it possible to override the last System.out.println output, so i can for example visualize changes in a array or create a progessbar?
For example if i have this class:
class Main{
public static void main(String[] args){
for(int i = 0; i < 10; i++){
for(int j = 0; j < i; j++){
System.out.print("#");
}
System.out.println("");
}
}
}
What do i have to do to create this simple progressbar which is shown in a single line and not in 10 seperate lines?

This works on my particular console (Windows) but it's not terribly portable...
public class Test {
public static void main(String[] args) throws Exception {
for (int i = 0; i < 100; i++) {
System.out.print("#");
if (i % 20 == 0) {
System.out.print("\r \r");
}
System.out.flush();
Thread.sleep(100);
}
}
}
There's also the Console class, but that doesn't actually buy you very much as far as I can see...

Related

How do I get this board to print out like I want it to

Total noob to java and I have a homework where I need to make a custom chess game but I am even struggling with printing the gameboard which I am trying all the empty spaces to be with "X".
My current "code":
public class Chess {
public static void main(String[] args) {
String[][] board = new String[7][7];
fillBoard(board);
}
public static void fillBoard(String[][] board){
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 7; j++) {
board[i][j] = "X";
System.out.print(Arrays.deepToString(board));
}
System.out.println();
}
}
}
It prints out:
This
When I want it to print out:
Is this
Just print the value of the position directly.
public static void fillBoard(String[][] board){
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 7; j++) {
board[i][j] = "X";
System.out.print(board[i][j]);
}
System.out.println();
}
}
After reading your comments, I realized that it's not just about printing the array that you are concerned about; rather, you are concerned about how you will be able to access it later. The solution to the problem of incorrect printing has already been provided in the comments i.e. replacing System.out.print(Arrays.deepToString(board)); with System.out.print("X");. I am explaining here the other part (i.e. how to access it later) and also some additional information that will help you in designing your application.
When you pass and change an array in some method, the array will be changed for the calling method too (check this to learn more about it). It means that when board[][] will be changed in fillBoard, you will get the changed board[][] in main.
According to Single-responsibility principle, the module/method, fillBoard should do only one thing, which is filling up the board. It should not also do printing (additional thing). You should write a different method for printing.
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
String[][] board = new String[7][7];
fillBoard(board);
displayBoard(board);
}
static void fillBoard(String[][] board) {
for (String[] row : board) {
Arrays.fill(row, "X");
}
}
static void displayBoard(String[][] board) {
for (String[] row : board) {
for (String cell : row) {
System.out.print(cell);
}
System.out.println();
}
}
}
Output:
XXXXXXX
XXXXXXX
XXXXXXX
XXXXXXX
XXXXXXX
XXXXXXX
XXXXXXX
Additional note:
A 2-D array is an array of 1-D arrays and therefore there are many ways in which you can write the code given above e.g.
static void fillBoard(String[][] board) {
for (int i = 0; i < board.length; i++) {
Arrays.fill(board[i], "X");
}
}
or using enhanced for loop:
static void fillBoard(String[][] board) {
for (String[] row : board) {
Arrays.fill(row, "X");
}
}
or without using Arrays.fill:
static void fillBoard(String[][] board) {
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
board[i][j] = "X";
}
}
}
Similarly, the method, displayBoard can be written as:
static void displayBoard(String[][] board) {
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
System.out.print(board[i][j]);
}
System.out.println();
}
}
Note that I have used board.length and board[i].length instead of 7 for the number of rows and 7 for the number of columns to make sure you don't have to change it every time you change the size of the array.

Freeman Chain Code in Java

Okay, hopefully this makes more sense. I have an array hard coded with only 1s and 0s. I am trying to write a function that reads each element to see if it is a 0 or 1. If it is a 1, it will execute another function and then change that 1 to a 0 so that it is not read again. I have it printing simply as a placeholder for the other function I will be implementing later. I'm having trouble getting the findfirst1 function to check every element in the array. I have tried putting the incrementors for i and k in different places within the flow of the code but nothing I have tried gets me the correct output.
public static void main(String[] args)
{
int[][] testarray = {{1,0,0,0,0,0,0,1},{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0},{1,0,0,0,0,0,0,1}};
findfirst1(testarray);
}
public static void findfirst1(int[][] array1)
{
int value = 0;
for(int i = 0; i < 6;i++)
{
for(int k = 0; k < 7;k++)
{
value = array1[i][k];
if(value == 1)
{
System.out.println(value);
array1[i][k] = 0;
}
else
{
System.out.println(value);
}
}
}
}
Okay, so after starting completely over and writing it all from scratch I figured it out. The array.length was right all along. I had trouble wrapping my head around it because I was so focused on the idea of the "image".
Edit: I just found an error where it wouldn't print the last line for array1, so I just added an extra row of 0s and it worked.
public class ChainCodeClass {
public static void main(String[] args)
{
int[][] array1 = {{0,1,0,0,0,0,0,0},{0,1,0,0,0,1,0,0},{0,1,1,1,0,0,1,0},
{0,0,0,1,1,0,0,1},{0,0,0,0,1,0,0,1},{0,0,0,0,0,1,0,0},
{0,0,0,0,0,0,1,0}**,{0,0,0,0,0,0,0,0}**};
int[][] array2 = {{0,0,0,0,0,0,0,0},{0,1,1,1,1,0,1,0},{0,0,0,0,1,0,1,0},
{0,0,1,1,1,0,1,0},{0,0,1,0,0,0,1,0},{0,0,1,0,0,0,1,0},{0,0,0,0,0,0,1,1},
{0,0,0,0,0,0,0,0}};
System.out.print("First Image");
print(array1);
findfirst1(array1);
print(array1);
System.out.print("Second Image");
print(array2);
outline8(array2);
}
public static void findfirst1(int[][] array)
{
int value = 0;
for(int i = 0; i < array.length; i++)
{
for(int k = 0; k < array.length; k++)
{
value = array[i][k];
if(value == 1)
{
System.out.print(value + " ");
array[i][k] = 0;
}
else
{
System.out.println(value);
}
}
}
}
public static void print(int[][] array)
{
for(int i = 0; i < array.length; i++) // print function for the array using incrementors
{
System.out.print("\n");
for(int k = 0; k < array.length; k++)
{
System.out.print(array[i][k] + " ");
}
}
System.out.println();
}
}

Printing Right-Aligned Triangle by Calling Methods

Good day! Newbie in Java Programming. Would like to ask for some help on understanding the looping. The program's aim is to print a right-aligned triangle. I've created method for printing "*" and another one for " " (whitespace). I'm having trouble understanding on how can I implement the whitespace in my main method. Thank you!
Expected output:
"printTriangle(4);"
*
**
***
****
Here is my code:
public class PrintingLikeBoss {
public static void printStars(int amount) {
int i = 1;
while (i <= amount) {
System.out.print("*");
i++;
}
System.out.println("");
}
public static void printWhitespaces(int amount) {
int i = 1;
while (i <= amount) {
System.out.print(" ");
i++;
}
System.out.println("");
}
public static void printTriangle(int size) {
int i = 1;
int j = 1;
while (i >= 0) {
printStars(size);
i++;
}
}
printTriangle(4);
}
}
The printTriangle() methode will never end due to i always being greater then 0.
Also there is no main method in your code, therefore you will not be able to run it.
Now for the answer to your question:
public static void printTriangle(int size){
int i = size;
int j = 1;
while(j<=i){
printWhite(i-j);
printStar(j);
j++;
System.out.println("");
}
}
public static void printWhite(int size){
int i = size;
for(int j = 0; j<i; j++){
System.out.print(" ");
}
}
public static void printStar(int size){
int i = size;
for(int j = 0; j<i; j++){
System.out.print("*");
}
}
public static void main(String[] args){
printTriangle(4);
}
This should provide you an output like this:
*
**
***
****

Make a print method with for loop Java

The task of exercise is to make a method that will work like in Example.( We must use for loop. But if you know how to do it in another way, it will be also very interesting.) Input number can be any.
Example:
Input: 3
Output:
**1**
*121*
12321
*121*
**1**
My example:
public static void main(String[] args) {
printMatrix(5);
}
public static void printMatrix (int n) {
int d = n +(n-1);
for (int i = 0; i < n ; i++)
{
for(int j = 1; j <=d; j++){
int abs = Math.abs(j-n);
System.out.print(abs>i ? "*" : i-abs+1);
}
System.out.println("");
}
My output:
****1****
***121***
**12321**
*1234321*
123454321
I can't make the next step, to turn it upside down. Dose anybody hava an ideas?
I have solved this exercise, but I think it is not a perfect solution. Does anybody have another way to solve this task?
public static void main(String[] args) {
printMatrix(5);
}
public static void printMatrix (int n) {
int d = n +(n-1);
int k = n*2;
int g = -1;
for (int i = 0; i < d ; i++)
{
if(i<n){
g++;
}
else{k=n*2;
g--;}
for(int j = 1; j <=d; j++){
if (i < n){
int abs = Math.abs(j-n);
System.out.print(abs>i ? "*" : i-abs+1);
}
else{
k--;
int abs = Math.abs(k-n);
System.out.print(abs>g ? "*" : g-abs+1);
}
}
System.out.println("");
}
}
}
Output:
****1****
***121***
**12321**
*1234321*
123454321
*1234321*
**12321**
***121***
****1****

printing an array in multiple lines

I am taking a programming class, and things just aren't clicking for me. I have an assignment that asks to:
Write a program to assign the integer values 1 through 25 to a 25
element integer array. Then, print the array as five separate lines
each containing five elements separated by commas. The last element
on each line should be followed by a newline instead of a comma. The
output of your program should appear exactly as follows:
1,2,3,4,5
6,7,8,9,10
11,12,13,14,15
16,17,18,19,20
21,22,23,24,25
Hints:
One way to determine every 5th element is to use the modules operator (%). If you divide the subscript by 5 and the remainder is
0, it is the 5th number.
You can use System.out.print() to print a value without a newline following it. This will allow you to print multiple things on the same
line.
I have a little bit of code but I don't know where to go from here:
public class Program4
{
public static int[] array;
public static void main(String[] args);
{
int[] numbers = new int [25]
for(int i=0; i<25; i++)
array[i] = i + 1;}
public static void printArray()
{
for(int i=1; i<=25; i++);
{
System.out.print(array[i - 1]);
if (i % 5 == 0)
System.out.printIn();
}
}
I just have a mental block about programming-can anyone help point me to some helpful examples?
Try this,
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static int[] array;
public static void main(String[] args)
{
array = new int[25];
for(int i=0; i<25; i++)
array[i] = i + 1;
printArray();
}
public static void printArray()
{
int i;
for(i=1; i<=25; i++){
if (i % 5 != 0)
System.out.print(array[i-1]+",");
else
System.out.println(array[i-1]);
}
}
}
public class Foo {
public static int[] nums;
public static void main(String[] args) {
nums = new int[25];
for (int i = 0; i < nums.length; i++) {
nums[i] = i + 1;
}
printArray(nums);
}
public static void printArray(int[] myArray) {
for (int i = 0; i < myArray.length; i++) {
System.out.print(String.valueOf(myArray[i]);
if (i % 5 == 0) {
System.out.println();
} else if (i % 5 != 4){
System.out.println(", ");
}
}
}
public class Test {
public static void main(String[] args) {
int[] array = new int [25];
for(int i=0; i<25; i++) {
array[i] = i + 1;
}
for (int i=1; i<=25; i++) {
System.out.print(array[i - 1]);
if (i % 5 == 0) {
System.out.println();
} else {
System.out.print(", ");
}
}
}
}
And try to learn Java syntax first of all.
Here is an enhanced version of your code.
public class Program4
{
public static int[] array = new int[25];//instantiate the array to its default values
public static void main(String[] args)
{
//calling the methods from main
addToArray();
printArray();
}
//add the numbers to the array
public static void addToArray(){
for(int i=0; i<25; i++)
array[i] = i + 1;
}
//print the numbers from the array
public static void printArray()
{
for(int i = 1; i <= 25; i++){
if(i % 5 == 0){
System.out.print(i);
System.out.println();
}
else{
System.out.print(i + ",");
}
}
}
}

Categories