(Java) How to get an input like this - java

I need a code that reads the number of tests that will be done and then read values on a matrix for each test.
INPUT:
3
Test 1
0 20 10
1 12 34
1 5 6
Test 2
0 22 10
1 10 34
0 0 0
Test 3
0 10 10
0 0 20
0 0 0
My attempt:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Number of tests");
int n = input.nextInt();
String[] name = new String[n];
int test[][] = new int[n][3];
for (int i = 0; i < n; i++) {
System.out.println("Name of the test" + i);
name[i] = input.nextLine();
System.out.println("Values");
for (int j = 0; j < 3; j++) {
test[i][j] = input.nextInt();
}
}
}

You need a 3-D array instead of a 2-D because for each of the test has a 2-D array under it.
Demo:
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
final int ROWS = 3, COLS = 3;
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int test[][][] = new int[n][ROWS][COLS];
for (int i = 0; i < n; i++) {
System.out.println("Test " + (i + 1));
for (int j = 0; j < ROWS; j++) {
for (int k = 0; k < COLS; k++) {
test[i][j][k] = input.nextInt();
}
}
}
// Display
System.out.println("Displaying...");
for (int i = 0; i < n; i++) {
System.out.println("Test " + (i + 1));
for (int j = 0; j < ROWS; j++) {
for (int k = 0; k < COLS; k++) {
System.out.printf("%4d", test[i][j][k]);
}
System.out.println();
}
}
}
}
A sample run:
3
Test 1
0 20 10
1 12 34
1 5 6
Test 2
0 22 10
1 10 34
0 0 0
Test 3
0 10 10
0 0 20
0 0 0
Displaying...
Test 1
0 20 10
1 12 34
1 5 6
Test 2
0 22 10
1 10 34
0 0 0
Test 3
0 10 10
0 0 20
0 0 0

Related

DFS and BFS with adjacency matrix counted in Java

