What char variable really is? - java

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);
}

Related

How do you format and read randomly generated numbers in the console?

I've created a loop and random number generator that generates 100 numbers within the range of 1-100. I need to format these numbers so that it is 10 per line. I tried using printf and had a hard time. In addition, I have to find the average of all these numbers. The issue is I am unsure of how to do this because all the numbers are under the int variable 'randoms'. I can't add a single variable together and divide by 100.
public static void main(String[] args) {
Random rand = new Random();
int n = 100;
for (int i=1; i<=n; i++) {
int randoms = rand.nextInt(101);
}
}
You may print each number without a new line, and with spaces before to pad at 4-length string, and each 10 values, print a new line. For the average, use math : sum/count
Random rand = new Random();
int n = 100;
int total = 0;
for (int i = 1; i <= n; i++) {
int randoms = rand.nextInt(101);
total += randoms;
System.out.format("%4d", randoms);
if (i % 10 == 0) {
System.out.println();
}
}
System.out.println("AVG " + total / (double) n);
49 55 89 26 88 58 80 98 62 8
34 65 9 3 28 71 30 11 50 50
18 90 61 62 18 93 83 83 57 14
9 54 49 6 24 28 60 8 86 83
60 6 17 67 49 89 66 13 65 50
70 24 3 90 89 4 47 49 48 7
16 38 79 59 51 9 22 81 8 84
52 30 64 97 42 100 30 26 66 44
22 46 16 100 73 100 56 63 8 48
50 88 55 93 6 82 65 46 44 7
AVG 49.29

i am getting this error while using && in if

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.

How to fix this code for Java to rotate an array for large arrays and large number of rotations?

