Hello fellow StackOverflowers, today I pose a strange question.
As all of you know, there are different ways in which to count (in terms of bases)
For base 2 (a.k.a binary), one would count as follows
0000 = (2^3 x 0) + (2^2 x 0) + (2^1 x 0) + (2^0 x 0) = 0
0001 = (2^3 x 0) + (2^2 x 0) + (2^1 x 0) + (2^0 x 1) = 1
0010 = (2^3 x 0) + (2^2 x 0) + (2^1 x 1) + (2^0 x 0) = 2
0011 = (2^3 x 0) + (2^2 x 0) + (2^1 x 1) + (2^0 x 1) = 3
...and so on...
When we reach bases higher than 10, such as 16 (a.k.a hexadecimal) we use letters to represent values:
For example:
200 base 16 = C8 ---> http://www.binaryhexconverter.com/decimal-to-hex-converter (if you dont believe me ;))
However, how will we count in bases higher than letters allow? (base 37+)
I wrote a simple java program to illustrate my point:
public class Testing {
public static void main(String[] args) {
for (int base = 1;base<=50;base++){
System.out.println("===========================Base " + base + "===============================");
for (int value=1; value<=50; value++){
System.out.println(value + " base " + base + " is equal to: " + Integer.toString(value, base));
}
}
}
}
Here are some snippets of the output of my program:
40 base 36 is equal to: 14
41 base 36 is equal to: 15
42 base 36 is equal to: 16
43 base 36 is equal to: 17
44 base 36 is equal to: 18
45 base 36 is equal to: 19
46 base 36 is equal to: 1a
47 base 36 is equal to: 1b
48 base 36 is equal to: 1c
49 base 36 is equal to: 1d
50 base 36 is equal to: 1e
28 base 37 is equal to: 28
29 base 37 is equal to: 29
30 base 37 is equal to: 30
31 base 37 is equal to: 31
32 base 37 is equal to: 32
33 base 37 is equal to: 33
34 base 37 is equal to: 34
35 base 37 is equal to: 35
36 base 37 is equal to: 36
37 base 37 is equal to: 37
38 base 37 is equal to: 38
39 base 37 is equal to: 39
40 base 37 is equal to: 40
41 base 37 is equal to: 41
42 base 37 is equal to: 42
43 base 37 is equal to: 43
44 base 37 is equal to: 44
45 base 37 is equal to: 45
46 base 37 is equal to: 46
47 base 37 is equal to: 47
48 base 37 is equal to: 48
49 base 37 is equal to: 49
50 base 37 is equal to: 50
As you can see, base 37+ doesn't work.
Is there anyway to fix this? Thank you!
Character.MAX_RADIX is equal to 36 (10 digits and 26 letters).
If you want to use higher values for radix, you will have to write your own method. This should not be too hard. I suggest modifying the source code for Integer.toString(int, int). The code for this method uses this array:
final static char[] digits = {
'0' , '1' , '2' , '3' , '4' , '5' ,
'6' , '7' , '8' , '9' , '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'
};
Try this.
public static String DIGITS = "0123456789abcdefghijklmnopqrstuvwxyz#!#$%&;:";
public static String toString(int i, int base) {
StringBuilder sb = new StringBuilder();
for (; i > 0; i /= base)
sb.append(DIGITS.charAt(i % base));
if (sb.length() == 0)
sb.append(0);
return sb.reverse().toString();
}
Related
Solution.java:30: error: illegal character: '\u202c'
if(x>=-2147483648 && x<=2147483647)
^
Solution.java:30: error: not a statement
if(x>=-2147483648 && x<=2147483647)
^
Solution.java:30: error: ';' expected
if(x>=-2147483648 && x<=2147483647)
^
Solution.java:34: error: illegal character: '\u202c'
if(x>=-(pow(2,61)) && x<=pow(2,61)-1)
^
Solution.java:34: error: not a statement
if(x>=-(pow(2,61)) && x<=pow(2,61)-1)
^
Solution.java:34: error: ';' expected
if(x>=-(pow(2,61)) && x<=pow(2,61)-1)
^
6 errors
As the comments have pointed out, there is an extra character in there before the first space before the first ampersand. Here is how you could use java to tell.
public static void printStringDetails(String s) {
for (int i = 0; i < s.length(); i++) {
System.out.println(s.charAt(i) + " " + (int)(s.charAt(i)));
}
}
public static void main(String args[]) {
String s = "if(x>=-2147483648 && x<=2147483647)";
printStringDetails(s);
s = "if(x>=-(pow(2,61)) && x<=pow(2,61)-1)";
printStringDetails(s);
}
where I pasted your strings in.
Output is:
i 105
f 102
( 40
x 120
> 62
= 61
- 45
2 50
1 49
4 52
7 55
4 52
8 56
3 51
6 54
4 52
8 56
8236
32
& 38
& 38
32
x 120
< 60
= 61
2 50
1 49
4 52
7 55
4 52
8 56
3 51
6 54
4 52
7 55
) 41
i 105
f 102
( 40
x 120
> 62
= 61
- 45
( 40
p 112
o 111
w 119
( 40
2 50
, 44
6 54
1 49
) 41
) 41
8236
32
& 38
& 38
32
x 120
< 60
= 61
p 112
o 111
w 119
( 40
2 50
, 44
6 54
1 49
) 41
- 45
1 49
) 41
Notice the two occurrences of 8236 before the space (ASCII 32) before the first ampersand in each line.
Then you can go back in and edit so that the output is what you expect. Thus:
if(x>=-2147483648 && x<=2147483647)
(paste that between the quotes and you will see I deleted the unwanted character)
and
if(x>=-(pow(2,61)) && x<=pow(2,61)-1)
should fix it.
Both statements are valid:
char c1 = 'a';
char c2 = 97;
Now, if I add another char variable, lets say:
char c3 = 10;
And I sum c2 and c3 together like so:
int sum = c2 + c3
I do not get compile error and why is that?
And why would I need to ever sum two chars?
Thanks
Each character represented by the type 'char' in java is mapped to a number in the ASCII table. When you add the values you are adding the operands numeric equivalent (the char types are cast as int) so x (120 ascii val) - P(80 ascii val) would give integer value 40.
Dec Char Dec Char Dec Char Dec Char
--------- --------- --------- ----------
0 NUL (null) 32 SPACE 64 # 96 `
1 SOH (start of heading) 33 ! 65 A 97 a
2 STX (start of text) 34 " 66 B 98 b
3 ETX (end of text) 35 # 67 C 99 c
4 EOT (end of transmission) 36 $ 68 D 100 d
5 ENQ (enquiry) 37 % 69 E 101 e
6 ACK (acknowledge) 38 & 70 F 102 f
7 BEL (bell) 39 ' 71 G 103 g
8 BS (backspace) 40 ( 72 H 104 h
9 TAB (horizontal tab) 41 ) 73 I 105 i
10 LF (NL line feed, new line) 42 * 74 J 106 j
11 VT (vertical tab) 43 + 75 K 107 k
12 FF (NP form feed, new page) 44 , 76 L 108 l
13 CR (carriage return) 45 - 77 M 109 m
14 SO (shift out) 46 . 78 N 110 n
15 SI (shift in) 47 / 79 O 111 o
16 DLE (data link escape) 48 0 80 P 112 p
17 DC1 (device control 1) 49 1 81 Q 113 q
18 DC2 (device control 2) 50 2 82 R 114 r
19 DC3 (device control 3) 51 3 83 S 115 s
20 DC4 (device control 4) 52 4 84 T 116 t
21 NAK (negative acknowledge) 53 5 85 U 117 u
22 SYN (synchronous idle) 54 6 86 V 118 v
23 ETB (end of trans. block) 55 7 87 W 119 w
24 CAN (cancel) 56 8 88 X 120 x
25 EM (end of medium) 57 9 89 Y 121 y
26 SUB (substitute) 58 : 90 Z 122 z
27 ESC (escape) 59 ; 91 [ 123 {
28 FS (file separator) 60 < 92 \ 124 |
29 GS (group separator) 61 = 93 ] 125 }
30 RS (record separator) 62 > 94 ^ 126 ~
31 US (unit separator) 63 ? 95 _ 127 DEL
https://www.cs.cmu.edu/~pattis/15-1XX/common/handouts/ascii.html
This prints numbers 32-128 and the associated char:
for (int c=32; c<128; c++)
{
System.out.println("Ascii #: " + c + " Char" + ": " + (char)c);
}
I want to print "Two" in the place of 2 and "Four" in the place of 4 in Java within a FOR Loop while printing the numbers from 1 to 50.
For example:
1
Two
3
Four
5
.
.
.
1Four
15
.
.
.
Two1
TwoTwo
Two3
TwoFour
.
.
.
50
Java 8 solution:
public class Play {
public static void main(String[] args) {
rangeClosed(1, 50).forEach(Play::twoOrFour);
}
public static void twoOrFour(long n) {
String result = n + "";
if (n % 10 == 2) {
n /= 10;
result = (n == 0 ? "" : n) + "two"; // the ternary exp: an ugly patch to get rid of the "0" in the first two cases
} else if (n % 10 == 4) {
n /= 10;
result = (n == 0 ? "" : n) + "four";
}
System.out.print(result + " ");
}
}
OUTPUT
1 two 3 four 5 6 7 8 9 10 11 1two 13 1four 15 16 17 18 19 20 21 2two 23 2four 25 26 27 28 29 30 31 3two 33 3four 35 36 37 38 39 40 41 4two 43 4four 45 46 47 48 49 50
UPDATE
In case you want to replace any occurrence of "2" with "two" and "4" with "four" the referenced method can be even simpler:
public static void twoOrFour(long n) {
String result = n + "";
result = result.replaceAll("2", "two").replaceAll("4", "four");
System.out.print(result + " ");
}
which will output:
1 two 3 four 5 6 7 8 9 10 11 1two 13 1four 15 16 17 18 19 two0 two1 twotwo two3 twofour two5 two6 two7 two8 two9 30 31 3two 33 3four 35 36 37 38 39 four0 four1 fourtwo four3 fourfour four5 four6 four7 four8 four9 50
Or if to be even funkier - it can be done in a one-liner:
rangeClosed(1, 50).forEach((x)-> System.out.print((x + " ").replaceAll("2", "two").replaceAll("4", "four")));
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);
}
I was wondering if someone could help me with a small problem I'm having.
I am trying to print out an array of 100 items, every row needs 10 items, after which it starts a new row. I have done this successfully, however, every row in every column displays the same number. For example. My output would be:
1 1 1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2 2 2
When it should be appearing as something along the lines of:
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
Here is the following method I am using to print the table:
public static void printTable(int[] emirps) {
for (int i = 0; i < 100; i++) {
for (int j = 0; j < 10; j++) {
// After 10 go to a new line
System.out.printf("%d\t", emirps[i]);
}
System.out.println("");
}
}
If anyone can help me pinpoint where I have goofed I'd greatly appreciate it.
You are printing the same emirps[i] each time through the inner loop. There are a couple of ways around this. Here's one:
public static void printTable(int[] emirps) {
for (int i = 0; i < 100; i += 10) {
for (int j = 0; j < 10; j++) {
// After 10 go to a new line
System.out.printf("%d\t", emirps[i + j]);
}
System.out.println();
}
}
Here's another (not very elegant):
public static void printTable(int[] emirps) {
for (int i = 0; i < 100; ++i) {
System.out.printf("%d\t", emirps[i]);
if (i % 10 == 9) {
System.out.println();
}
}
}
This looks like homework, so instead of giving you the answer, here's a hint:
For the first iteration of the first loop, i = 0. When this is the case, you increment j from 0 to 9. Note that i stays the same throughout. This is when you print your first line.
Once you're through with the second for loop, i increases by 1 to, and now i = 1. Again, you increment j from 0 to 9, and i stays at 1. This is when you print your second line.
This repeats 100 times, as i goes from 0 to 99.
There are 2 ways of approaching this:
Use one for loop, and figure out when to print an end-of-line character.
Use two for loops, each going up to 10. Figure out your index by simple multiplication and addition.
You should try and implement both methods. It'll help you understand loops better.
You are not using the j index anywhere in this code.
Here is how you should print:
System.out.printf("%d\t", emirps[i+j]);
Notice how in the print statement you are now using both the i and j indices.
Also, to get your row offset working correctly, you should be incrementing i by 10 at each step of the loop.
Alternatively, declare emirps as a 2-dimensional array. Then the signature of printTable() would be:
public static void printTable(int[][] emirps)
In that setup, here is how you would print:
System.out.printf("%d\t", emirps[i][j]);
So in the first setup, i+j together index into a 1-D array. In the second setup, i is the row and j is the column, indexing together into a 2-D array. It looks like you're doing a 1-D array but this other example is for your future reference.
Use the modulo operator. Every time your number can be divided by 10 without a remainder, you can make a new line because you have filled up the row. You obtain the remainder using the 'modulo operator' which is the percent (%) sign. It can be used like /, except that rather than the quotient being the result, the remainder of the division operation is the result.
public static void main(String[] args)
{
StringBuilder builder = new StringBuilder();
for (int i = 0; i < 100; i++)
{
if (i != 0 && i % 10 == 0)
{
builder.append('\n');
}
builder.append(i + " ");
}
System.out.println(builder.toString());
}
Prints out:
0 1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18 19
20 21 22 23 24 25 26 27 28 29
30 31 32 33 34 35 36 37 38 39
40 41 42 43 44 45 46 47 48 49
50 51 52 53 54 55 56 57 58 59
60 61 62 63 64 65 66 67 68 69
70 71 72 73 74 75 76 77 78 79
80 81 82 83 84 85 86 87 88 89
90 91 92 93 94 95 96 97 98 99