This question already has answers here:
how to use NOT operator for integers in JAVA
(2 answers)
Closed 5 years ago.
I was working on a program in java where I was creating a truth table of a full subtractor using a 2 dimensional array. While doing so, I wanted to compute the borrow out with the following formula:
B(out) = !(X).Y + (!(X ^ Y))B(in)
So, I wrote it like:
table[i][4] = ((!(table[i][2]))&table[i][1])+((!(table[i][2]^table[i][1]))*table[i][0]);
(here: table[i][4] = cell to store borrow out;
table[i][2] = cell storing X;
table[i][1] = cell storing Y;
table[i][0] = cell storing borrow in B(in) )
Whereas XOR(^) and AND(&) and or(|) worked fine, I got the following error for NOT(!):
operator ! cannot be applied to int
How can I correct this? Is there any other way to write this formula as a JAVA code? Please help.
The bitwise complement operator that can be applied to integer values is the ~, not !
Related
This question already has answers here:
Java output formatting for Strings
(6 answers)
Closed 11 months ago.
Given these variables:
int a = 1, b = 123, c = 55, d= 1231;
Is there a way in Java to print them with a set width of 5, say. In case number is less than five digits - only print dashes.
1----,123--,55---,1231-
I am aware that these can be achieved with some loops and if statements, looking for something similar to setw() from C++
System.out.println(String.format("%-5.5s", s).replace(" ", "-"));
In short, you can't do it directly. Java has functionality broadly similar to that of C's "printf" formatting.
You can set a field width, you can justify left or right, but your fill characters are limited to zero and space.
Documentation
If the format uses the general "%s" directive, and the corresponding argument is of a class under your control, then you can implement a 'formatTo' method to do the conversion. So a wrapper class might be useful to you.
This question already has answers here:
What is float in Java?
(4 answers)
Closed 7 years ago.
case 1
float a=033.0 //shows compilation problem
case 2
double a=033.0 //works fine
Why case 1 is showing error but not case 2 or vice-versa?
By default java uses double type to store floating values. since you are storing double in float (down casting) java will throw an error. it can be resolved by two ways
float a=033.0f
float a= (float)033.0
case 1----float a=033.0 //shows compilation problem
case 2----double a=033.0 //works fine
In Java, decimal number is interpreted as a double ,so converting from double to float cannot be performed automatically ,so you need to give like this :
float a= 033.0f;
Its simply the understanding of Java Syntax.
You can read the Primitive data types of java.
You'll get it anywhere...
Link : https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
Direct to this link, do ctrl+F, & paste this "Floating-Point Literals".
You wont't waste time wandering...
For your own convenience now, you can prefer this.
float fractionNumber = 25.24F;
fractionNumber = 25.24f;
double biggerFractionNum = 56.65555D;
biggerFractionNum = 56.65555;
but generally its like this all over... later you'll get used to it.
float foo = 34.4F;
double doo = 34.4;
IMPLEMENT it right away ! ...experience it, understand it...& you'll never forget it. :)
This question already has answers here:
Assigning variables with dynamic names in Java
(7 answers)
Closed 8 years ago.
An example:
int x1 = 1;
int x2 = 2;
int x3 = 3;
...
int xn = n;
As you can see the name and value of the variables are generated - in this case by a counter. Is this possible?How?
I do not know how, but this is probably possible with some crazy Reflection hacking - do not try to do it, as it will produce code that is hard to maintain, hard to understand and vulnerable to hard-to-trace bugs.
Instead use Collections (do not use arrays if not absolutely needed), for example ArrayList.
Yes, it is valid Java code, but in this particular case you would just use the numbers :D
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I compare strings in Java?
im new to android and this is my first post in StackOverflow.In short I face a very strange problem :
in some methods I used if(...) ,and both of the values were equal ,yet it doesn't go through the if .
Here is an example :
String []s=db.getStudentsNames();
String []t=CopyNames(s);
String t1,t2;
t2=Id.getText().toString();
for(int i=0;i<s.length;i++)
{
t1=t[i].substring(t[i].indexOf("-")+1).toString();
Notifications(t[i].substring(t[i].indexOf("-")+1).toString());
if(t1.toString()==t2.toString())//Problem!
{
Notifications("Id already exists for "+t[i].substring(0,t[i].indexOf("-")).toString());
return false;
}
}
The variables t1 & t2 are : t1="123456789" & t2="123456789" , yet it doesn't enter the if like they are not equal.
And there are other places were two equal sides are considered as not equal - in the same java page (activity) like:
if(add.getText().toString()=="Add Student") : add refers to a button which has ,by default , a text :"Add Student"
so how can I solve this problem ?
**when this problem started , I started to see in LogCat:
W/KeyCharacterMap(282): No keyboard for id 0
W/KeyCharacterMap(282): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
D/dalvikvm(282): GC_FOR_MALLOC freed 5438 objects / 256800 bytes in 73ms
You shouldn't use == to compare Strings in Java. Use t1.toString().equals(t2.toString()) instead.
never use == on String objects. Use t1.equals(t2) instead.
This question already has answers here:
How do you test to see if a double is equal to NaN?
(7 answers)
Closed 8 years ago.
I am collecting some data from a database and adding them together to get some statistics, but since I backdate some of my data then the calculated sum will sometime come up as NaN (not a number) I want to create an if sentence that says if(not a number) then exclude this data from my table.
How do I test if the data (in this case double) is NaN?
There are static methods Float.isNaN(float) and Double.isNaN(double) that you can use.
double x = ... // whatever calculation you do
if (Double.isNaN(x)) {
...
}
You can test for NaN two ways. You can use the built in function
Double.isNaN(x)
or perform the check this does which is
if (x != x)
provided x is a double or a float
This would work for you.
if(number == Float.NaN)