How can make a two dimensional Array of integers by reading from a .txt file that looks like this:
0000
0100
1233
Would you use BufferedReader or InputStream?
Here is what I have so far and it either crashes, or it just says 52, 52, 52....
public static void loadTileMap(String fileName, int height, int width) throws IOException{
BufferedReader reader = new BufferedReader(new InputStreamReader(GameMainActivity.assets.open(fileName)));
String line;
tileArray = new int[width][height];
while (true) {
line = reader.readLine();
if (line == null) {
reader.close();
break;
}
for (int i = 0; i < width; i++) {
String string = line.toString();
for (int j = 0; j < height; j++) {
if (j < string.length()) {
int k = (int)string.charAt(j);
tileArray[i][j] = k;
}
}
}
}
}
Bufferdreader. Also to make a 2D array like that the steps should look like this:
1 - make the 2D array
2 - as you read watch line add it to the array[line#][0]
Also, you are converting the char into an int. Thereby causing it to change to its Unicode (or ASCII I don't know) representation e.g. 52.
OK thank you for all your help! I did what CyberGeek.exe suggested but I modified it a bit. Here is my code:
public static int[][] tileArray;
public static void loadTileMap(String fileName, int height, int width) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(GameMainActivity.assets.open(fileName)));
String line;
tileArray = new int[width][height];
for (int i = 0; i < width; i++) {
line = reader.readLine();
if (line == null) {
reader.close();
}
for (int j = 0; j < height; j++) {
int k = Integer.parseInt(line.substring(j, j+1));
tileArray[i][j] = k;
}
}
}
There might be an easier way but I'm not sure. Either way this works for me!
I want to know how to hide some numbers in TextView and some are shown just like that
(****-****-1234)
Thank you
Try This Method...
public static String StrRpl(String str) {
char[] chars = str.toCharArray();
for (int i = 0, j = 0; i < chars.length && j < 5; i++) {
char ch = chars[i];
if (!Character.isWhitespace(ch)) {
chars[i] = '*';
j++;
}
}
str = new String(chars);
return str;
}
Output : *****234
pass a string to this method and it will return string with '*' character till first 5 characters (You can change your number of count.. current is 5)
I am attempting to implement a conversion from a decimal number to ascii. Right now, i've hard coded it to 50 but I will use userinput later on. When I run the application and call the method, what is outputed is random (it changes everytime I press it) eg "[C#4263f600"
I followed the process in the link: decimal to binary. Why am I getting such a weird output?
//method
if (valid) {
String str = ascii();
mTextOutput.setText(str);
...
public String ascii(){
char[] binary_reverse = new char[9];
char[] binary = new char[9];
int ascii = 50;
int y = 0;
while (ascii !=1) {
if (ascii % 2 ==0)
{
binary_reverse[y]='0';
}
else if (ascii % 2 == 1)
{
binary_reverse[y]='1';
}
ascii /= 2;
y++;
}
if (ascii ==1)
{
binary_reverse[y] = '1';
}
if (y<8) {
for(; y < 8; y++) {
binary_reverse[y] = '0';
}
}
for (int z = 0; z < 8; z++) {
binary[z] = binary_reverse[7-1];
}
String str = binary.toString();
return str;
}
//You can try this :
binary[z] = binary_reverse[7-1]; - So you want to set every element of binary to the value in binary_reverse[6]
//and also
String str = binary.toString(); to String str = new String(binary);
reference : #Tim
I am using this code for translate English digit into Arabic. But now i am trying to change Arabic digits into English digit.
private void decimalToArabic() {
String str = showOutputEdit.getText().toString();
char[] arabicChars = {'٠','١','٢','٣','٤','٥','٦','٧','٨','٩'};
// char[] arabicChars = {'٩','٨','٧','٦','٥','٤','٣','٢','١','٠'};
StringBuilder builder = new StringBuilder();
for(int i =0;i<str.length();i++)
{
if(Character.isDigit(str.charAt(i)))
{
builder.append(arabicChars[(int)(str.charAt(i))-48]);
}
else
{
builder.append(str.charAt(i));
}
}
System.out.println("Number in English : "+str);
System.out.println("Number In Arabic : "+builder.toString() );
showOutputEdit.setText(builder.reverse().toString());
}
what should i need to change?
i think you can use something like this.(Here i convert integer to arabic)
public String convertToArabic(int value)
{
String newValue = (((((((((((value+"").replaceAll("1", "١")).replaceAll("2", "٢")).replaceAll("3", "٣")).replaceAll("4", "٤")).replaceAll("5", "٥")).replaceAll("6", "٦")).replaceAll("7", "٧")).replaceAll("8", "٨")).replaceAll("9", "٩")).replaceAll("0", "٠"));
return newValue;
}
private static String arabicToenglish(String number)
{
char[] chars = new char[number.length()];
for(int i=0;i<number.length();i++) {
char ch = number.charAt(i);
if (ch >= 0x0660 && ch <= 0x0669)
ch -= 0x0660 - '0';
else if (ch >= 0x06f0 && ch <= 0x06F9)
ch -= 0x06f0 - '0';
chars[i] = ch;
}
return new String(chars);
}
arabic chars array must be reversed
WRONG
char[] chars = {'٠','١','٢','٣','٤','٥','٦','٧','٨','٩'};
RIGHT
char[] chars = {'٩','٨','٧','٦','٥','٤','٣','٢','١','٠'};
It is just because of stackoverflow editor, but if you will copy text and paste (CTRL + C CTRL + V) it will work just fine.
so function will look like this
private static String toArabicDigits(String eng) {
char[] chars = {'٩','٨','٧','٦','٥','٤','٣','٢','١','٠'};
StringBuilder builder = new StringBuilder();
for (int i = 0; i < eng.length(); ++i) {
if (Character.isDigit(eng.charAt(i))) {
builder.append(chars[(int)(eng.charAt(i))-48]);
System.out.println("char - " + eng.charAt(i) + " " + (int)(eng.charAt(i)-48) + " - " + chars[(int)(eng.charAt(i))-48]);
} else {
builder.append(eng.charAt(i));
}
}
return builder.toString();
}
public String convertArabic(String arabicStr) {
char[] chArr = arabicStr.toCharArray();
StringBuilder sb = new StringBuilder();
for (char ch : chArr) {
if (Character.isDigit(ch)) {
sb.append(Character.getNumericValue(ch));
} else {
sb.append(ch);
}
}
return sb.toString();
}
I would like to check if the current char is a blank space as i dont want to change that. So i would like to change all the letters like i do now, but keep the spaces.
e.g. "that man" with C = 2 should become "vjcv ocp".
thanks in advance :)
String initialString = yourString.getText().toString();
char[] chars = initialString.toCharArray();
for (int i = 1; i <= chars.length; i++) {
C = Integer.valueOf(ceasarNr);
chars[i-1] = characters.get((characters.indexOf(chars[i-1]) + C)%29);
}
String resultString = new String(chars);
krypteredeTekst.setText(resultString);
}
Just skip the iteration if the character is a space:
String initialString = yourString.getText().toString();
char[] chars = initialString.toCharArray();
for (int i = 1; i <= chars.length; i++) {
//Skip if it is space.
if chars[i-1] == ' ';
continue;
C = Integer.valueOf(ceasarNr);
chars[i-1] = characters.get((characters.indexOf(chars[i-1]) + C)%29);
}
String resultString = new String(chars);
krypteredeTekst.setText(resultString);
}
There is method for checking if char is blank space ->
String initialString = yourString.getText().toString();
char[] chars = initialString.toCharArray();
for (int i = 1; i <= chars.length; i++) {
C = Integer.valueOf(ceasarNr);
if(!Character.isWhitespace(chars.charAt(i-1)) {
chars[i-1] = characters.get((characters.indexOf(chars[i-1]) + C)%29);
}
}
String resultString = new String(chars);
krypteredeTekst.setText(resultString);