Java make rows and columns - java

Building a rectangular with character input and specified column and rows. whitespace in the middle.
using standard input for string s, int r, int c
private static void printstuff(String s, int r, int c) {
colums(s, c);
rows(s, c, r);
colums(s, c);
}
// straight columns
private static void colums(String cs, int cc) {
for (int i = 1; i <= cc; i++) {
System.out.print(cs);
}
}
this creates desired whitespace or "" to concat string with ie making
x""""""""x
private static String whitespace(int wc) {
String ws = " ";
for (int i = 1; i <= wc - 3; i++) {
ws += " ";
}
return ws;
}
whitespace to built a rectangular.
// downwards building
private static void rows(String rs, int rc, int rr) {
String ws = whitespace(rc);
for (int i = 1; i <= rr - 1; i++) {
System.out.println(rs + ws + rs);
// put strings together
}
}
}
whitespace and character rows to built a rectangular. needless to say it failed.
sample output:
XXXX X
X X
xxxx
desired output:
xxxx
x x
xxxx

one quick solution below.. Cheers
public class Main {
public static void main(String[] args) {
String s = "X";
int totalColumns = 4;
int totalRow = 3;
colums(s, totalColumns);
rows(s, totalColumns, totalRow);
colums(s, totalColumns);
}
private static void colums(String cs, int cc) {
for (int i = 0; i < cc; i++) {
System.out.print(cs);
}
}
private static String whitespace(int tc) {
String ws = " ";
for (int i = 1; i < tc - 2; i++) {
ws += " ";
}
return ws;
}
private static void rows(String rs, int tc, int tr) {
System.out.println();
for (int i = 0; i < tr - 2 ; i++) {
System.out.println(rs + whitespace(tc) + rs);
}
}
}

Im not sure if this what you want but throw a System.out.println(""); after the for loop in colums

Related

Progressbar come outside bracket

i am making a progress bar for my school assignment. But when i run my code my progress bar come outside my bracket, but the = most be inside the bracket.
public static String repeatString(int number, String str) {
for (int i = 0; i < number; i++) {
System.out.print(str);
}
return str;
}
public static String formatPercentage(int percentage) {
if (percentage == 100) {
return "done";
}
else{
return percentage + "%";
}
}
public static String formatBar(int percentage, int length) {
int amount = percentage * length/ 100;
int size = length - amount;
return "[" + repeatString(amount, "=") + repeatString(size, " ") + "] " + formatPercentage(percentage);
}
this is the result:
[= ] 5%
== [= ] 20%
======= [= ] 70%
==========[= ] done
============== [= ] 70%
Change your repeatString method to the following: Don't print anything here, just build up the string and return it.
public static String repeatString(int number, String str) {
String pad = "";
for (int i = 0; i < number; i++) {
pad += str;
}
return pad;
}
Try this:
public class ProgressBar {
private int length;
private int maxSteps;
private int step;
private char symbol;
public ProgressBar(int length, int maxSteps, char symbol) {
this.length = length;
this.maxSteps = maxSteps;
this.symbol = symbol;
}
public void increment() {
increment(1);
}
public void increment(int numSteps) {
step+=numSteps;
print();
}
private void print() {
float percentage = (float)step/(float)maxSteps;
int numSymbols = (int) Math.floor((double)length*percentage);
StringBuilder builder = new StringBuilder();
for (int i = 0; i < Math.min(numSymbols, length); i++) {
builder.append(symbol);
}
for (int i = Math.min(length, numSymbols); i < length; i++) {
builder.append(' ');
}
builder.append(String.format("[%s ]", symbol));
if (numSymbols >= length) {
builder.append(" done");
} else {
builder.append(String.format("%d %%", (int)(percentage*100)));
}
System.out.println(builder.toString());
}
public static void main(String [] args) {
ProgressBar bar = new ProgressBar(10, 50, '=');
bar.increment(10);
bar.increment(15);
bar.increment(20);
bar.increment(5);
System.out.println("Now exceeding max..");
bar.increment(10);
bar.increment(5);
}
}

How do you return multiple values from a for loop in Java using a return statement?