Currently trying to build a program that can implement both DFS and BFS in Java by taking in an adjacency matrix from a file and printing out the following info: order that vertices are first encountered, order that vertices become dead ends, number of components, and the tree edges.
Here is my code:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class ProjectDriver {
public static int count;
public static void main(String[] args) throws FileNotFoundException {
ArrayList<Integer> dfsDeadEnd = new ArrayList<Integer>();
ArrayList<Integer> dfsVertices = new ArrayList<Integer>();
ArrayList<Integer> bfsVertices = new ArrayList<Integer>();
int dfsComponents = 0;
int bfsComponents = 0;
Scanner scanner = new Scanner(new File("sample1.txt"));
int n = 8;
int[][] edges = new int[n][n];
boolean[] visited = new boolean[n];
count = 0;
for(int i=0; i < n; i++) {
for (int j=0; j < n; j++) {
edges[i][j] = scanner.nextInt();
}
}
for(int i = 0; i < n; i++){
visited[i] = false;
}
int[][] BFStreeEdgeGraph = new int[n][n];
int[][] DFStreeEdgeGraph = new int[n][n];
int[][] crossGraph = new int[n][n];
for(int i = 0; i <= n-1; i++){
for(int j = 0; j <= n-1; j++){
DFStreeEdgeGraph[i][j] = 0;
BFStreeEdgeGraph[i][j] = 0;
crossGraph[i][j] = 0;
}
}
for(int i = 0; i <= n-1; i++){
if(visited[i] == false){
dfs(edges,i,visited, dfsVertices, dfsDeadEnd, DFStreeEdgeGraph);
dfsDeadEnd.add(i);
dfsComponents++;
}
}
for(int i = 0; i <= n-1; i++) {
if(visited[i] == false) {
bfs(edges, i, visited, bfsVertices, BFStreeEdgeGraph);
bfsComponents++;
}
}
System.out.println();
System.out.println("DFS: Number of Connected Components: " + dfsComponents);
System.out.print("DFS: Order of First Encountered: ");
for(int i : dfsVertices){
System.out.print((i+1) + " ");
}
System.out.println();
System.out.print("DFS: Order of First Dead-Ends: ");
for(int i : dfsDeadEnd){
System.out.print((i+1) + " ");
}
System.out.println();
System.out.println();
System.out.println("Tree edges:");
displayGraph(DFStreeEdgeGraph, n);
System.out.println();
System.out.println();
System.out.println("BFS: Number of Connected Components: " + bfsComponents);
System.out.print("BFS: Order of First encountered: ");
for(int i : bfsVertices){
System.out.print((i+1) + " ");
}
System.out.println();
System.out.println();
}
public static void dfs(int[][] edges, int vertex, boolean[] visited, ArrayList<Integer> dfsVertices, ArrayList<Integer> dfsDeadEnd, int[][] DFStreeEdgeGraph) {
visited[vertex] = true;
dfsVertices.add(count, vertex);
count = count + 1;
for(int w = 0; w <= edges.length-1; w++) {
if(edges[vertex][w] == 1 && !visited[w]) {
DFStreeEdgeGraph[vertex][w] = 1;
dfs(edges, w, visited, dfsVertices, dfsDeadEnd, DFStreeEdgeGraph);
dfsDeadEnd.add(w);
}
}
}
public static void bfs(int[][] edges, int vertex, boolean[] visited, ArrayList<Integer> bfsVertices, int[][] BFStreeEdgeGraph) {
bfsVertices.add(count, vertex);
count = count + 1;
for(int w = 0; w < edges.length; w++) {
if(edges[vertex][w] != 0 && !visited[w]) {
visited[vertex] = true;
}
}
for(int w = bfsVertices.indexOf(vertex) + 1; w < bfsVertices.size(); w++) {
int value = bfsVertices.get(w);
bfs(edges, value, visited, bfsVertices, BFStreeEdgeGraph);
}
}
public static void displayGraph(int[][] graph, int n) {
for(int i = 0; i <= n-1; ++i){
System.out.print(" ");
for(int j = 0; j <= n-1; ++j){
System.out.print(graph[i][j] + " ");
}
System.out.println();
}
}
}
And here is the output from running my code:
Input graph:
0 1 0 0 1 1 0 0
1 0 0 0 0 1 1 0
0 0 0 1 0 0 1 0
0 0 1 0 0 0 0 1
1 0 0 0 0 1 0 0
1 1 0 0 1 0 0 0
0 1 1 0 0 0 0 1
0 0 0 1 0 0 1 0
DFS: Number of Connected Components: 1
DFS: Order of First Encountered: 1 2 6 5 7 3 4 8
DFS: Order of First Dead-Ends: 5 6 8 4 3 7 2 1
Tree edges:
0 1 0 0 0 0 0 0
0 0 0 0 0 1 1 0
0 0 0 1 0 0 0 0
0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0
0 0 0 0 1 0 0 0
0 0 1 0 0 0 0 0
0 0 0 0 0 0 0 0
BFS: Number of Connected Components: 0
BFS: Order of First encountered:
And here are my issues. For the DFS, my order of first encountered should be 1 2 6 7 4 3 5 8, but this is not what I'm getting. Additionally, my order of dead ends should be 8 7 5 4 1 2 6 3, but this is also off. My tree edges managed to come out correct.
For the BFS, I can't get anything to print, and debugging my DFS and BFS methods hasn't given me the answer yet. I'd be very grateful from some help.

My nested for loop number pattern doesn't out put the correct pattern

