I'm having a problem printing out the descending order of my array. The array order goes like (Title,Studio,Year). I try to create to ints with the compareTo method but when the program is run the I get array out of bounds. Could the answer possibly be that in order not not have the out of bounds error, to create a for loop inside of the while?
public class Movie2
{
// instance variables
private int year;
private String Title;
private String Studio;
I have been working on getting the merge sort working, I have a complteted code but am not getting the right results... Here is my code and the results are "876323149" ...
public class MergeSort { private static int[] local; // for use in copying private static int[] a; public static void main(String[] args) { int[] test = {2,3,1,4,7,8,6,3,9};
Merge sort implementation on the given array i listed in the code below. I know Arrays.sort() or Collections.sort() could do the trick but i want to achieve it through merge sort.
import java.util.*; public class Merge{ public static void main(String[] args){ String[] myStringArray=new String[]{"z","a","z","b","c","e","z","f" ,"g","a","w","d","m" ,"x","a","R" ,"q","r","y","w","j","a","v","z","b"}; } }
I've playing around with linked lists and methods for sorting. So far I've tested the iterative sort, insertion sort, quick sort and they all worked perfectly. Now, I am trying to implement merge sort that would take a linked list of jobs and sort them according to their priority. I found a few solutions on the web, of which I am trying to implemented this one: LeetCode.
My system is a simple one, I do have a linked list of print jobs, each of which has the priority. The following code should sort my print queue and return the link node of the first sorted element. Here's the code.
//defining a job that has priority public class Job { private int priority;
[Code]....
The problem I've been trying to solve is that I am getting the stack overflow at line
ListNode<T> h1 = mergeSort(left);
meaning that I am getting into a loop somewhere down through the process of breaking the linked list into half, half or halfs and so on.
I have this assignment to write a Merge Sort algorithm using recursion. To start I have a very tough time picturing what is happening when it comes to recursion, but I do understand how merge sorting works. At the moment I feel as though a very good portion of my code is correct, but I am having trouble with the recursion in the main method [ mergeSort(Queue<T> queue) ].
I have another 4 or so hours to pass in my assignment finished or not, and at this point I can honestly say I have no clue how to make my code work. I tried working through the problem on paper with a simple queue of size 3, but even that is a struggle. On paper my code works perfectly fine, so there is definitely something I am missing.
Below is what I have along with my JUnit test.
Java Code:
private Queue<T> output = new Queue<T>(); private Queue<T> output1 = new Queue<T>(); private Queue<T> output2 = new Queue<T>(); public Queue<T> mergeSort(Queue<T> queue) { // TODO 1 if(queue.size() <= 1) { return queue;
im having an issue with the 3rd thread that are supposed to merge the two sorted sub arrays , i pass the 2 subarrays to my runnable function sortlist and they are renamed IntSortList 1 and 2 and th1.start() and th1.join() are called and it works fine, but then i have another runnable constructor that takes IntSortList 1 and 2 but it does take a runnable. below is the code in my main,
Runnable InSortlist1 = new sortList(data2p1); Runnable InSortlist1 = new sortList(data2p1); Thread th1 = new Thread (IntSortlist1); Thread th2 = new Thread (IntSortlist2); try { th1.start(); th1.join();
I am trying to sort a set of parallel arrays. I really believe that the code is correct, but it is not working out as expected.This is the specific code for the sort:
Java Code: for (int y = 1; y < (dataArray.length + 1); y++) { for (int x = 0; x < dataArray.length - 1 ; x++) { if ((dataArray[x][1]) <= (dataArray[x + 1][1])); { tempOpen = dataArray[x][1]; dataArray[x][1] = dataArray[x + 1][1]; dataArray[x + 1][1] = tempOpen;
I am working on a class that sorts and matches items in a collection of arrays. In the end, elements common to all 3 arrays should be printed. The idea is that as the first array is compared to the second it stores the matched items in tempArray. When all items are compared the tempArray should overwrite the checked array, and the process continues until all arrays are checked. I have set all the checking in a do loop that should run while the value is <= to the array length. This allows all items in the reference array to be checked. I get an index out of bounds message but not where I would expect it. I have tried varying the condition in the do while loop in the match method, but it did not change the result. There may be other issues I have not addressed with solving the algorithm, but this one has me stumped and I am not able to progress.
package testing; public class TestMatchMain { public static void main(String[] args) { Comparable[] innerCollection0 = {1,2,3,4,5}; Comparable[] innerCollection1 = {1,1,5,6,7};
I have to make a flowchart that shows a bubble sort. Here is the exact directions: provide a flowchart for a bubble sort, and write the pseudo-code that corresponds to the flowchart. You can assume the existence of a swapElement method (ie. use the predefined process symbol).one that does one pass through the array and returns a boolean if items were swapped (I'll call this the one pass method) another method that repeatedly calls the one pass method until that method returns false.This means that your flowchart will have two separate pieces for the one pass method and for the overall flow of the program (this one would use the predefined process symbol for the one pass method).
I have looked up what a bubble sort. It says it is another type of sort that involves repeatedly stepping through the array comparing adjacent items and swapping them if they are in the wrong order.The sort steps through the list multiple times until no swaps are needed, at which point the list is sorted.
So in my java class we are suppose to ask for how many names are going to be entered, create an array of that length, and then store that many inputted names. We then take those names, alphabetize them, and create a set of pairs. For example, it might look like this:
How many people are there? 4 Please enter the names of those people: Dana Bob Alice Charlie
There are 6 possible pairings. They are: Alice & Bob Alice & Charlie Alice & Dana Bob & Charlie Bob & Dana Charlie & Dana
We are also suppose to account for less than two names (both 1 and 0 as well as negatives).
I keep getting a null point exception error and I can't figure out why. Even debugging in Eclipse doesn't give any pointers except that it is originating in line 76.
import java.util.Scanner; import java.util.Arrays; class partners { public static int numNames; public static int numPairs;
I'm trying to read user input from the terminal and separate the input into separate arrays depending on if the user input is an integer, scanner, or a string. The terminal should keep asking the user for input until the user types "quit".
import java.util.*; public class arrayScanner { public static void main(String[] args) { ArrayList<Integer> intList = new ArrayList<Integer>(); ArrayList<Double> doubleList = new ArrayList<Double>(); ArrayList<String> otherList = new ArrayList<String>();
I am trying to get the average of 3 different fraction arrays. I made a fraction class and I made methods such as read() and average() in this new class.
package fractions; import java.util.Scanner; import java.util.Arrays; public class FractionArrays { public static void main(String[] args) { Fraction completeFraction = new Fraction(5,6);
[Code] ....
I was wondering if there was any way to use the arrays I created in the read method in the average method. If I find that out I should be able to do it on my own. Is there a way to make the arrays public to use in different methods?
I am trying to create a program that reads a sentence, such as: "abba is running to the radar" scans this sentence, and then prints out all the palindromes. I am running into issues with my arrays and for statements. Here is my code:
static String palindrome, backwardsLower, palindromeLower, palindromeClean, backwards2, backwards = ""; static String[] words; static int counter; public static void main(String[] args) { palindrome = JOptionPane.showInputDialog("Please enter a phrase. " +
[Code] ...
I am aware that there is a few "useless" variables in there at the moment, I will clean them up (as well as some useless statements, I see those too). The issue comes at about line 17. The variable backwards REMOVES all the spaces from the array, so when it comes time to compare the strings, it is comparing individual words to the ENTIRE string, thus no words will ever be a palindrome.
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);
I am getting incombatable types, I do not know why I am getting them..why I am getting the error?
The Error I am getting: stringSort.java:26: error: incompatible types if(myArray[j].compareToIgnoreCase(myArray[i].toString())){ ^ required: boolean found: int */
We were suppose to make a program for an assignment and the prof provided some codes to start of. What does this basically mean? How do i access the string arrays from consolelist?
class ConsoleInfo { private String conTitle; private double conPrice; private int conQty; private String conPic; private static String empPassword; ConsoleInfo(String title, double price, int qty,String pic)
I'm having trouble with sorting Strings- 3 strings inputted by user, and I would like to output them in alphabetical order. I've used the str.compareToIgnoreCase method, and then I've tried to loop them through a series of if/ else statements. Everything I've been able to find online (including the forums here) has suggested to use the Comparator class, or to put the strings into an array, and sort list- I really would like to stick with just the String class, and its methods .
The program itself works and compiles, but I am getting logic errors that I have been unable to solve. I'm using IntelliJ Idea, and I've ran it through the built in debugger, about 100+ times (not exaggerating, lol) just to see what it's doing in particular scenarios. For instance, I can get c, a, b, to print out as a,b,c correctly, but a,b,c, will print out as b,a,c.
For me this is kind of like a Sudoku puzzle, or a Rubik's cube! Each time I fix one scenario, it breaks another one, so I don't know if there's a(logic) solution to fix all possible scenarios (abc, acb, bac etc... to all print abc) or if possibly I just need more if statements. I've only pasted in the area where I'm having problems (the if statements). I'm a big fan of the "Next Line" syntax.
(Note: please assume the non relevant content- import Scanner class, main method, etc... I didn't want to paste the entire program.)
System.out.println("Enter the first statement: "); //input.nextLine(); string1 = input.nextLine(); System.out.println("Enter the second statement: "); string2 = input.nextLine(); System.out.println("Enter the third statement: "); string3 = input.nextLine();
When out is equal to the String "2x2.5", the array operations ends up looking like this when it is printed using the toString method:
[, , , x]
As you can see, before the array element x, there are three String variables which only contain whitespace. Why does this occur, and how can I prevent this from happening?
I am working on a class project where I have to give the program a predefined String, have it output the first char, then the second char, then the String backwards, and then the String as it originally was. The problem is that it's not out putting all of the information that I need it to. Here's my code:
Here is the class that prints the first char, second char, etc.:
public class Word { private String word; public Word() { } public Word(String s) { setString(word);
[code]....
Here's the main class:
public class WordRunner { public static void main(String[] args) { Word run = new Word(); run.setString("Hello");
How would I go about and make an enum, that has Strings and Methods?I want to make a class called GraphicalEffects, this class and be instanstiated and it has a method to apply graphicalEffects AS methods or some type of references to methods in an ArrayList.
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.