Placing an array into a file column column - java

I currently have a .txt file with one line that looks like:
xxxxxxxxxxxxxxxxxxxxx
I would like to place that one line into a 2d array so my file can come out like so:
xxxxxxx
xxxxxxx
xxxxxxx
Here's my java class:
import java.util.*;
import java.io.*;
public class EncryptDecrypt {
public static void encrypt() throws IOException {
BufferedReader in = new BufferedReader(new FileReader("TextFile.txt"));
String line = in.readLine();
String[][] e = new String[5][3];
// fill array
for(int i = 0; i < e.length; i++) {
for(int j = 0; j < e.length; j++) {
e[i][j] = line;
}
}
// print array
for(int i = 0; i < e.length; i++) {
for(int j = 0; j < e.length; j++) {
System.out.println(e[i][j]);
break;
}
}
}
public static void main(String[] args) throws IOException {
encrypt();
}
}
When I run my java class, all I get is this which's not what I want:
xxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxx

You have to either use a 2D array of char or 1D array of String
'String' class is already internally implemented as an array of 'Char'.
Just use a 1D array of String, like:
String[] e = new String[3];
// fill array
for(int i = 0; i < e.length; i++) {
e[i] = line;
}
// print array
for(int i = 0; i < e.length; i++) {
System.out.println(e[i]);
}
The 2D version would be something like below (not recommended)
String line = in.readLine();
Char[][] e = new Char[3][5];
for(int i = 0; i < e.length; i++) {
for(int j = 0; j < e.length; j++) {
e[i][j] = line.charAt(j);//to access element at jth index of string
}
}
// print array
for(int i = 0; i < e.length; i++) {
for(int j = 0; j < e.length; j++) {
System.out.print(e[i][j]);
}
System.out.println()
}
Anyways for printing you do not to store in array, just do:
int noOfRows = 3;
for(int i=0;i<noOfRows;i++){
System.out.println(line)
}

You could just iterate over the string like this:
String line = "xxxxxxxxxxxxxxxxxxxxx";
int colums = 7;
for (int i = 0; i < line.length(); i++) {
System.out.print(line.charAt(i));
if ((i + 1) % colums == 0) {
System.out.println();
}
}
A more declarative version is possible when using Guava's Splitter:
for (String token : Splitter.fixedLength(coluns).split(line)) {
System.out.println(token);
}

Related

error: incompatible types: String[] cannot be converted to String

I tried taking input of a 6 by 6 matrix in java using the string split function when the string is input in the following way, and to print the matrix.
1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
The output that I get is
Main.java:24: error: incompatible types: String[] cannot be converted to String
c[j] = b[i].split(" ");
my code:
import java.util.*;
import java.io.*;
class Solution {
public static void main(String args[]) {
Scanner s = new Scanner(System.in);
int a[][] = new int[6][6];
String b[] = new String[6];
for (int i = 0; i < 6; i++) {
b[i] = s.nextLine();
}
// initializing the 2d array a[][]
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 6; j++) {
String c[] = new String[6];
c[j] = b[i].split(" ");
a[i][j] = Integer.parseInt(c[j]);
}
}
// printing the input array
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 6; j++) {
System.out.print("\ta[i][j]\t");
}
}
}
}
pls, suggest how I can overcome this error
When we call split function of String return the String[]. So c[j] (which is of type String) can't be equal to String[].
Below code should be replaced as:
// initializing the 2d array a[][]
for (int i = 0; i < 6; i++) {
String[] c = b[i].split(" ");
for (int j = 0; j < 6; j++) {
a[i][j] = Integer.parseInt(c[j]);
}
}
The return type of split() function is type of array. Because you are asking java to give me each value as separate which is separated by " " (space). So java will create an array of each value and returns you the array. For storing the array you need an variable of type array. here c represent an array, but c[j] represents an single index of the array.
You can change your code like:
for (int i = 0; i < 6; i++) {
String c[] = b[i].split(" ");
for (int k = 0; k < c.length; k++) {
a[i][k] = Integer.parseInt(c[k]);
}
}
The the inputs are integer and you are converting them to integer later, I would suggest you to take input as integer like below:
class Solution {
public static void main(String args[]) {
Scanner s = new Scanner(System.in);
int a[][] = new int[6][6];
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 6; j++) {
a[i][j] = s.nextInt();
}
}
// printing the input array
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 6; j++) {
System.out.print("\ta[i][j]\t");
}
}
}
}

Two dimensional array to one dimensional array in Java