I'm trying to print out this pattern but I'm having trouble arranging the numbers and spacing them out to achieve the expected out put.
Expected Output:
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
6 5 4 3 2 1
1 2 3 4 5 6
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
Here is my Code. I don't know how to space the numbers and put the correct numbers in the order that the out put has specified.
import java.util.Scanner;
public class patterns{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
for (int i = 6; i >= 1; i--)
{
for (int j = 1; j < i; j++)
{
System.out.print(" ");
}
for (int j = i; j <= 6; j++)
{
System.out.print(j);
}
System.out.println();
}
System.out.println();
for (int i = 1; i <= 6; i++)
{
for (int j = 1; j < i; j++)
{
System.out.print(" ");
}
for (int j = i; j <= 6; j++)
{
System.out.print(j);
}
System.out.println();
}
}
}
This is the Output I'm Getting
run:
6
56
456
3456
23456
123456
123456
23456
3456
456
56
6
You should think that each triangle block actually has 6x6 square cells. So it means you need 2 nested loops, each will iterate 6 times.
Then inside you will decide whether you print a number or space, depending on some condition.
for (int i = 6; i >= 1; i--)
{
for (int j = 1; j <= 6; j++)
{
if (j < i)
{
System.out.print(" ");
} else
{
System.out.print(7 - j + " ");
}
}
System.out.println("");
}
System.out.println("");
for (int i = 1; i <= 6; i++)
{
for (int j = 1; j <= 6; j++)
{
if (j < i)
{
System.out.print(" ");
} else
{
System.out.print(j-i+1 + " ");
}
}
System.out.println("");
}
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
6 5 4 3 2 1
1 2 3 4 5 6
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
int a = 654321;
String str = new Integer(a).toString();
for (int i = str.length() - 1; i >= 0 ; i--) {
String g = String.format("%6s", str.substring(i));
System.out.println(g.replaceAll(".(?=.)", "$0 "));
}
System.out.println();
str = new StringBuffer(str).reverse().toString();
for (int i = str.length() - 1; i >= 0 ; i--) {
String g = String.format("%6s", str.substring(0, i+1));
System.out.println(g.replaceAll(".(?=.)", "$0 "));
}
Here's the modified code for your patterns.
public class Patterns {
public static void main(String[] args) {
for (int i = 0; i < 6; i++) {
for (int j = 0; j < (5 - i) * 2; j++) {
System.out.print(" ");
}
for (int j = i + 1; j >= 1; j--) {
System.out.print(j + " ");
}
System.out.println();
}
System.out.println();
for (int i = 0; i < 6; i++) {
for (int j = 0; j < i * 2; j++) {
System.out.print(" ");
}
for (int j = 1; j <= 6 - i; j++) {
System.out.print(j + " ");
}
System.out.println();
}
}
}
Output
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
6 5 4 3 2 1
1 2 3 4 5 6
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
Update: fixed bug for spaces
Working=> Consider the first half of the pattern
1 line 0: 10 spaces = (5 - 0) * 2 spaces
2 1 line 1: 8 spaces = (5 - 1) * 2 spaces
3 2 1 line 2: 6 spaces = (5 - 2) * 2 spaces
4 3 2 1 line 3: 4 spaces = (5 - 3) * 2 spaces
5 4 3 2 1 line 4: 2 spaces = (5 - 4) * 2 spaces
6 5 4 3 2 1 line 5: 0 spaces = (5 - 5) * 2 spaces
Consider the second half of the pattern
1 2 3 4 5 6 line 0: 0 spaces = 0 * 2 spaces
1 2 3 4 5 line 1: 2 spaces = 1 * 2 spaces
1 2 3 4 line 2: 4 spaces = 2 * 2 spaces
1 2 3 line 3: 6 spaces = 3 * 2 spaces
1 2 line 4: 8 spaces = 4 * 2 spaces
1 line 5: 10 spaces = 5 * 2 spaces
One way to do this would be to do:
import java.util.Scanner;
public class pattern{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
for (int i = 1; i <= 6; i++)
{
String val = "";
for (int j = i; j >= 1; j--)
{
val += j+ " ";
}
System.out.printf("%12s", val);
System.out.println();
}
System.out.println();
for (int i = 6; i >= 1; i--)
{
String val = "";
int ii=1;
for (int j = i; j >= 1; j--)
{
val += ii + " ";
ii++;
}
System.out.printf("%12s", val);
System.out.println();
}
}
Output:
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
6 5 4 3 2 1
1 2 3 4 5 6
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
One thing to keep in mind is that when you are trying to format text like you are, you should use printf rather than adding print lines.
Best way is to use an array -> you loop less times. http://ideone.com/diYtvM Cheers!
import java.util.Scanner;
public class patterns{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int[][] arr = new int[6][6];
for(int i = 0; i<6; i++)
arr[i][5] = 1;
for(int i = 1; i<6; i++)
for (int j = 4; j>=5-i; j--)
{
arr[i][j] = arr[i-1][j+1] + 1;
}
for(int i = 0; i<6; i++)
{
for (int j = 0; j<6; j++)
{
if(arr[i][j] > 0)
System.out.print(arr[i][j] + " ");
else
System.out.print(" ");
}
System.out.println("");
}
System.out.println("");
int z = 0;
for(int i = 5; i>=0; i--)
{
for(int k = 0; k<z; k++)
System.out.print(" ");
for (int j = 5; j>=0; j--)
{
if(arr[i][j] > 0)
System.out.print(arr[i][j] + " ");
}
System.out.println("");
z++;
}
}
}
OUT:
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
6 5 4 3 2 1
1 2 3 4 5 6
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1

Java Multidimensional Array Outprint

I dont understand, why these numbers are printed out. Shouldn't it just out print 3 2 1? Instead, it prints:
3
0
0
0
2
0
0
0
1
Thank you for your help :)
public static void main(String[] args) {
int i, j, n = 3;
int[][] polje = new int[n][n];
polje[0][0] = 3;
polje[1][1] = 2;
polje[2][2] = 1;
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
System.out.print(polje[i][j] + " ");
System.out.println();
}
}
}
you have set likewise,
3 0 0
0 2 0
0 0 1
so obviously you get, 3 0 0 0 2 0 0 0 1
if you want to print only 3 2 1 then made this changes only,
if(i == j){
System.out.print(polje[i][j] + " ");
System.out.println();
}

How to print numbers in a triangle pattern?

Below is the pattern I would like to print in the console:
1
2 0 2
3 0 0 0 3
And below is the code I have right now. Each number should have 5 spaces between each other.
import java.util.Scanner;
public class Triangle {
public static void main(String args[]) {
System.out.println("Input the number of lines you want to print.");
Scanner a = new Scanner(System.in);
int n = a.nextInt();
int[] row = new int[0];
for(int i=0 ; i < n ; i++){
row = nextRow(row);
for(int j=0;j < n-i;j++){
//Padding For Triangle
System.out.print(" ");
}
//Output the values
for(int j=0 ; j < row.length ; j++){
System.out.print(row[j]+" ");
}
//Start New Line
System.out.println();
}
}
/*Find Values Of Next Row*/
public static int[] nextRow(int row[]){
int nextRow[] = new int [row.length+1];
nextRow[0] = row.length+1;
nextRow[nextRow.length-1] =row.length+1;
for(int i=1 ; i < nextRow.length-1 ; i++){
nextRow[i] = 0;
}
return nextRow;
}
}
Can anyone help me with this?
Here is some modified code. Its not complete and still something need to update your code to get desired result. That part I am leaving it for you. I also left hint in code so that you could modify.
1.
// Padding For Triangle
System.out.print(" "); // 6 white space
2.
System.out.print(row[k]);
System.out.print(" "); // 5 white space
3.
/* Find Values Of Next Row */
#SuppressWarnings("null")
public static int[] nextRow(int row[]) {
int nextRow[] = null;
if (row.length == 0) {
nextRow = new int[1];
nextRow[0] = 1;
} else {
// count++; // Hint
nextRow = new int[row.length + 2];
for (int i = 0; i < nextRow.length; i++) {
if ((i == 0 || i == nextRow.length - 1)) {
nextRow[i] = nextRow.length - 2;
// nextRow[i] = count;
} else {
nextRow[i] = 0;
}
}
}
return nextRow;
}
If you have any questions, just ask. Good Luck.
You first have to identify the pattern that the output expects, which in your case is:
Row0, columns 1
Row1, columns 3
Row2, columns 5
Row3, columns 7
which is, rowNum*2+1
Based, on this, I modified your code and here is the working solution:
import java.util.Scanner;
public class Triangle{
public static void main(String args[]) {
System.out.println("Input the number of lines you want to print.");
Scanner a = new Scanner(System.in);
int n = a.nextInt();
int[] row = null;
for(int i=0 ; i < n ; i++){
row = nextRow(i);
for(int j=0;j < n-i;j++){
//Padding For Triangle
System.out.print(" ");
}
//Output the values
for(int j=0 ; j < row.length ; j++){
System.out.print(row[j]+" ");
}
//Start New Line
System.out.println();
}
}
/*Find Values Of Next Row*/
public static int[] nextRow(int rowNum){
int nextRow[] = new int [rowNum*2+1];
nextRow[0] = rowNum+1;//-rowNum/2;
nextRow[nextRow.length-1] = nextRow[0];
for(int i=1 ; i < nextRow.length-1 ; i++){
nextRow[i] = 0;
}
return nextRow;
}
}
And here is some output:
2:
1
2 0 2
5:
1
2 0 2
3 0 0 0 3
4 0 0 0 0 0 4
5 0 0 0 0 0 0 0 5
10:
1
2 0 2
3 0 0 0 3
4 0 0 0 0 0 4
5 0 0 0 0 0 0 0 5
6 0 0 0 0 0 0 0 0 0 6
7 0 0 0 0 0 0 0 0 0 0 0 7
8 0 0 0 0 0 0 0 0 0 0 0 0 0 8
9 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 9
10 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 10
i saw your code, i think that we can do this, follow is my modified code:
public class Triangle {
public static void main(String args[]) {
System.out.println("Input the number of lines you want to print.");
Scanner a = new Scanner(System.in);
int n = a.nextInt();
int [] row = new int[0];
for (int i = 0; i < n; i++) {
row = nextRow(row);
for (int j = 0; j < n - i; j++) {
// Padding For Triangle
System.out.print(" ");
}
// Output the values
for (int j = 0; j < row.length; j++) {
System.out.print(row[j] + " ");
}
// Start New Line
System.out.println();
}
}
/* set space between each other. */
public static String printSpace(int n) {
String result = "";
for (int i = 0; i < n; i++) {
result += " ";// 5 space
}
return result;
}
/* Find Values Of Next Row */
public static int [] nextRow(int row[]) {
int nextRow[] = new int[row.length + 1];
nextRow[0] = row.length + 1;
nextRow[nextRow.length - 1] = row.length + 1;
for (int i = 1; i < nextRow.length - 1; i++) {
nextRow[i] = 0;
}
return nextRow;
}
}
may be this is answer what you want.
Look up the arithmetic sequence for printing out the zeros, the rest should be trivial.

