Comparing Sign Of A Number In Java?
Sep 23, 2014
I want to check to see if given integers digits a, b are both positive or both negative. How do i do that in java.
how to check below number 'a' is negative or not in if condition using signum method?
if(Integer.signum(a)==-1){
Above line is not working
View Replies
ADVERTISEMENT
Mar 7, 2015
I am having a bit of trouble making the '+' add two numbers in java. For example:
When the user enters " + 4 5 " I want it to come out as " 4.00e+00 + 5.00e+00 = 9.00e+00 "
Here is what I have so far:
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNextDouble()) {
double num1 = in.nextDouble();
double num2 = in.nextDouble();
String s = "";
[Code] .....
View Replies
View Related
Apr 4, 2015
How do I compare an array (or maybe int?) (example: 3572) to a 4digit number from random generator? how many of the four digit match the generated numbers.
View Replies
View Related
Apr 13, 2014
package guess.the.numbers;
import java.awt.*;
import javax.swing.*;
import java.util.Random;
import java.awt.event.*;
ublic class GuessTheNumbers extends JFrame{
private JButton guessBtn;
private JButton restartBtn;
[Code] ....
I am getting a strange error and it almost seems like its not comparing it to the random generated number just the guess that i entered before. Here are my error messages.
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at guess.the.numbers.GuessTheNumbers$ButtonHandler.actionPerformed(GuessTheNumbers.java:119)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
[Code] .....
View Replies
View Related
May 11, 2015
There's a site that uses DBsign UWS to validate personal certificates on a smart card. I wound up breaking that functionality by moving the default Java truststore so I could create a new one with just root/intermediate CAs that I trust (I have no desire to allow apps signed in China, Russia, Turkey, and countries spelled with heiroglyphs). Now, my browser believes the UWS is self-signed and rfuses to run it. I need to find the certificate used to sign that app to see which cert(s) signed it, so I can add them back to the truststore. How can I find that?
View Replies
View Related
Jan 15, 2014
Here is the code I wrote:
import javax.swing.*;
import java.awt.MenuBar;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class NewCalculator implements ActionListener {
//assign button clicked number and answer onto variables.
[Code] .....
Also what is the code I need to use to do the percentage and square root calculation in the calculation. Cos I am not sure about the sign I am required to use to do the calculation.
The if statement is where the code to make the clear button (C and CE) work.
View Replies
View Related
Apr 27, 2015
I have a checksum function that is suppose to read IPV4 packet and return a short integer value. The IPV4 packets are stored in a byte array. I am having trouble storing the first 8 bits and second 8 bits of the short integer into the byte arrays especially when they have leading 1s. For example, if my checksum returns 5571 (binary = 0001 0101 1100 0011) The first 8 bits is suppose to represent 195 but when I try to assign a larger integer type to a btye the information gets sign extended. The 195 turns into -61. I tried using bit addition like array[10] = array[10] & 0xff, but the result remains the same.
public static short checksum(byte [] a, int length) {
short sum = 0;
long data;
int i = 0;
while(length > 1) {
data = (((a[i] << 8) & 0xff00) | ((a[i + 1]) & 0xff));
sum += data;
[code]....
View Replies
View Related
Nov 15, 2014
i know that int [][] x = new int[2][2] will generate a 2x2 array but I'm looking at a certification mock question and I see double [][] da = new double [3][]. What is the empty [] on the right hand side of the equal sign trying to tell me? Is there some default value?
View Replies
View Related
Jul 4, 2014
Is it possible to compare 100 objects using comparator interface in java?
View Replies
View Related
Sep 25, 2014
1. Write a Java program that randomly generates a five-digit lottery number and prompts the user to enter a five-digit number. Each digit in the number is in the range between 0~9. The program should determine which prize the user wins according to the following rule:
- The user wins the first prize if the user input matches all five digits in the lottery number in exact order.
-The user wins the second prize if the user input matches any four digits in the lottery number in exact positions.
-The user wins the third prize if the user input matches any three digits in the lottery number in its exact position.
-The user wins the fourth prize if the user input matches any two digits in the lottery number in its exact position.
- The user wins the fifth prize if the user input matches any one digit in the lottery number in its exact position.here is my code. I have tried replacing the && statements with || and it always returns either case 1 or case default.
import java.util.Scanner;
import java.util.Random;
class Hw5 {
static int getPrize(int g1, int g2, int g3, int g4, int g5,
int u1, int u2, int u3, int u4, int u5) {
[code]...
View Replies
View Related
Mar 30, 2014
Let's get it out of the way -- I'm stumped. On something that should be pretty simple to solve. Code follows.
public class Sum {
public static void main(String [] args) {
Double nSum = 0.0;
Double aDouble = 100.0;
for (int i = 0; i < 1000; i++){
nSum += 0.1;
[Code] ....
The output:
100.000000
100.000000
false
false
false
false
false
Process finished with exit code 0
I wrote another simple program and hardcoded the values: aDouble = 100.0; bDouble= 100.0000
Using aDouble.equals(bDouble) returns true, just as one would expect.
So what am I overlooking?
View Replies
View Related
Mar 3, 2015
I have a simple question but I dont understand why I am getting false for this boolean statement.
System.out.println("hash compare " + (trialSearch.returnHash() == fish.returnHash()));
System.out.println("fish.returnHash()" + (fish.returnHash()));
System.out.println("trialSearch.returnHash()" + (trialSearch.returnHash()));
the output is as follow:
hash compare false
fish.returnHash(){T=[1, 2, 3, 5], G=[], A=[0], C=[4]}
trialSearch.returnHash(){T=[1, 2, 3, 5], G=[], A=[0], C=[4]}
why is it printing false for the boolean statment when the two hashmaps contain the same values and keys?
View Replies
View Related
Nov 6, 2013
I'm trying to create an algorithm that compares the first names of two people, which goes ahead and cancels similar characters and then counts the remaining characters to give a 0 if the remaining characters are even and a 1 if the remaining characters odd.
View Replies
View Related
Apr 25, 2015
I am making a program i want String length and compare it that it will be null or not but it gives me error.
View Replies
View Related
Nov 8, 2014
I am trying some exercises on codingbat.com, and am stuck at the following program.
"Given a string, return true if it ends in "ly"."
With the following lines, if I type a print command instead of return, I get "ly". Yet if I aks to compare the result (which is "ly" as I can see with a print command) with == "ly", I get false?
What I also don't get, is that if I tye the programs in javascript, in that language the program works.
String str = "Oddly"
return ((str.substring(str.length()-2))== "ly");
}
View Replies
View Related
Aug 2, 2014
I'm working on a program with the following instructions: Write a class named Octagon that extends GeometricObject and implements the Comparable and Cloneable interfaces. Assume that all 8 sides of the octagon are of equal size. The area can be computed using the following formula
area = (2 + 4/square root of 2) * side * side
Write a program (driver) to read in a series of values from a file, display the area and perimeter, create a clone and compare the object and its clone (based on the area). In addition, your program should compare the current object (just read in) with the first object read in. The program ends when a negative number is read from the file.
My GeometricObject abstract class:
public abstract class GeometricObject {
public abstract double getArea();
public abstract double getPerimeter();
}
My Octagon Class:
public class Octagon extends GeometricObject implements Comparable<Octagon> {
private double side = 1.0;
protected native Object clone() throws CloneNotSupportedException;
public Octagon() {
}
public Octagon(double side) {
super();
[code]....
As you can tell, I've still got a long way to go in the tester class but this is where I'm running into some difficulties.
You'll notice that in the return statement of the toString method in the Octagon class, I put a ? after the "Clone Compare:" portion of the code, what should go here. I've never worked with either the Comparable or Cloneable interfaces before.
How I should create my objects in a way that would give the following sample output.
Object 1: Area: 120.71
Perimeter: 40.0
Clone Compare: Equal
First Compare: Equal
Object 2: Area: 271.60
Perimeter: 60.0
Clone Compare: Equal
First Compare: First is smaller
View Replies
View Related
Jan 24, 2015
I have tried to compare word in same line..instally i divided words using split...after that my move got stopped....
View Replies
View Related
May 24, 2014
I was writing a code to have the library books classified in name, author, area, ed, etc. I'm using NetBeans and it doesn't accuse any error. But when I run the project, it never goes right and shows the books only in one area, regarthless what I type. (The goal of the algorithm is to separate the books in areas (sciences, humanities and biological science).
View Replies
View Related
Apr 20, 2014
Ok I am trying to compare a string to see if all characters are unique. If there is a library for this or a better way to approach this do tell. However I find it important to understand what is going on behind the scenes. The issue is that the program counts the spaces '/0' and therefore everything will never be unique.
public class CheckUnique
{
private String sentence = "This will be compard";
private char[] checker;
private String isUnique = "The sentence is unique";
private String notUnique = "The sentence is not unique";
[code]...
View Replies
View Related
May 3, 2014
I have to make a program in which users inputs a number and the program should search into a two dimensional array and print out all the values that are below the number This is my first time experimenting with 2D Arrays and how to do this program I have the array set up
String firstarray[][]=
{{"", "Store101", "Store102", "Store103", "Store104"},
{"Tennis Shoes", "102", "54", "20", "78"},
{"Sweaters", "45", "25", "35", "75"},
{"Jeans", "12", "35", "45", "65"},
{"Shorts", "54", "25", "34", "45"},
{"Jackets", "15", "35", "50", "25"}
};
View Replies
View Related
Jan 27, 2015
This program accepts Student ID numbers, Name, and grade point average. The problem I am having is with the if else statement that compares id to studentID[x]. I have tried to compare using if(id.equals(studentID[x])) and also I have tried using if(id == (studentID[x])) as shown in the code below. I keep getting incorrect results though.
//FILE: StudentIDArray.java
import javax.swing.*; //Used for the JOption dialog boxes
import java.util.*; //Used for Scanner input
[Code]....
View Replies
View Related
May 7, 2013
Is it possible to compare 2 arrays as below if they have same size?
Int array[][] = new int[10][10];
array1[I] == array2;
Or assign the whole array to another array?
array1[I] = array2[I];
Suppose I have 2 arrays, 1 is old, and 1 is new.
I will do the calculation to update the new array.
Then, I will compare between the 2 arrays.
If they are the same, I will stop the calculation.
If they are not the same, I will assign the new array to the old array.
Then, I will do the calculation again until the old array and the new array are the same.
As I do not want to write a loop to compare and assign the value to the arrays. May I compare or assign the values to the whole array directly.
View Replies
View Related
Apr 30, 2014
what will i compare in if statemet is the 1st letter of each if i have code="a" and name="Angelina" first letter of each is "a" and "A" then in convert it to string so that i can make it uppercase but when i compare it in if statement it always go into "not x" but the ouput that im getting is x=A y=A then it always direct me into else statement.
String code = "a";
String name = "Angelina";
char c = code.charAt(0);
char n = name.charAt(0);
[code]...
View Replies
View Related
Mar 7, 2014
I want to compere two element of string array by each other! eventually I want to print Yes or No in matrix . SO, I start reading data from file then split them into two parts .
File file= new File(fileName);
try {
inputStream = new Scanner(file);
while (inputStream.hasNext()){
String data= inputStream.next();
String [] token =data.split(",");
System.out.println("day"+token[0] +"embloyee name:"+ token[1]) ;
}
inputStream.close();
Now I want to compere each cell from token[0] by another array :
String[] day= { "Sunday", "Monday" ................};
if the days are equal then I want print yes in front of the employee name if not then i want to print No..is this gone work with me as I imagine it to be or do I have to take few more steps to get my code going?
View Replies
View Related
May 5, 2015
I’ve been working on this problem for a couple days already but I came to a point where I don’t really know what to do. Basically I’ve got two arrays filled with int values (0, 1, and 2), which I get from Class B through their respective getMethods.
1 stands for black and 2 stands for something else.
The program counts how many times the value 1 (if chosen color is black) occurs in both arrays and then compares the both counts. If it detects any difference the program does something else, otherwise it waits.
public class A {
private static boolean finished;
public A() {
B objectClassB = new B();
int[] numbers = objectClassB.getNumbers();
int[] numbers2 = objectClassB.getNumbers2();
String color = objectClassB.getColor();
[Code] ....
Here the class B.
public class B {
private int[] numbers = new int[] { 2, 2, 0, 2, 1, 0, 1, 2, 1, 1, 0, 2, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 2, 1 };
private int[] numbers2 = new int[] { 2, 2, 0, 2, 1, 0, 1, 2, 1, 1, 0, 2, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 2, 0 };
private String color = "black";
public B() {
}
public int[] getNumbers(){
[Code] .....
this would be the Output:
[2, 2, 0, 2, 1, 0, 1, 2, 1, 1, 0, 2, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 2, 1]
[2, 2, 0, 2, 1, 0, 1, 2, 1, 1, 0, 2, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 2, 0]
number of black tokens of first array: 8
number of black tokens of second array: 7
the number of black tokens has changed
The actual problem is that I will be getting one array at a time (meaning: the array int[] numbers from Class B updates itself and might change its values). I just declared and initialize both arrays for illustration purposes.
That would mean I need to hold the array’s values the first time I get them in a temporary variable until I get the values of the updated array. Then I could use them in the method –numberOfRelevantElements- and check if any changes have occurred.
What would be the best approach for doing this? I though of inserting the different counts that I get into a queue and then comparing these values one after the other. But I’m not really sure if that would work.
View Replies
View Related
Jan 6, 2014
I have to write a diff which will compare two txt files, diff have to return changes and additional lines in console with indicating in what file and line.
Example:
File1:
This is war
on the front.
Flying is cool.
File2:
This is war
on the beach.
Something here.
Flying is cool.
Result of this should be:
Changed lines:
line2/line2 - on the front./beach.
file1 line2 on the front.
file2 line2 on the beach.
Additional lines:
file2 line3 Something here.
View Replies
View Related