I have a two dimensional array and I fill it with scanner. I want to copy the elements that start with letter 'a' to a new one dimensional array without using ArrayList. Please advise on what I can do to get this code functioning properly. the question is how can I know the new array size while I don't know how many words start with letter a
Here is what I have so far:
import java.util.Scanner;
class Untitled {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String[][] name = new String[2][2];
for (int i = 0; i < name.length; i++) {
for (int j = 0; j < name[i].length; j++) {
name[i][j] = input.next();
}
}
student(name);
}
public static void student(String[][] arr) {
int count = 0;
int c2 = -1;
String[] name2 = new String[count];
String temp = "";
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
if (arr[i][j].charAt(0) == 'a') {
c2++;
temp = arr[i][j];
name2[c2] = temp;
count++;
temp = "";
}
}//inner
}//outer
for (int i = 0; i < name2.length; i++) {
System.out.println(name2[i]);
}
}
}
A two dimensional arrray of size [n][n] is equal to one dimensional array of size n. If you want to copy them on proper place then you can use this formula, it is useful if you later want to copy these elements back to twodimensional array at proper places:
int v = i * n + j; // i and j your loops and n is length of rows or colums.
array[v] = array[i][j];
for in your codes it's like:
int v = 0;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
if (arr[i][j].charAt(0) == 'a') {
v = i * arra.length +j;
name2[v] = arr[i][j];
count++;
Ok here is a working code:
public static void main(String [] args) {
Scanner input = new Scanner(System.in);
String[][] name = new String[2][2];
System.out.println("Enter the name: ");
for (int i = 0; i < name.length; i++) {
for (int j = 0; j < name[i].length; j++) {
name[i][j] = input.next();
}
}
student(name);
}
public static void student(String[][] arr) {
int count = 0;
int v = 0;
String[] name2 = new String[arr.length*arr[0].length];
String temp = "";
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
if (arr[i][j].charAt(0) == 'a') {
v = i *+arr[0].length + j;
name2[v] = arr[i][j];
count++;
}
}//inner
}//outer
for (int i = 0; i < name2.length; i++) {
System.out.println(name2[i]);
}
System.out.println("printing without nulls");
//if you don't want null to be printed then do this:
for (int i = 0; i < name2.length; i++) {
if(name2[i] != null)
System.out.println(name2[i]);
}
}
I did it with two nested for loop one for indicating the array size and the other for filling the elements into the array, it does the work but is there any way to do this better
public static void student(String[][] arr) {
int size = 0;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
if (arr[i][j].charAt(0) == 'a') {
size++;
}
}//inner
}//outer
String[] name2 = new String[size];
int count = 0;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
if (arr[i][j].charAt(0) == 'a') {
name2[count] = arr[i][j];
count++;
}
}//inner
}//outer
for (int i = 0; i < name2.length; i++) {
System.out.println(name2[i]);
}

How would fill this array in this form?

I want my program to output this in a certain way, how would I make this possible? So far, my code is giving me the wrong thing.
Here's my .txt file:
ABCDEFGHIJKLMNOPQRSTUVWXYZOOOOOOO
Here's my java file:
import java.io.*;
public class EncryptDecrypt {
public static void encrypt() throws IOException {
BufferedReader in = new BufferedReader(new FileReader("cryptographyTextFile.txt"));
String line = in.readLine();
char[][] table = new char[6][5];
// fill array
for(int i = 0; i < 6; i++) {
for(int j = 0; j < 5; j++) {
while(table[i][j] < 6) {
table[i][j] = line.charAt(j);
}
}
}
// print array
for(int i = 0; i < 6; i++) {
for(int j = 0; j < 5; j++) {
System.out.println(table[i][j] + " ");
}
System.out.println();
}
}
public static void main(String[] args) throws IOException {
encrypt();
}
}
How would I print this .txt file like so:
ABCDE
GHIJK
MNOPQ
STUVW
XYZOO
OOOOO
A couple of problems
see
String line = "ABCDEFGHIJKLMNOPQRSTUVWXYZOOOOOOO";
char[][] table = new char[6][5];
int counter = 0;
// fill array
for(int i = 0; i < 6; i++) {
for(int j = 0; j < 5; j++) {
table[i][j] = line.charAt(counter++); // need to increment through the String
}
}
// print array
for(int i = 0; i < 6; i++) {
for(int j = 0; j < 5; j++) {
System.out.print(table[i][j] + " "); // not println
}
System.out.println();
}
output
A B C D E
F G H I J
K L M N O
P Q R S T
U V W X Y
Z O O O O
Although a more extensible way would be link
String line = "ABCDEFGHIJKLMNOPQRSTUVWXYZOOOOOOO";
String lines [] = line.split("(?<=\\G.....)");
for (String l : lines) {
System.out.println(l);
}

Hi, trying to print a certain amount of character based on array values. Java