Pascal's Triangle Format [duplicate]

This question already has answers here:
Pascal's triangle 2d array - formatting printed output
(5 answers)
Closed 1 year ago.
The assignment is to create Pascal's Triangle without using arrays. I have the method that produces the values for the triangle below. The method accepts an integer for the maximum number of rows the user wants printed.
public static void triangle(int maxRows) {
int r, num;
for (int i = 0; i <= maxRows; i++) {
num = 1;
r = i + 1;
for (int col = 0; col <= i; col++) {
if (col > 0) {
num = num * (r - col) / col;
}
System.out.print(num + " ");
}
System.out.println();
}
}
I need to format the values of the triangle such that it looks like a triangle:
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
I can't for the life of me figure out how to do that. Please answer keeping in mind that I'm a beginner in Java programming.
public static long pascalTriangle(int r, int k) {
if (r == 1 || k <= 1 || k >= r) return 1L;
return pascalTriangle(r - 1, k - 1) + pascalTriangle(r - 1, k);
}
This method allows you to find the k-th value of r-th row.
This is a good start, where it's homework, I'll leave the rest to you:
int maxRows = 6;
int r, num;
for (int i = 0; i <= maxRows; i++) {
num = 1;
r = i + 1;
//pre-spacing
for (int j = maxRows - i; j > 0; j--) {
System.out.print(" ");
}
for (int col = 0; col <= i; col++) {
if (col > 0) {
num = num * (r - col) / col;
}
System.out.print(num + " ");
}
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
In each row you will need to print:
n spaces
m numbers
n spaces
Your job is to figure out n (which will be zero in the last line) and m based on row number.
[This is more like a comment but I needed more formatting options than comments provide]
You need to print the spaces (like others have mentioned) and also as this is homework I'm leaving it to you but you might want to look at this handy little function
System.out.printf();
Here is a handy reference guide
Also note that you will need to take into account that some numbers are more than 1 digit long!
import java.util.*;
class Mine {
public static void main(String ar[]) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for (int i = 1; i < n; i++) {
int size = 1;
for (int j = 1; j <= i; j++) {
int a[] = new int[size];
int d[] = new int[size];
for (int k = 1; k <= size; k++) {
a[1] = 1;
a[size] = 1;
for (int p = 1; p <= size; p++) {
d[p] = a[p];
}
if (size >= 3) {
for (int m = 2; m < size; m++) {
a[m] = d[m] + d[m - 1];
}
}
}
for (int y = 0; y < size; y++) {
System.out.print(a[y]);
}
System.out.println(" ");
}
++size;
}
}
}
public class HelloWorld {
public static void main(String[] args) {
int s = 7;
int k = 1;
int r;
for (int i = 1; i <= s; i++) {
int num = 1;
r = i;
int col = 0;
for (int j = 1; j <= 2 * s - 1; j++) {
if (j <= s - i)
System.out.print(" ");
else if (j >= s + i)
System.out.print(" ");
else {
if (k % 2 == 0) {
System.out.print(" ");
} else {
if (col > 0) {
num = num * (r - col) / col;
}
System.out.print(num + " ");
col++;
}
k++;
}
}
System.out.println("");
k = 1;
}
}
}
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
You can try this code in java. It's simple :)
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
Code perfectly prints pascal triangle:
public static void main(String[] args) {
int a, num;
for (int i = 0; i <= 4; i++) {
num = 1;
a = i + 1;
for (int j = 4; j > 0; j--) {
if (j > i)
System.out.print(" ");
}
for (int j = 0; j <= i; j++) {
if (j > 0)
num = num * (a - j) / j;
System.out.print(num + " ");
}
System.out.println();
}
}
Output:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1

Categories

Resources