I would like to send multiple values from my getMultiples method to my main method using a return statement and no print or println statements.
public class StaticMethods {
public static void main (String[] args) {
int a = 6;
int b = 9;
int result = getMultiple(a,b);
System.out.println(result + "\n")
System.out.println("The first " + a + " multiples of " + b + " are: ");
int p = getMultiples(a,b);
}
public static int getMultiple(int a,int b) {
return (int) (a * b);
}
public static int getMultiples(int a, int b) {
int p = 0;
for (int i = 1; i <= a; i++) {
p = getMultiple(a,i);
}
return (p);
}
}
I have tried putting the return statement in the for loop but it does not work.
In Java as soon as return is encountered in the code, method is removed from execution stack and flow is returned back to calling method. So you can not return multiple values from a method. Rather you should create a list/array and return that as below(array example):
public class StaticMethods {
public static void main (String[] args) {
int a = 6;
int b = 9;
int result = getMultiple(a,b);
System.out.println(result + "\n");
System.out.println("The first " + a + " multiples of " + b + " are: ");
int p[] = getMultiples(a,b);
}
public static int getMultiple(int a,int b) {
return (int) (a * b);
}
public static int[] getMultiples(int a, int b) {
int[] p = new int[a];
for (int i = 1; i <= a; i++) {
p[i-1] = getMultiple(a,i);
}
return p;
}
}

Assign values to array that have multible variables in java

I had created an array of the following class , when I try to assign new values it gives me null
my code as follows
public class edge {
public double w = 0 ;
public int a = 0 ;
public int b = 0 ;
}
edge edges[];
edges = new edge[5];
int c = 10
for ( int i = 0 , i< 5 , i++) {
edges[i]=new edge();
edges[i].a = i;
edges[i].b = i + 1;
edges[i].w = i/c ;
}
You have some problems in your code :
First : you should not put your code outside your class like you do
Secondd: you have to use the main method to start your program
Third : you should to separate your statement in your loop with ; and not with , your program should look like this :
public class Edge {
public double w = 0;
public int a = 0;
public int b = 0;
public static void main(String[] args) {
Edge edges[];
edges = new Edge[5];
int c = 10;
for (int i = 0; i < 5; i++) {
edges[i] = new Edge();
edges[i].a = i;
edges[i].b = i + 1;
edges[i].w = i / c;
}
}
}
Or you can separate them in differentiate classes like this :
public class Cls {
public static void main(String[] args) {
Edge edges[];
edges = new Edge[5];
int c = 10;
for (int i = 0; i < 5; i++) {
edges[i] = new Edge();
edges[i].a = i;
edges[i].b = i + 1;
edges[i].w = i / c;
}
System.out.println(Arrays.toString(edges));
//Output
//[edge{w=0.0, a=0, b=1}, edge{w=0.0, a=1, b=2}, edge{w=0.0, a=2, b=3}, edge{w=0.0, a=3, b=4}, edge{w=0.0, a=4, b=5}]
}
}
class Edge {
public double w = 0;
public int a = 0;
public int b = 0;
#Override
public String toString() {
return "edge{" + "w=" + w + ", a=" + a + ", b=" + b + '}';
}
}

