Comparing Specific Elements Of Two Arrays

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


ADVERTISEMENT

Comparing Elements In Array To Find Highest Int

Sep 3, 2014

So in this program, which is a grading program, I am trying to compare all the students averages to find who has the highest one and list the grades and the student's names from least to greatest. Yes, I see there are other problems in the program but it is nowhere near finished.

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
String[] studentName = new String[20];
int[] studentAverage = new int[20];
Scanner input = new Scanner(System.in);

[code]....

View Replies View Related

Comparing Two Arrays With Same Size

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

Equivalent Arrays - Comparing 2 User Inputs To Determine If Same

Mar 4, 2015

I have been stuck on this for the past 2 hours, basically we have to compare user input, put user input into an array and compare for equivalency. An example of the program:

Array 1: 1 2 3 4 5 6 7 8
Array 2: 1 2 3 4 5 6 7 8
Array 3: 1 2 3 4 5 6 7 8 9
Array 4: 1 2 3 4 5 6 7 8

Array 1 & 2 are equal
Array 1 & 3 are not equal
Array 1 & 4 are not equal.

This cannot be done by importing Java.util.Array at all!! (my research only find this to compare arrays ) which is why I am having trouble starting off. My code thus far:

import java.util.Scanner;
public class Lab07b {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter Integer 1:");
int a = input.nextInt();

[Code] .....

View Replies View Related

Creating Final Arrays With Final Elements

Aug 2, 2013

I want to create a final array with final elements
 
{code}
final int[] array = {0, 1, 2, 3, 4, 5};
{code}
 
But here only the reference of the array is final , not it's elements ... So how can I do that??

View Replies View Related

Declare Array Of 50 Elements Of Type Double - Print Output 10 Elements Per Line

Feb 5, 2015

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class array
{
public static void main(String[] args)

[Code] ...

Is there a way to write this, where, alpha is one array.

Write a program that declares an array "alpha" of 50 elements of type "double". Initialize the array so that the first 25 elements are equal to the square of the index variable and the last 25 elements are equal to three times the index variable. Output the array so that 10 elements per line are printed.

If I have an array of 50 integers, can I break that to read in lines of 10?

View Replies View Related

Write A Specific Byte Sequence To A Specific Memory Location On A Removable Storage Drive?

Jan 29, 2014

I am trying to write a specific byte sequence to a specific memory location on a removable storage drive. Does Java allow me a way to do this? I know the dangers in accessing memory, but the memory location of the data that will be written will never change.

how to assign a variable a memory location.

View Replies View Related

Swing/AWT/SWT :: Move Focus From Specific Jcombobox To Specific Jtextarea?

Nov 5, 2014

How do I move focus from a jcombobox to a specific component say a jtextarea.

My attempt below seems to be moving to a random component not the desired jtextarea.

takenByCombo.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_TAB ) {
e.consume();
dayJTArea.requestFocus(); // focust not moving dayJTArea
}
}
});

I applied the above logic to move focus from a specific jtextarea to another jtextarea as seen below and it works as desired.

dayJTArea.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_TAB ) {
e.consume();
monthJTArea.requestFocus();
}
}
});

View Replies View Related

Changing Value In Specific Point Of Specific Line In TXT File

Feb 9, 2015

So here we go with my problem:

- from the main class will arrive three variable (String name_used, int level_choose, int level_result)

I have a .txt file with this kind of formatting:

mario 1 1 0 1 0 1
carlo 0 0 0 1 1 0
...

Where I use 1 and 0 in the main for write if the level (you see that the numbers are always sixr? are egual to six level existing) BEFORE is done correct or wrong

- when in the main a user make a level a feedback coming back from the class level saying if the user made the count correctly or wrong. and i wanna replace the value (1 or 0) in the txt file with the new level result.

So i have all what i need as parameters i think.

name_used to look for the correct line in .txt file with .indexOf
level_choosed to go throught the correct index of that line
level_result (1 or 2) to be replaced with the existing one

Java Code:

public void salvaRisultati(String name_used, int level_choosed, int result_of_level) throws FileNotFoundException{
}
} mh_sh_highlight_all('java');

View Replies View Related

Check Specific Value From A Specific Line From TXT File

Feb 8, 2015

I have some problem to understand the way to make this:

In my main class a user can save his name in a txt file (and the system initially will add 6 value equals to 0) than he can choose between 6 level and make it.

example of .txt file data:

mario 0 0 0 0 0 0
carl 0 0 0 0 0 0

AT THIS MOMENT i just made other class and they work, is this new one that is hard for me. I'm trying to make a class that:

1- (first method called verificaRisultati) take name_used and level_choosed from the main and go to check in the .txt file if that level before was done right(1) or wrong(0)

and return something like "before you made this level properly" or "before you made this level incorrectly" AND THEN let the user start with the level.

2- (second method called salvaRisultati) at the end of the level i wanna pass the result (correct/incorrect) to another method of this class that will save the value (1 or 0) associated to the user in the right position.

This is the class that i'm writing:

Java Code:

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
public class ResultUsers {

[Code] ....

I really need some hint and some code example because I'm stuck. How I can take exactly the line with the user name? How I can correctly split the line in an array and then read/modify the value for that level?

View Replies View Related

Add Elements To 2D Array Without Losing Elements

Jan 12, 2015

I am trying to make a 2d array that keeps track of comparison counts. what I have so far works ok but writes over the previous elements with 0. can't seem to find where I am re-initializing the previous elements.

//this is one of my fill sort arrays

public void fillSelectionArray(int index, long countSum) {
//rand = new Random( );
//for ( int i = 0; i < listsize; i++) {
selectionList[ index -1] = countSum;
// }

[Code] ....

I know I am missing something just not sure what.

View Replies View Related

What Is The Difference Between Regular Arrays And Multi Dimensional Arrays

Jun 28, 2014

I mainly would like to know is a int[4][4] or a int [4*4] is more efficient to use. Which takes more storage? Which requires more to process? that kind of stuff.

View Replies View Related

Comparing Two Variables

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

Comparing Two Hashmaps

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

Comparing First Name Of Two People

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

Comparing Lengths On Strings

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

Comparing 2 String Values?

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

Comparing And Cloning Objects?

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

Comparing Words In Same Line

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

Error Comparing Strings

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

Comparing String But /0 Keeps Being Counted

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

Comparing In A Two Dimensional Array?

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

Comparing String Data?

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

Comparing Char In If That Convert To String

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

Comparing Two Dimensional String Array

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

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 View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved