So I'm working on a method in Java that basically takes a random string of letters then goes through the method and changes the string into parity bits, which is basically converting each character to its numeric value in binary.
This is what I have:
public class ShiftData {
//this is the method that where Logic is implemented..
static String shiftRows(String text) {
//character array to store given String elements into an array..
char[] chs = text.toCharArray();
StringBuffer samBuffer = new StringBuffer();
//loop through the length of the character array..
for (int i = 0; i < chs.length; i++) {
//adding characters to the StringBuffer...
samBuffer.append(Integer.toHexString((int) chs[i]));
// here in the above statement toHexString method pads parity bit and converts to hexadecimal value..
}
return samBuffer.toString();//returning the value
}
}
This is the code that converts the string into 4x4 matrices:
if(text !=null && !text.isEmpty()) {
int len = text.length();
int rem = len %16;
int padChars = 16-rem;
for(int i =0; i < (len+padChars); i++) {
if(i < len) {
System.out.print(text.charAt(i)+ " ");
} else {
System.out.print( "A ");
}
if((i+1) % 4 == 0)
System.out.println();
if((i+1) % 16 == 0)
System.out.println("\n");
}
}
So basically if the input string is: WVOGJTXQHUHXICWYYMGHTRKQHQPWKYVGLPYSPWGOINTOFOPMO
The output should look like this:
d7 56 cf 47
d4 d8 d1 ca
48 d8 48 55
59 c9 c3 d7
59 4d 47 48
d2 4b d1 d4
50 d7 48 d1
47 4b 59 56
cc 50 59 53
d7 47 cf 50
d4 cf c9 4e
4d c6 cf 50
cf 41 41 41
41 41 41 41
41 41 41 41
41 41 41 41
I just need help combining the codes! I can get them working separately but I cant get the output I need. Please show how you would code this.
Don't use StringBuffer. Use StringBuilder instead.
Your printing loop is writing one letter at a time, separated by 3 spaces (and newlines). Letters in hex consist of two hex digits, as you already show in the desired output, so that won't work.
Your code prints blank lines at the end, which you probably don't want.
Integer.toHexString() will return a single digits if value is 0-15.
static String shiftRows(String text) {
char[] chs = text.toCharArray();
StringBuilder samBuffer = new StringBuilder();
for (int i = 0; i < chs.length; i++)
samBuffer.append(String.format("%02x", (int)chs[i])); // always 2 hex digits, even for 0-15
return samBuffer.toString();
}
public static void main(String[] args) {
String text = shiftRows("WVOGJTXQHUHXICWYYMGHTRKQHQPWKYVGLPYSPWGOINTOFOPMO");
if (text != null && ! text.isEmpty()) {
int len = (text.length() + 31) / 32 * 32; // round up to next increment of 32 (16 hex pairs)
for (int i = 0; i < len; i += 2) {
if (i != 0 && i % 8 == 0) { // every 4 hex pairs, but not first time
System.out.println();
if (i % 32 == 0) // every 16 hex pairs
System.out.println();
}
if (i < text.length())
System.out.print(text.substring(i, i + 2) + " ");
else
System.out.print("41 ");
}
}
}
I try to program a simple game (but my question is not so game specific) so i wrote a little MapLoader. At the moment it only reads the File Header and prints it on the Screen.
Info:
Signature is a byte
Version is an int
length is a long
name is a 32 byte long String
Also, all these are signed as there is no unsigned in Java.
However i have a function toString(byte[] array). Its implemented like this:
private String toString(byte[] array) {
char[] buffer = new char[array.length];
StringBuilder builder = new StringBuilder(array.length);
for(int i = 0; i < array.length; i++) {
buffer[i] = (char) array[i];
builder.append(buffer[i]);
}
return builder.toString();
}
I pass in an sub-array of data[] (holding all data)
header.name = toString(Arrays.copyOfRange(data, 11, 32));
But when doing
Debugger.debug("Signatur: " + header.signature);
Debugger.debug("Version: " + header.version);
Debugger.debug("Länge: " + header.length);
Debugger.debug("Name: " + header.name);
I only get:
Signatur: 86
Version: 1
Länge: 14
Name:
My Map File looks like this:
56 00 00 00 01 00 00 00 00 00 00 00 0E 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 0A
Use one of the java.lang.String constructors that takes a byte array as a parameter, rather than rolling your own. The String implementation deals properly with all the character set issues.
I'm having quite a lot of issues when it comes to parsing through a stringBuffer into a 2D Array.
This is what I am trying to achieve....
This is an example of one of my files.
123456789
123456789
123456789
123456789
123456789
123456789
123456789
123456789
123456789
and if I was just to manually write it into Java I would get...
int[][] board = {
{1,2,3,4,5,6,7,8,9},
{1,2,3,4,5,6,7,8,9},
{1,2,3,4,5,6,7,8,9},
{1,2,3,4,5,6,7,8,9},
{1,2,3,4,5,6,7,8,9},
{1,2,3,4,5,6,7,8,9},
{1,2,3,4,5,6,7,8,9},
{1,2,3,4,5,6,7,8,9},
{1,2,3,4,5,6,7,8,9},
};
But I'm trying to create a method which does it for us, seeing as I'm loading in many different files.
This is my code.
int[][] board = new int[9][9];
File file = new File ("keiron.sud");
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
StringBuffer stringBuffer = new StringBuffer();
String newline;
while ((newline = bufferedReader.readLine()) != null) {
stringBuffer.append(newline);
stringBuffer.append("\n");
}
fileReader.close();
String data = stringBuffer.toString();
int counting = data.length();
int counter = 0;
int i,j;
for(i = 0; i<9; i++) {
for (j =0; j < 9; j++) {
int numbers = data.charAt(counter);
counter++;
board[i][j] = numbers;
}
}
Print(board);
And this is the output I'm getting
49 50 51 52 53 54 55 56 57
10 49 50 51 52 53 54 55 56
57 10 49 50 51 52 53 54 55
56 57 10 49 50 51 52 53 54
55 56 57 10 49 50 51 52 53
54 55 56 57 10 49 50 51 52
53 54 55 56 57 10 49 50 51
52 53 54 55 56 57 10 49 50
51 52 53 54 55 56 57 10 49
Which really doesn't make sense!
I'm fairly new to Java, so please can you explain things like you would to a 5 year old!
Thank you,
CodeMonkey
Since you're doing this
int numbers = data.charAt(counter); // char to int
you're getting the ASCII values of the numbers.
On the LHS you've an int and on the RHS you've a method charAt() which returns a char. Therefore, the ASCII value of the char on the RHS is being assigned to the int on the LHS.
To get the actual numeric value, use this
int numbers = Character.getNumericValue(data.charAt(counter)); // fetches the numeric value
You'll get a -1 in between, because you're adding a \n manually within your string. Remove that and it won't show that -1.
// stringBuffer.append("\n"); // This is unnecessary. Comment it out.
Replace int numbers = data.charAt(counter); to int numbers = data.charAt(j)-'0';
and consider remove the space in data
String data = stringBuffer.toString().trim();
data = data.replaceAll("\\s+", " ");
I tried to Google and search StackOverflow my question but I didn't found any answers to this.
I have made an array where both size and values are randomly generated. When the array values have been printed 20 times I would like to make a line break, but without printing the rest values with always new line.
Here is my code:
public static void catArr() {
Random rändöm = new Random();
int size = rändöm.nextInt(100);
int[] arr = new int[size];
for (int i = 0; i < size; i++) {
arr[i] = rändöm.nextInt(100);
}
Arrays.sort(arr);
for (int i = 0; i < size; i++) {
System.out.print(" " + arr[i]);
if (i > 20)
System.out.println(); //How to do this only once?
else if (i > 40)
System.out.println(); //Same here?
}
}
And this is one of the generated outputs:
3 8 10 25 30 31 34 38 46 50 55 59 62 66 67 68 68 68 72 76 76 81
82
83
84
86
91
92
93
94
94
97
I think that one way to solve this is using 2-D array, but I would like to know if there is another way.
Yay thanks to Patric, I got the wanted result:
0 2 3 7 7 9 11 14 14 16 18 19 24 25 26 28 28 30 30 31
31 33 33 34 41 41 41 42 43 44 45 46 51 51 52 53 59 60 61 62
62 62 63 65 65 67 67 68 69 70 74 74 76 78 82 83 84 84 87 88
89 93 93 94 94 94 95
try using
if ( ( i % 20 ) == 0 ){
System.out.println();
}
if i divided by 20 leaves no remainder then print a new line!
Maybe
if (i % 20==0)
can solve your else if problem.
Use (++i % 20) == 0 and remove i++ from loop; pre-increment avoid first unwanted line break.
Literally, this will do what you seem to want:
if (i == 20)
System.out.println();
else if (i == 40)
System.out.println();
But I have a feeling that you actually want to add a newline after the 20th, 40th, 60th and so on.
if (i % 20 == 0)
System.out.println();
And if you want to output exactly one newline at the end, then you need something like this:
for (int i = 0; i < size; i++) {
if (i > 1 && i % 20 == 1) {
System.out.println();
System.out.print(" " + arr[i]);
}
System.out.println();
You may use boolean for your Sys outs.
boolean myBoolean = true;
if(myBoolean){
//print
myBoolean = false; //set boolean to false.
}
On the other hand, in my preferences, I still stick with my integer flagging.
int isTrue = 1;
if(isTrue == 1){
//print
isTrue = 0;
}
I am working on a hex file and display its content but if the value had "0". It did not appear when i print it out.
for example
0 0 0 b7 7a 7a e5 db 40 2 0 c0 0 0 9 18 16 0 e3 1 40 0 0 3f 20 f0 1 5 0 0 0 0 0 0 41 bc 7a e5 db 40 2 0 c0 1 0 9 18 16 0 e3 1 40 0 0 3f 20 f0 1 5 0 0 0 0 0 0 53 3f 7b e5 db 40 2 0 c0 3 0 9 2 19 24 3d 0 22 68 1 db 9
Code
String filename = "C:\\tm09888.123";
FileInputStream in = null;
int readHexFile = 0;
char hexToChar = ' ';
String[] bytes = new String[10];
try
{
in = new FileInputStream(filename);
while((readHexFile = in.read()) != -1)
{
if (Integer.toHexString(readHexFile).equals("f0"))
{
System.out.print("\n\n\n");
}
System.out.print(Integer.toHexString(readHexFile) + " ");
}
}
catch (IOException ex)
{
Logger.getLogger(NARSSTest.class.getName()).log(Level.SEVERE, null, ex);
}
}
When I printed out the file the "0"s did not appear and values such as "c0" became "c".
How I do i rewrite the code to display the "0"s?
Integer.toHexString doesn't guarantee returning a two-digit result.
If you want it to always be two digits, you could use String.format instead:
System.out.print(String.format("%02x ", readHexFile));
When display it on the screen "0" values did not appear and value like "c0" become only "c"
I suspect it's more likely that "0c" becomes just "c". I'd expect "c0" to be fine.
The problem is that you're using Integer.toHexString which will only use as many digits as it needs to. You could manually fix this by writing:
if (readHexFile < 0x10) {
System.out.print("0");
}
Alternatively, just use:
private static final char[] HEX_DIGITS = "0123456789abcdef".toCharArray();
...
System.out.print(HEX_DIGITS[readHexFile >> 4]);
System.out.print(HEX_DIGITS[readHexFile % 15]);
System.out.print(" ");
Or even more simply:
System.out.printf("%02x ", readHexFile);
Also note that there's no need to convert to a hex string to compare with 0xf0. You can use:
if (readHexFile == 0xf0) {
System.out.print("\n\n\n");
}
I cannot say what the problem is with the code, but it seems things would be much clearer if you used Scanner
Scanner sc = new Scanner(new File(fileName));
while(sc.hasNext()) {
String s = sc.next();
System.out.println(s);
}