java.lang.NullPointerException appearing for no reason [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 6 years ago.
I have made a program inside of which, there is a specific method that makes sure that all of the objects in an array that point to null point to a blank space. For some reason, whenever I run the code, it gives me a java.lang.NullPointerException.
I understand what a NullPointerException is, which is why I added the if statement, so that it wouldn't give the error, but it still does
Code:
public class TextGraphics {
public static void main(String[] args) {
displaySize size = new displaySize(5,5);
displaySize.output(5,3,size,"H");
displaySize.run(size);
}
}
class displaySize {
public static int indent;
public static int sizeX = 0;
public static int sizeY = 0;
public displaySize() {
}
public displaySize(int screenX, int screenY) {
sizeX = screenX;
sizeY = screenY;
indent = sizeX;
}
public static void output(int x, int y, displaySize size, String print) {
rarray(size)[x + y * size.sizeX] = print;
}
public static String[] rarray(displaySize size) {
String [] display;
return display = new String[sizeX * sizeY];
}
public static void run(displaySize size) {
int next = 0;
for (int i = 0; i < sizeY; i++) {
for (int b = 0; b < indent; b++) {
next++;
if(rarray(size)[next].equals(null) )
{
System.out.print( rarray(size)[next] + " ");
rarray(size)[next] = " ";
}
System.out.print( rarray(size)[next] + " ");
}
System.out.println("/n");
}
}
}
first problem used .equals(null) instead of == null
second problem your code throws a arrayoutofindex because your next++ was in the wrong for loop
finally your new line character was wrong its \n not /n
corrected code
public class TextGraphics {
public static void main(String[] args) {
displaySize size = new displaySize(5,5);
displaySize.output(5,3,size,"H");
displaySize.run(size);
}
}
class displaySize {
public static int indent;
public static int sizeX = 0;
public static int sizeY = 0;
public displaySize() {
}
public displaySize(int screenX, int screenY) {
sizeX = screenX;
sizeY = screenY;
indent = sizeX;
}
public static void output(int x, int y, displaySize size, String print) {
rarray(size)[x + y * size.sizeX] = print;
}
public static String[] rarray(displaySize size) {
String [] display;
return display = new String[sizeX * sizeY];
}
public static void run(displaySize size) {
int next = 0;
for (int i = 0; i < sizeY; i++) {
next++;
for (int b = 0; b < indent; b++) {
if(rarray(size)[next]==(null) )
{
rarray(size)[next] = " ";
System.out.print( rarray(size)[next] + " ");
}
System.out.print( rarray(size)[next] + " ");
}
System.out.println("\n");
}
}
}

How to input numbers into a cell of a table?

So far I wrote code that makes a table looking kind of like the table from excel and I am able to input letters words when it compiles, but I cant say for example 1a equals 5 and make the cell in 1a show 5 inside it.this is what my table looks like so far. This is my code to draw up the table:
package table;
public class Spreadsheat {
private int xVal = 137;
private int yVal = 1;
private int[][] table = new int [20][20];
private char yaxis = 'A';
private int xaxis = 0;
public Spreadsheat(){
for(int i = 0; i < xVal; i++){
System.out.print("-");
}
System.out.println();
System.out.print(" |");
for(int i = 0; i < 17; i++){
System.out.print("| " + yaxis + "\t" + "|" );
yaxis = (char) (yaxis + 1);
}
System.out.println();
for(int i = 0; i < xVal; i++){
System.out.print("-");
}
System.out.println();
for(int j = 0; j <10;j++){
System.out.print(" " + xaxis + "|");
for(int i = 0; i < 17; i++){
System.out.print("|" + "\t" + "|" );
}
System.out.println();
xaxis = xaxis + 1;
}
}
public void setxval(int xVal) {
this.xVal = xVal;
}
public int getxVal() {
return xVal;
}
public void setyVal(int yVal) {
this.yVal = yVal;
}
public int getyVal() {
return yVal;
}
public void setTable(int table[][]) {
this.table = table;
}
public int[][] gettable() {
return table;
}
}
And this is my client program:
package Client;
import java.util.Scanner;
import table.Spreadsheat;
public class VisiCalc {
public static void main(String[] args){
Scanner kb = new Scanner(System.in);
Spreadsheat table = new Spreadsheat();
boolean end = false;
while(end == false){
String input = kb.next();
System.out.println(input);
if(input.contains("quit")){
end = true;
}
}
}
}
Your Spreadsheet needs a way to update table. For example, you need a method like this in your Spreadsheet class:
public void setValue(int row, int col, int value) {
if (row >= 0 && row < 20 && col >=0 && col < 20) {
table[row][col] = value;
}
}
You need a method way to convert your keyboard input so you can call table.setValue(...). For example, if you enter "2A=8", your method should parse that string into x=1, y=0, v=8 and call table.setValue(x,y,v). It's up to you if you want the method in Spreadsheet or in your client.
You need a method in Spreadsheet so you can print out your table to the screen with the updated content. You will realize you need to format your content so that your columns line up properly. The easiest way is to set each column to a fixed width say 8 and use java.text.DecimalFormat to convert your int to String.
I guess this is a start.

Categories

Resources