Comparing Two Array Lists - Output If They Are Equal Or Not

Nov 21, 2014

I need comparing two array lists. For this program i am comparing 2 array lists. The list is integers entered by the user the second is random generated numbers. So far in my program i am able to compare the 2 arrays together and output if they are equal or not however i need the program to output even if atleast one if the integers match,

EXAMPLE list one: 1, 2 ,3 ,4, 5. LIST TWO: 1, 3, 3, 3, 3.

Since the first number matches i want it to out put there is one match, so on and so forth with if there are 3 or 4 matching integers. here is my code so far.

public static void main(String[] args)
{
final int NbrsEntered = 5; //Number of guessed numbers entered
final int LOTTOnbr = 5;
int[] numbers = new int[NbrsEntered];
int[] randomNum = new int[LOTTOnbr];
//int[] TestArrayOne = { 1, 2, 3, 4, 5 };
//int[] TestArrayTwo = { 1, 2, 3, 3, 5 };
boolean arraysEqual = true;
int index = 0;

[Code] ....

View Replies


ADVERTISEMENT

Convert String To Two Dimensional Char And Output Is Equal To Matrix

Jan 6, 2014

I have written the code:

Java Code:

public static void main(String[]args)
{
Scanner input = new Scanner(System.in);
System.out.print("Type your text: ");
String text = input.nextLine();
int counter = text.length();
if(text.length()> 16)

[Code] ....

And input is: abcdefghijklm

output is:

Java Code:

a b c d
e f g h
i j k l
m x x x mh_sh_highlight_all('java');

So all i want is, if i type: abcdefghijklm

I want this output:

Java Code:

a e i m
b f j x
c g k x
d h l x mh_sh_highlight_all('java');

View Replies View Related

How Many Numbers In Array Have Value Greater Than Or Equal To 100

Sep 10, 2014

I have an assignment that wants me to write a Java function based on induction to determine how many numbers in an array have a value greater than, or equal to, 100.

I have started with:

Java Code:

int recurseHundred (int [] A, int n) {
//n is the number of elements in the array.
//Base case:
if (n == 1 && n >= 100) return A[0];
//Recurse
int num = recurseHundred(A, n-1);
if (n-1 >= 100) return A[n-1];
else return num;
} mh_sh_highlight_all('java');

I don't think this actually does the trick.

View Replies View Related

How To Test For Array Length Equal To Zero

Mar 15, 2015

I am working on an assignment covering exception handling and am stuck on part of the assignment. How can you test for array length = 0?

When I try something like: if (array.length == 0) on a null array, naturally I get a NullPointerException. I would try something like if (array != null) but both array length of 0 and null array are supposed to throw different expressions.

View Replies View Related

Initializing Array - Equal Sign?

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

Divide Array In Two Parts - Equal To Sum Of Elements

Dec 1, 2014

Divide an Array in two Parts in such a way so that sum will be equal while we add all the elements of array.

View Replies View Related

Array Of Scores - Return True If Each Score Is Equal Or Greater Than One Before

Mar 8, 2014

Given an array of scores, return true if each score is equal or greater than the one before. The array will be length 2 or more.

scoresIncreasing({1, 3, 4}) → true
scoresIncreasing({1, 3, 2}) → false
scoresIncreasing({1, 1, 4}) → true

Java Code:

public boolean scoresIncreasing(int[] scores)
{
for (int i = 0; i< scores.length-1; i++) {
if (scores[i] < scores[i+1] || scores[i] == scores [i+1]) {
return true;
} else {
return false;
}
}
return false;
}

Unsure what the problem is, it will always return True for some reason...

View Replies View Related

Array Lists In A Class

Dec 6, 2014

What is going on here in the main class is a zoo that requires information to be read from and saved to a .txt file. I have made three arrayLists for each .txt file, I am getting errors for illegal start to an expression

import java.io.*;
import java.util.*;
public class ColumbusZoo
{
public static void addHelper(ArrayList<DomesticAnimal> a){
Scanner s = new Scanner(System.in);
System.out.println("What species");

[code].....

View Replies View Related

Array Lists - Adding New Elements

Apr 1, 2014

I am working on a assignment that has to do with array lists, it mainly has to do with adding new elements, telling then where it is it located, if the memory block is empty , ect. so far i have been having problems with my indexOf method which should display the array cell number that a input element E is in, and if it is not in there it should display a -1.

public class MyArrayList<E>
{
private E[] data_store = (E[])new Object[2];
private int sizeofa = 0;
private void resize()// makes the array list bigger if need {
E[] bigspacemaker = (E[])new Object[data_store.length * 2];
for(int x = 0 ; x< sizeofa ; x++)

[Code] ....

Error message:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 512
at MyArrayList.indexOf(MyArrayList.java:28)
at MyArrayListDemo1.main(MyArrayListDemo1.java:26)

View Replies View Related

Merging And Sorting Array Lists Code Is Returning 0s?

Apr 19, 2015

I'm working on an assignment that asks for the user to input 2 lists of numbers and my program will merge and sort the lists using arrays and 2 methods. I think I have most of it down, but I'm not sure how to go about getting the user inputs. In my current code, it's giving me a bunch of 0s instead of a sorted list.

import java.util.Scanner;
public class merge2sortedlists {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);

[code]....

View Replies View Related

Split Text File Lines Into Pieces And Put Them In Different Array Lists?

Mar 14, 2015

For example, if I had a text file with one line such as "first, second, third", would there be a way to make it so I could make "first" go to the first array list, "second" to the second, and "third" to the third?

I haven't posted any of my code so far as it wouldn't be right to be handed the finished code on a platter and I should put the work into it, but I'd just need a loop to make it go through the rest of the lines in the text file to add all the first, second and third parts of each line, right?

(I already know how to input data from a file, but not how to split up a line into bits to put into different array lists).

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 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 String To Array Element?

Oct 17, 2014

I have the following code:

Java Code:

public class Equals {
String[] s1 = {"red", "white", "black", "blue"};
String[] s2 = {"red", "black", "green"};
String[] s3 = {"red", "green"}; mh_sh_highlight_all('java');

What I need is to give me the following output:

Select Strings: s1,s2,s3
Comparing String s1, s2, s3

red 3 matches.

black 2 matches.

green 2 matches.

View Replies View Related

Comparing String To Array Element

Oct 17, 2014

I have the following code:

public class Equals {

String[] s1 = {"red", "white", "black", "blue"};
String[] s2 = {"red", "black", "green"};
String[] s3 = {"red", "green"};

What I need is to give me the following output:

Comparing String s1, s2, s3

red 3 matches.
black 2 matches.
green 2 matches.

View Replies View Related

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 Array List And Outputting Difference

May 12, 2014

I'm having a bit of trouble outputting the difference between two array list

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.PrintWriter;
import java.util.ArrayList;
public static void main(String[] args) throws Exception {

[Code] ....

This prints out something like

Test1
Test1
Test2
Test2
Test2
Test3
Test3
Test3
Test4
Test4
Test5
Test5
Test6
Test6
Test6

Ideally, it's suppose to print out only Test2, Test3, Test6.. I've tried different combinations of loops and equals() but I never get the correct output.

View Replies View Related

Comparing Array Or Int To 4 Digit Number From Random Generator?

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

Output Showing 0 For Array Elements

Oct 27, 2014

This is my code for the driver class:

import java.util.*;
public class SavingsAccountDriver {
public static void main(String[] args) {
System.out.printf("%10s%10s%10s%10s%10s" , "Month" , "Account #" , "Balance" ,
"Account #" , "Balance");

[Code] .....

Here is the output I get :

Month Account # Balance Account # Balance
----- --------- ------- --------- -------
0 10002 2000.00 10003 3000.00
1 10002 2002.00 10003 0.00
2 10002 2004.00 10003 0.00
3 10002 2006.01 10003 3030.00
4 10002 2008.01 10003 0.00
5 10002 2010.02 10003 0.00
6 10002 2012.03 10003 3060.30
7 10002 2014.04 10003 0.00
8 10002 2016.06 10003 0.00
9 10002 2018.07 10003 3090.90
10 10002 2020.09 10003 0.00
11 10002 2022.11 10003 0.00
12 10002 2024.13 10003 3121.81

Why is it not showing those months updated balance? I feel like I'm pretty close. I guess I should specify that this is supposed to calculate monthly interest for one account and quarterly interest for the other. This is what the output should look like.

Month Account # Balance Account # Balance
----- --------- ------- --------- -------
0 10002 2000.00 10003 3000.00
1 10002 2002.00 10003 3000.00
2 10002 2004.00 10003 3000.00
3 10002 2006.00 10003 3030.00
4 10002 2008.01 10003 3030.00

... ... 12 ... ...

The quarterly interest is the one that is giving me trouble.

View Replies View Related

Reverse Order Array Output

Oct 10, 2014

I'm trying to make this code's output display like a sentence, since right now it displays downward and doesn' look right.

public class ReverseOrder {
public static void main(String[] args) {
String phrase = "The rain in Spain falls mainly on the Plain";
char[] phraseArray;
phraseArray = phrase.toCharArray();
for(int i = phraseArray.length - 1; i >= 0; i--){
System.out.println(phraseArray[i]);
}
} // end main
} // end ReverseOrder class

View Replies View Related

Printing Index Of Output Array

Feb 6, 2014

I want to Display the Index of output Array.

here is the code:
 
import java.util.ArrayList;
import java.util.List;
public class selection {
private static int[] numbers= { 1, 2, 4, 8, 16, 32, 64, 128, 250, 250, 250, 250, 250, 250, 250 };
private static int[] sumsum= new int[numbers.length];
private static int sum= 1759;

[Code] .....

View Replies View Related

Print Array Output On Different Lines

Nov 19, 2014

I've to read in an array of 1000 random numbers, and then my program sorts them depending on whether the user chooses bubblesort, selectionsort, etc...My code all works fine, my problem is with the actually output display. I have method that displays the content of the array that I call for the unsorted array, and for the sorted array. So at the moment the output to the user looks like:This program sorts an array of numbers by your choosing.

Array before sorting:
148
626
817
4
312
652
643
134
etc... for 1000 numbers

----------------------
1. Bubble Sort
2. Selection Sort
3. Insertion Sort
4. Merge Sort
5. Quick Sort
----------------------

Please enter your sort choice: 4

Array after sorting:
1
3
4
4
5
6
6
7
etc... for 1000 numbers

is there a way that I can display the unsorted and sorted array's content on various lines like so:

150 312 149 31 392 384 66
498 909 35 602 568 897 131
654 645 274 264 895 281 462
244 171 958 649 740 351 344

I just think it'll look nicer if I can do it like that.My display method looks like this:

//method to display array
public static int[] displayArray(int[] array){
//display array
for (int i =0; i < array.length; i++)

[code]...

View Replies View Related

Program Should Print Array As Output

Jul 11, 2014

Write a program to create an integer array of size 20. Then, the program should generate and insert random integers between 1 and 5, inclusive into the array. Next, the program should print the array as output.

A tremor is defined as a point of movement to and fro. To simulate it, the program will generate a random number between 0 and 19, which represents the location in the array (i.e. index number). Then, the 3 numbers to the left and right of this location should be reset to the value 0. If there isn't 3 numbers to the left and right you may assume a lesser number depending on the boundaries of the array.

Then, the final array should be printed as output. There is no user input for this program.Your program must include, at least, the following methods:

-insertNumbers, which will take as input one integer array and store the random numbers in it.
-createTremor, which will generate the random number as the location and return it.

A sample run of the program is shown below:

Sample output #1:
Array:1 2 2 3 1 5 4 2 3 4 4 2 1 1 3 2 1 4 3 2 1
Random position: 5
Final Array:1 2 0 0 0 5 0 0 0 4 4 2 1 1 3 2 1 4 3 2 1

View Replies View Related

Not Getting Output From Array But Debug Shows Data Is There

Jul 1, 2014

I am working on a simple JAVA tutorial, not homework, where employee data is taken from an array and displayed on the console. The data is divided by department, age, name and for the Accounting and Information Systems departments, they are displayed in ascending order by employee age. Everything works except I am not getting the output to the console other than the titles. As I step through the debug, the data clearly is populating the array.

Here is my code:

Company Class:

package SimpleJavaAssignment;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;

[Code] .....

Prime Age Checker:

package SimpleJavaAssignment;
import java.math.*;
public class PrimeAgeChecker {
public boolean PrimeAgeChecker(int age) {
BigInteger bi = new BigInteger(String.valueOf(age));
boolean prime = bi.isProbablePrime(10);
return prime;
}
}

View Replies View Related

Array Recursion Sum Is Not Producing Correct Output?

Feb 2, 2015

I am trying to sum up the elements of an array. When I test my code the sum is always off by one. For example if I input: 20, 40,30 it gives me 89 instead of 90.

This is what I have so far:

public static void main(String[args]){
int size = kbd.nextInt();
int [] myArray = new int [size]
//user inputs the elements of array
for(int i =0; i<myArray.length; i++){
myArray[i]= kbd.nextInt();
}
int total = sumNum(myArray,0, myArray.length-1)
System.out.println("The sum is"+ total);
}

[code]....

View Replies View Related

What Does Setting Object Equal To Another Do

Mar 5, 2014

I've seen this done in code:

For example:

Java Code: BufferedImageOp op = new ConvolveOp() mh_sh_highlight_all('java');

What does this mean? Is it creating an object of convolveOP for the BufferedImage class?

What does it mean when you set an object from one class equal to another?

View Replies View Related







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