The following code is for rotation of an integer array.
Takes user input for array size and then an input for the element upto which the array is to be rotated.
Doesn't work for large values of array size and the rotation element.
The following code works for 15 array elements and rotates by 11 numbers (I didn't try for higher numbers) but it fails for a 77 array element and for rotation by 69 numbers.
It works with both sorted and unsorted array.
Why is it not working and how can I fix this?
int dupRotate = D;
for(int j=N-1; j>=0; --j) {
/* checks whether loop is to be stopped or not.
bottom condition fails to stop the 1st iteration for
third loop run.
*/
if(dupRotate == 0) {
break;
}
/* Exchanges elements here.
for loop run1 -
arr = 1,2,3,4,5
after j reaches index 1
arr = 2,3,4,5,1.
for loop run2 -
arr = 2,3,4,5,1.
after j reaches index 1
arr = 3,4,5,1,2.
*/
int temp = arr[0];
arr[0] = arr[j];
arr[j] = temp;
/* if j reaches 1
decrements dupRotate and sets j = N
then for loop decrements j and starts loop again.
*/
if(j == 1 && dupRotate > 0) {
dupRotate--;
j = N;
}
}
77 69
40 13 27 87 95 40 96 71 35 79 68 2 98 3 18 93 53 57 2 81 87 42 66 90 45 20 41 30 32 18 98 72 82 76 10 28 68 57 98 54 87 66 7 84 20 25 29 72 33 30 4 20 71 69 9 16 41 50 97 24 19 46 47 52 22 56 80 89 65 29 42 51 94 1 35 65 25
Its Correct output is:
29 42 51 94 1 35 65 25 40 13 27 87 95 40 96 71 35 79 68 2 98 3 18 93 53 57 2 81 87 42 66 90 45 20 41 30 32 18 98 72 82 76 10 28 68 57 98 54 87 66 7 84 20 25 29 72 33 30 4 20 71 69 9 16 41 50 97 24 19 46 47 52 22 56 80 89 65
And Your Code's output is:
45 20 41 30 32 18 98 72 82 76 10 28 68 57 98 54 87 66 7 84 20 25 29 72 33 30 4 20 71 69 9 16 41 50 97 24 19 46 47 52 22 56 80 89 65 29 42 51 94 1 35 65 69 40 13 27 87 95 40 96 71 35 79 68 2 98 3 18 93 53 57 2 81 87 42 66 90
It's pretty hard to realise your problem. I have to add more description.
I think, if you use the correct algorithm of array rotation, then it does not depend on an array's length. This is one of the most efficient algorithm:
[1,2,3,4,5] -> k = 2 -> [4,5,1,2,3]
-----------------------------------
1. [1,2,3,4,5] -> [5,4,3,2,1]
2. [5,4,3,2,1] -> [4,5,3,2,1]
3. [4,5,3,2,1] -> [4,5,1,2,3]
public static void rotate(int[] arr, int k) {
if ((k %= arr.length) != 0) {
k = k < 0 ? arr.length + k : k;
swapSubArr(arr, 0, arr.length); // 1.
swapSubArr(arr, 0, arr.length - k); // 2.
swapSubArr(arr, arr.length - k, arr.length); // 3.
}
}
private static void swapSubArr(int[] arr, int from, int to) {
for (int i = from, j = to - 1; i < j; i++, j--)
swap(arr, i, j);
}
private static void swap(int[] arr, int i, int j) {
int tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
Move step by step
[1,2,3,4,5] -> k = 3 -> [4,5,1,2,3]
-----------------------------------
1. [1,2,3,4,5] -> [5,4,3,2,1]
2. [5,4,3,2,1] -> [4,5,3,2,1]
3. [4,5,3,2,1] -> [4,5,1,2,3]
Code
static void rotate(int arr[], int k) {
for (int i = 1; i <= k; i++) {
move(arr);
}
}
static void move(int arr[]) {
int i, n, tmp;
n = arr.length;
tmp = arr[0];
for (i = 0; i < n - 1; i++) {
arr[i] = arr[i + 1];
}
arr[i] = tmp;
}
public static void main(String[] args) {
int arr[] = { 1, 2, 3, 4, 5 };
int k = 3;
// [1, 2, 3, 4, 5]
rotate(arr, k);
// [4, 5, 1, 2, 3]
}

Regex on Windows vs Mac

I have a string that I am splitting using Java. It works perfectly fine on my Windows machine, but not on my Mac (using Eclipse)
I have a string of text that contains single spaces and double spaces like so:
August 05   July 09   May 07   April 01   March 19   February 19   January 15   December 17   December 04   December 03   December 02   November 24   October 30
I am splitting using
String monthAndDay[] = formattedNewsDates.split("\\s\\s");
Again, not sure why it is working on Windows but not Mac. Does one platform understand regular expressions a different way?
Since you're using Eclipse, you can debug the code and look at the char[] that's backing the String.
Alternately, you can print the string in hex. Here is a quick and (very) dirty hex printer:
String s = " August 05 July 09 May 07 April 01 March 19 February 19 January 15 December 17 December 04 December 03 December 02 November 24 October 30";
for (byte b : s.getBytes("UTF-8"))
System.out.printf("%02x ", b);
System.out.println();
for (byte b : s.getBytes("UTF-8"))
System.out.print(b >= 32 ? (char)b + " " : "? ");
System.out.println();
Output
20 41 75 67 75 73 74 20 30 35 20 20 20 4a 75 6c 79 20 30 39 20 20 20 4d 61 79 20 30 37 20 20 20 41 70 72 69 6c 20 30 31 20 20 20 4d 61 72 63 68 20 31 39 20 20 20 46 65 62 72 75 61 72 79 20 31 39 20 20 20 4a 61 6e 75 61 72 79 20 31 35 20 20 20 44 65 63 65 6d 62 65 72 20 31 37 20 20 20 44 65 63 65 6d 62 65 72 20 30 34 20 20 20 44 65 63 65 6d 62 65 72 20 30 33 20 20 20 44 65 63 65 6d 62 65 72 20 30 32 20 20 20 4e 6f 76 65 6d 62 65 72 20 32 34 20 20 20 4f 63 74 6f 62 65 72 20 33 30
A u g u s t 0 5 J u l y 0 9 M a y 0 7 A p r i l 0 1 M a r c h 1 9 F e b r u a r y 1 9 J a n u a r y 1 5 D e c e m b e r 1 7 D e c e m b e r 0 4 D e c e m b e r 0 3 D e c e m b e r 0 2 N o v e m b e r 2 4 O c t o b e r 3 0
As you can see, the spaces are hex 20. Hex values 00 to 1F are control characters, like CR and LF. If your string has non-ASCII characters, then each character is printed as 2-4 bytes with hex values of 80 or higher.

How to print numbers in right-aligned manner? [duplicate]

This question already has answers here:
Align printf output in Java
(5 answers)
Closed 8 years ago.
So I am trying once of the codeeval's easy problems for multiplication tables
One of the requirement is
(The numbers are right-aligned and strip out leading/trailing spaces
on each line)
I am not sure about how to do that, my current code looks like
private static void printTable(final int numberOfTables, final int numberOfTimes) {
for (int i = 1; i <= NUMBER_OF_TABLES; i++) {
final StringBuilder sb = new StringBuilder();
for (int j = 1; j <= NUMBER_OF_TIMES; j++) {
sb.append(i * j).append(" ");
}
System.out.println(sb.substring(0, sb.length() - 4));
}
}
and what I get back is
1 2 3 4 5 6 7 8 9 10 11 12
2 4 6 8 10 12 14 16 18 20 22 24
3 6 9 12 15 18 21 24 27 30 33 36
4 8 12 16 20 24 28 32 36 40 44 48
5 10 15 20 25 30 35 40 45 50 55 60
6 12 18 24 30 36 42 48 54 60 66 72
7 14 21 28 35 42 49 56 63 70 77 84
8 16 24 32 40 48 56 64 72 80 88 96
9 18 27 36 45 54 63 72 81 90 99 108
10 20 30 40 50 60 70 80 90 100 110 120
11 22 33 44 55 66 77 88 99 110 121 132
12 24 36 48 60 72 84 96 108 120 132 144
How do I right-align numbers?
As you said in your question title, you use printf(). Something like,
for (int i = 1; i <= NUMBER_OF_TABLES; i++) {
for (int j = 1; j <= NUMBER_OF_TIMES; j++) {
System.out.printf("%6d", i * j);
}
System.out.println();
}
Where 6 is the width and the Javadoc for Formatter describes width as
Width
The width is the minimum number of characters to be written to the output. For the line separator conversion, width is not applicable; if it is provided, an exception will be thrown.
And
'd' ... Formats the argument as a decimal integer.
To print a number as right aligned you could use
System.out.printf("%nd", x);
where n is minimum width and x is the integer to be print.
Now for your questions answer you could use below code
for(int i=1; i<= NUMBER_OF_TABLES; i++)
{
int j=1;
System.out.print(i*j++);
for(; j<= NUMBER_OF_TIMES; j++)
{
System.out.printf("%4d",j*i);
}
System.out.println("");
}

Categories