Copy Matching Numbers From Two Arrays Onto New One

Jun 4, 2014

I want my function to return an array, with the array holding just the values of data that appear in good.

This is what should be returned:

{3, 5, 3, 2, 3, 3}

What is currently being returned:

{0, 3, 5, 3}

I didn't want to miss any numbers, so I decided to iterate through j for the "good" array, and then just i for the one that I was looking for matching numbers. Perhaps part of the problem is that if the condition is met, it goes to the next iteration of the loop. I'm not sure.

public class Arrays2 {
public static void main(String args[]){
int [] data = {8, 3, 5, 3, 7, 2, 8, 3, 3 };
int [] good = {5, 2, 3, 2};
int [] result = f2(data, good);
for (int i = 0; i < result.length; i++){

[Code] ....

View Replies


ADVERTISEMENT

Arrays Classes With Copy Constructor

Sep 29, 2014

Here is the exact instruction for the assignment...

Make 2 classes WrapperShallow and WrapperDeep.
Each class is simply a wrapper class to hold a private array variable. int [] a;
The default constructor for each class should initialize “a”.
Each class should have a toString() and equals().
Each class should have a setArray method that allows you to set the “a” variable.
WrapperShallow should have an invalid copy constructor.

[Code] ,....

Think about why shallow is wrong and deep is correct! What happens to the old “a” in the WrapperDeep copy
constructor? (think garbage collection)

Example Output:

--------------------Configuration: <Default>--------------------
**** TESTING SHALLOW OBJECTS ****
inital shallow object contains
7 17 77
copy shallow object contains
7 17 77
inital shallow object changed to
13 14 15
copy shallow object not changed contains
13 14 15

[Code] .....

So far I've only finished part of the WrapperDeep class. I can't get my equals method to work properly, I keep getting the error:

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - missing return statement
at WrapperDeep.equals(WrapperDeep.java:27)
at WrapperTest.main(WrapperTest.java:18)
Java Result: 1

My class looks like this:

public class WrapperDeep {
private int a[] = new int[3];
public WrapperDeep(int x, int y, int z){ //constructor initializes 3 integers in array
a[0] = x;
a[1] = y;
a[2] = z;

[Code] ....

View Replies View Related

Lottery Game Matching Numbers Error

Dec 11, 2014

import java.util.Scanner;
import java.util.Arrays;
public class LotteryTester {
public static void main(String[] args) { 
LotteryApplication lottery = new LotteryApplication();
int lotteryNumbersCount = lottery.getLotteryNumbers().length;
 
 [Code] ....

Everything works fine but when I run it the matching numbers tends to be off. For example I will type Enter in 1,1,1,1,1 for my 5 numbers. The lottery numbers will be 5,3,4,5,8 and it will say 1 number matched.

View Replies View Related

Generate 100 Numbers Using Arrays - Sort Even Numbers Into Separate Array And Display Both

Apr 24, 2014

I've just written a program that generates 100 numbers that range from 0 ~ 25 using arrays, the program calls another method that sorts the even numbers into a separate array and returns the array. I need it to display both arrays, however, when I run my program, the numbers of both arrays are mixed together, and I'm not sure how to separate them.

[ public class Array1
{
public static void main(String[] args)
{
int array [ ] = new int[100];
for (int i = 0; i < array.length; i++)
{
array[i] = (int) (Math.random() * 26);

[Code] .....

View Replies View Related

Generate Two Arrays - One With All Positive Numbers And Another With Negative Numbers

Mar 10, 2015

Create an integer array with 10 numbers, initialize the array to make sure there are both positive and negative integers. Write a program to generate two arrays out of the original array, one array with all positive numbers and another one with all negative numbers. Print out the number of elements and the detailed elements in each array.

public class problem3 {
public static void main(String[]args){
int[] numbers = {1, 2, 3, 4, 5, -1, -2, -3, -4, -5};
for (int i = 0; i<numbers.length;){
if(i>0){
System.out.println(numbers);
}
else
System.out.println(numbers);
}
}
}

View Replies View Related

Reverse Numbers Using Java Arrays

Feb 27, 2014

I am trying to do this assignment but I can't get the needed output. Create a program that asks the user how many floating point numbers he wants to give. After this the program asks the numbers, stores them in an array and prints the contents of the array in reverse order.

Program is written to a class called ReverseNumbers.

Example output

How many floating point numbers do you want to type: 5
Type in 1. number: 5,4
Type in 2. number: 6
Type in 3. number: 7,2
Type in 4. number: -5
Type in 5. number: 2

Given numbers in reverse order:
2.0
-5.0
7.2
6.0
5.4

My code:

import java.util.Scanner;
public class apples {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
double[] numbers;
System.out.print("How many floating point numbers do you want to type: ");

[Code] .....

View Replies View Related

1D Arrays List Of Numbers From User Input?

Dec 4, 2014

Write a program to maintain a list of the high scores obtained in a game. The program should first ask the user how many scores they want to maintain and then repeatedly accept new scores from the user and should add the score to the list of high scores (in the appropriate position) if it is higher than any of the existing high scores. You must include the following functions:

-initialiseHighScores () which sets all high scores to zero.

-printHighScores() which prints the high scores in the format: “The high scores are 345, 300, 234”, for all exisiting high scores in the list (remember that sometimes it won’t be full).

-higherThan() which takes the high scores and a new score and returns whether the passed score is higher than any of those in the high score list.

-insertScore() which takes the current high score list and a new score and updates it by inserting the new score at the appropriate position in the list

View Replies View Related

Java Arrays - Entering Lists Only Let To Enter 2 Numbers?

Oct 9, 2014

I am trying to write a code that allows you to input 2 lists and it tells you if they are identical or not. However, when I enter my fist list I can only enter two values and then it asks for the next list. How would I fix this to allow me to enter more than two values?

Also, If the second list is different it has me enter more values that list one.

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
  // Enter values for list1
System.out.print("Enter list1: ");
int size1 = input.nextInt();
int[] list1 = new int[size1]; 
for (int i = 0; i < list1.length; i++)
list1[i] = input.nextInt();
  // Enter values for list2
System.out.print("Enter list 2: ");

[code]...

My output looks like this:

Enter list1: 1
2
Enter list 2: 2
3
2

Two lists are not identical

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

Matching Grouping Symbols

Nov 27, 2014

I need to write a program that checks whether a Java source-code file has correct pairs of grouping symbols and the program must implement the Stack<E> class to solve this problem.

Here is the file:
[a({b,c}x{d,e})%x]
(1+(2-3)/4+[(a-({b+c}*5)/2)+e]
(a{b(c]}a)

I tried something like this, but I'm not exactly sure what I am doing wrong:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.Stack;
public class Driver {

[Code] .....

View Replies View Related

Java Matching Game

Apr 14, 2015

I'm working on a Java matching game and I'm having a hard time wrapping my mind around it. What I'm having trouble with is how exactly to create an array so that my squares have random values. Then how do I get it to reveal and compare those values by clicking on two of them? Here is what I have so far:

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

[code]....

View Replies View Related

Name And Address Matching From Oracle Database

Apr 26, 2015

I need a java program for getting name and address matching from oracle database. I need get all the customer name and address belonging to same person even if there is slight name and address changes are there.

James Smith
123 Broadway Av
Brownville, ME 04415

James S
123 Brodway Avenue
Brownville Junction, ME 04451

I am unable to get a best code for this and trying since long time.

View Replies View Related

Exception In Matching Parentheses Using Stacks

May 11, 2014

I am trying to write a program that checks for parentheses matching using stacks.This is my ArrayStack class.

public class ArrayStack{
private final int DEFAULT_SIZE=10;
public int tos;
Object[] array;
public ArrayStack(){
array=new Object[DEFAULT_SIZE];

[Code] ....

But the problem is when I compile matching I get an error as unreported exception EmptyStackException.must be caught or declared to be thrown. I think the problem is with exceptions which I don't have a good knowledge of.

View Replies View Related

Java Matching Braces Program?

Apr 17, 2014

Here if we give input from a text file from a disk and (a[b+c{d}]-v) is true and{a-(b]} is false but i get some syntax errors ...

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Stack;
public class Syste {
private static String expr;

[code]....

View Replies View Related

Matching Drawn Card To Its GIF In ArrayList?

May 4, 2015

I'm creating a card game and I'm having trouble getting a card gif to display. A card is drawn from an 52 card deck arraylist, and I then need to match that drawn card with the similarly named gif in an arraylist of card gifs, to display the gif on a card GUI. I have put my game, card, deck and graphical class below:

Card -
public class Card {
public String number;
public String suit;
public Card(String n, String s) {
number = n;
suit = s;

[Code] ....

The arrayList of card gifs (which contains all 52 gifs for the cards) is in the graphical class and my arrayList that contains the deck of cards in textual form (that I've imported from a textfile) is in deck. The names of the gifs in the gif arraylist are the same as the card names in my textual card arraylist (for example, 3h.gif is 3h in my card arraylist), so I believe I can use a .equals statement to match the card that has been drawn to the gif.

Here is what the gifs look like in their folder, which is an arraylist in my graphical class :

Here is what the drawn card looks like after the textual card arraylist has been shuffled:

The brown card in that image isn't the displayed card that has been drawn, it's just a graphical deck that I put in, I can't get the card that has been drawn on the GUI to display on the actual card frame, as you can see.

View Replies View Related

GUI Memory Game - Matching Images

Jun 30, 2014

Making a memory game where you click a square it turns over an image and you match the two pretty simple. My issue is my images are not displaying and I am not sure why everything looks correct to me.

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.util.Random;
public class MemoryApp extends JFrame {
static String Pics[] = {"beaver.JPG", "dawgs.jfif",

[Code] ....

View Replies View Related

Java Memory Matching Game

Feb 13, 2015

"A common memory matching game played by young children is to start with a deck of cards that contain identical pairs. For example, given six cards in the deck, two might be labeled

1, two might be labeled
2 and two might be labeled
3. The cards are shuffled and placed face down on the table.

The player then selects two cards that are face down, turns them face up, and if they match they are left face up. If the two cards do not match they are returned to their original position face down. The game continues in this fashion until all cards are face up.Write a program that plays the memory matching game. Use sixteen cards that are laid out in a 4x4 square and are labeled with pairs of numbers from 1 to 8. Your program should allow the player to specify the cards that she would like to select through a coordinate system.For example in the following layout:

1 2 3 4
--------------------
1 | 8 * * *
2 | * * * *
3 | * 8 * *
4 | * * * *

All of the cards are face down except for the pair 8 which has been located at coordinates (1,1) and (2,3). To hide the cards that have been temporarily placed face up, output a large number of newlines to force the old board off the screen.Use 2D array for the arrangement of cards and another 2D array that indicates if a card is face up or face down.Or, a more elegant solution is to create a single 2D array where each element is an object that stores both the cards value and face.Write a function that shuffles the cards in the array by repeatedly selecting two cards at random and swapping them.

Note:To generate a random number x, where 0<= x <1, use x=Math.random();.For example, multiplying y six and converting to an integer results in an integer that is from 0 to 5."I have been thinking about the algorithm and design of the question for a few hours.

View Replies View Related

Web Services :: Matching Images Pattern Recognition

Nov 27, 2014

I want to know that is it possible to match images in java and used in web? how is it possible?

View Replies View Related

Swing/AWT/SWT :: Matching Strings To Selected Rows In JTable

Nov 10, 2014

I have a JTable which shows pdf files. In Addition I can check if they have errors, if it so I can generate a "report" by clicking the pdf. But the report gives me errors from all of the pdfs in the table. How can I match the error messages to the pdf which I select?

Here is one of the checking methods:

public static void testAllPagesHaveSameRotation(PDFDocument pdf) throws PDFDocumentException {
if (SettingsGui.testAllPagesHaveSameRotation.isSelected()) {
if (pdf.getPages() == 0) {
rotationError = "";
} else {
rotationError = "";

[Code] ....

View Replies View Related

Searching Large Char Array For Matching Substrings

Feb 10, 2015

I have written an algorithm that compares the chars and returns all the found words in a 20x10 puzzle, but not for a 100x50 puzzle.

The puzzles are loaded in via text file and I have confirmed they're loading correctly.

guesses = String[] of search words
puzzle = char[][] of letters

Why it doesn't work on the large array?

Expected output:
word1, word2, word3

Actual output:
[blank line]

Here's the horizontal algorithm (row by row search)

//temp variable
ArrayList<String> temp = new ArrayList<String>();
try {
//for each guess word
for(String word : guesses) {
int count = 0;

[Code] .....

View Replies View Related

Image Processing Pattern Matching / Combination Algorithm

Jun 9, 2014

The problem I am trying to solve relates to block-matching or image-within-image recognition. This is an algorithm problem I am working on in java, but computationally, my computer can't handle generating all the combinations at one time.

I see an image, extract the [x,y] of every black pixel and create a set for that image, such as

{[8,0], [9,0], [11,0]}

The set is then augmented so that the first pixel in the set is at [0,0], but the relationship of the pixels is preserved. For example, I see {[8,0], [9,0]} and change the set to {[0,0], [1,0]}. The point of the extraction is that now if I see {[4,0], [5,0]}, I can recognize that basic relationship as two vertically adjacent pixels, my {[0,0], [1,0]}, since it is the same image but only in a different location.

I have a list of these pixel sets, called "seen images". Each 'seen image' has a unique identifier, that allows it to be used as a nested component of other sets. For example:

{[0,0], [1,0]} has the identifier 'Z'

So if I see:

{[0,0], [1, 0], [5,6]}

I can identify and store it as:

{[z], [5, 6]}

The problem with this is that I have to generate every combination of [x,y]'s within the pixel set to check for a pattern match, and to build the best representation. Using the previous example, I have to check:

{[0,0], [1,0]},
{[0,0], [5,6]},
{[1,0], [5,6]} which is {[0,0], [4,5]}
{[0,0], [1,0], [5,6]}

And then if a match occurs, that subset gets replaced with it's ID, merged with the remainder of the original set, and the new combination needs to be checked if it is a 'seen image':

{[z],[5, 6]}

The point of all that is to match as many of the [x,y]'s possible, using the fewest pre-existing pieces as components, to represent a newly seen image concisely. The greedy solution to get the component that matches the largest subset is not the right one. Complexity arises in generating all of the combinations that I need to check, and then the combinations that spawn from finding a match, meaning that if some match and swap produces {[z], [1,0], [2,0]}, then I need to check (and if matched, repeat the process):

{[z], [1,0]}
{[z], [2,0]}
{[1,0], [2,0]} which is {[0,0], [1,0]}
{[z], [1,0], [2,0]}

Currently I generate the pixel combinations this way (here I use numbers to represent pixels 1 == [x,y]) Ex. (1, 2, 3, 4): Make 3 lists:

1.) 2.) 3.)
12 23 34
13 24
14

Then for each number, for each list starting at that number index + 1, concatenate the number and each item and store on the appropriate list, ex. (1+23) = 123, (1+24) = 124

1.) 2.) 3.)
12 23 34
13 24
14
---- ---- ----
123 234
124
134

So those are all the combinations I need to check if they are in my 'seen images'. This is a bad way to do this whole process. I have considered different variations / optimizations, including once the second half of a list has been generated (below the ----), check every item on the list for matches, and then destroy the list to save space, and then continue generating combinations. Another option would be to generate a single combination, and then check it for a match, and somehow index the combinations so you know which one to generate next.

How to optimize what I am doing for a set of ~million items. I also have not yet come up with a non-recursive or efficient way to handle that each match generates additional combinations to check.

View Replies View Related

Non-Public Class In A Source File Doesn't Matching Its Name

Oct 20, 2014

I have a question about the following snippet concerning the steps the javac compiler follows to compile a program:

[...]at first, searching a class within a package is discussed if the latter doesn't contain a full package name[...]

It is a compile-time error if more than one class is found. (Classes must be unique, so the order of the import statements doesn't matter.)

The compiler goes one step further. It looks at the source files to see if the source is newer than the class file. If so, the source file is recompiled automatically. Recall that you can import only public classes from other packages. A source file can only contain one public class, and the names of the file and the public class must match. Therefore, the compiler can easily locate source files for public classes.

However, you can import nonpublic classes from the current package. These classes may be defined in source files with different names. If you import a class from the current package, the compiler searches all source files of the current package to see which one defines the class. I don't quite understand the red fragment. I wondered if the word "import" nonpublic classes from the current package weren't a synonym for the word "use", since why would we want to import a class from the same package when compiler searches the current package automatically anyway?

However I wanted to test nonpublic classes that are contained in source file which name doesn't match the class name:

NonpublicClass.java:

Java Code:

package com.work.company;
class NonpublicClass
{
public void description() {
System.out.println("Working!");
}
} mh_sh_highlight_all('java');

[Code] ....

Everything's fine when the source file names are the same as above. However, when I change NonpublicClass.java to a different name, there's an error "cannot find symbol" in:

Java Code: NonpublicClass v = new NonpublicClass(); mh_sh_highlight_all('java');

I noticed that the class file for NonpublicClass isn't even generated so that's probably the cause. If I change to the directory of the package the NonpublicClass is contained in and compile it directly, i.e. issue for example:

Java Code: javac NonpublicClass_different_name.java mh_sh_highlight_all('java');

Then a proper class file is generated with the class name, that is NonpublicClass.class, and afterwards I can compile the main program in Start.java.

So the question is what am I missing regarding the cited quote that reads:

If you import a class from the current package, the compiler searches all source files of the current package to see which one defines the class.

? Because adding such an import to CompanyClass.java:

import com.work.company.NonpublicClass;

didn't work either...

View Replies View Related

Compare 2 Strings - Generate New String With Matching Characters

Jul 3, 2014

I'm attempting a small program as I'm teaching myself the ropes. In it, I need to compare one string (the base) to another which is just the base string that's had it's characters shuffled.

Java Code:

String base = "ABCDEFG"
String shuffled = "CDAFBEG" mh_sh_highlight_all('java');

What I need to do is run a loop that shuffles the base string each time, but compares and saves any characters that match in the correct location. For example, if shuffled = "CDAFBEG", then the G would be "locked" in place and the rest of the characters shuffled and looped back to the comparison.

I have all the code I need for shuffling the string, but I'm not sure how I would go about comparing each character and then also locking it in place. I get the basics, I think, of needed to use several variables.

View Replies View Related

How To Copy Elements Of One Array Into Another

Mar 29, 2015

I have two arrays

private ReservedBook[] reservedBooks;
private LibrarySystem[] libraryBooks;

The library array has two books and I want to copy one of them to the reserved books when you type in the ISBN

public void borrowBook(String ISBN)
{
int i = 0;
if(numberOfBooks < MAX_BOOKS-1)
{
if(libraryBooks[i].getBookISBN().equals(ISBN))
{
for(i=0;i<MAX_BOOKS-1;i++)
reservedBooks[i] = libraryBooks[i];
}
else System.out.println("There is no such book");
}
else System.out.println("You have reached the maximum number of allowed books");
}

It shows me error: incompatible types - LibrarySystem cannot be converted into ReservedBook. How can I fix it?

View Replies View Related

Error In Array Value Copy

Aug 6, 2014

I am trying to copy all odd value of an array in one array and all even value in another array

this what i have try to so far

private static void arrayOperation(int[] a) {
// TODO Auto-generated method stub
int n=a.length;
int odd_value[] = {};

[Code].....

But i am getting error

View Replies View Related

Copy A Exe File From One Location To Another?

Aug 1, 2014

I'm trying to copy a exe file from one location to another. It seems simple, but I have failed to find anything about it besides coping the contents in txt files, but that does nothing for executable.

View Replies View Related







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