so my question is how could I print a certain amount of characters based on a array value?
So currently I have an array declared globally like this
static float timesOccured [] = {5,3,7,3,1};
In a method called draw I've tried a few things to try and get it so the output would be something along the line like this
|||||
|||
|||||||
|||
|
Could anyone help me out?
Much appreciated.
You would need to use nested for loops as I have done below:
for (int i = 0; i < timesOccured.length; i++) {
for (int j = 0; j < timesOccured[i]; j++) {
// print characters here
}
}
Loop through the timesOccured array and get each entry; and use the entry (i.e. timesOccured[i]) to print your lines in the nested for loop.
I hope this helps.
public class PrintChar {
static float timesOccured [] = {5,3,7,3,1};
public static void draw(){
for(int i=0; i<timesOccured.length;i++){
for(int j=0; j< timesOccured[i]; j++){
System.out.print("!");
}
System.out.println();
}
}
public static void main(String[] args) {
draw();
}
}
Here are seven ways to do it:
import org.apache.commons.lang.StringUtils;
import com.google.common.base.Strings;
public class StringRepeat {
static int timesOccured[] = { 5, 3, 7, 3, 1 };
static String s = "|";
static String t = "||||||||||||||||||";
public static String repeat(int j) {
return (t.substring(0, j));
}
public static void main(String[] args) {
System.out.println("Using native Java String.substring");
for (int i = 0; i < timesOccured.length; i++) {
System.out.println(repeat(timesOccured[i]));
}
System.out.println("\nUsing native Java char.replace");
for (int i = 0; i < timesOccured.length; i++) {
System.out.println(new String(new char[timesOccured[i]]).replace("\0", s));
}
System.out.println("\nUsing native Java String.format.replace");
String result = "";
for (int i = 0; i < timesOccured.length; i++) {
System.out.println(String.format(String.format("%%0%dd", timesOccured[i]), 0).replace("0",s));
}
System.out.println(result);
System.out.println("\nUsing native Java StringBuilder.append");
for (int i = 0; i < timesOccured.length; i++) {
StringBuilder sb = new StringBuilder();
for (int j = 0; j < timesOccured[i]; j++) { sb.append(s); };
System.out.println(sb.toString());
}
System.out.println("\nUsing native Jave double for loops");
for (int i = 0; i < timesOccured.length; i++) {
String u = "";
for (int j = 0; j < timesOccured[i]; j++) {
u = u + s;
}
System.out.println(u);
}
System.out.println("\nUsing org.apache.commons.lang.StringUtils.repeat");
for (int i = 0; i < timesOccured.length; i++) {
System.out.println(StringUtils.repeat(s, timesOccured[i]));
}
System.out.println("\nUsing com.google.common.base.Strings.repeat");
for (int i = 0; i < timesOccured.length; i++) {
System.out.println(Strings.repeat(s, timesOccured[i]));
}
}
}

Trying to read matrix in Java

Hi guys im trying to read from input a number that determines the size of matrix to be created. Then read said matrix and reproduce it.
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int dim = in.nextInt();
char[][] tab = new char[dim][dim];
in.nextLine();
String temp = in.nextLine();
for (int i = 0; i < dim - 1; i++) {
for (int j = 0; j < dim - 1; j++) {
tab[i][j] = temp.charAt(j);
}
temp = in.nextLine();
}
for (int i = 0; i < dim; i++) {
for (int j = 0; j < dim; j++) {
System.out.print(tab[i][j]);
}
System.out.println();
}
}
Thing is it is ignoring the last char of eache line and the last line. Testing with this input:
4
XXXX
OOO.
....
....
Your first double for-loop (the one that reads the input into your tab-matrix) should say i < dim instead of i < dim - 1, and idem for jin the inner for-loop
Like this:
for (int i = 0; i < dim ; i++) { //Removed - 1
for (int j = 0; j < dim ; j++) { //Removed - 1
tab[i][j] = temp.charAt(j);
}
temp = in.nextLine();
}
you just had a small issue you had to go to the last dimension so for an array of 5 you should go til 4 but in the code it goes just to 3 because of the int i = 0; **i < dim - 1**; i++
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int dim = in.nextInt();
char[][] tab = new char[dim][dim];
in.nextLine();
String temp = in.nextLine();
for (int i = 0; i <= dim - 1; i++) {
for (int j = 0; j < dim - 1; j++) {
tab[i][j] = temp.charAt(j);
}
temp = in.nextLine();
}
for (int i = 0; i < dim; i++) {
for (int j = 0; j < dim; j++) {
System.out.print(tab[i][j]);
}
System.out.println();
}
}
The bounds on the loops when creating your matrix are incorrect. You should either do
for (int i = 0; i < dim; i++) {
for (int j = 0; j < dim; j++) {
Or
for (int i = 0; i <= dim - 1; i++) {
for (int j = 0; j <= dim - 1; j++) {
But don't mix the two. Pick either strictly less than a bound or less than or equal to the bound - 1.

Categories