Characters From String In Alphabetic Order Using Quicksort
Oct 1, 2014
I am trying to do a program that takes all of the chars from a string and orders them in alphabetical order. It works fine, but when a is a last letter of a string it isn't being sorted.
Example: bcba = bbca, omnibus = bimnous (here u is in wrong place)
Here is my code:
public class sorty{
public static void sort(char[] a, int low, int high){
int i = low;
int j = high;
if (j - i < 2) return;
int m = (j+i)/2;
char p = a[m];
I have to check a String input from the user in the form of firstName lastName (i.e. John Smith). I have to check for an exception called NonAlphabeticCharacterException that gets thrown if there is anything but a number in that string. This is what I have right now but should I create an array of char for the alphabet and then check the whole string for non alphabetic characters?
for(int i=0; i<name.length(); i++) { if()){ throw new NonAlphabeticCharactersException("Non-alphabetic character found"); } }
I am currently trying to make a calculator in Java. I want to use the String split method to tokenize the string of characters inputted. I thought I was using the String split method wrongly, because I had surrounded the characters I wanted to delimit with square brackets. However, when I removed the square brackets, the code threw an exception when I pressed the equal button. The exception was a PatternSyntaxException exception. Am I using the String split method wrongly? And why is the exception thrown? Here is my code:
import javax.swing.*;//import the packages needed for gui import java.awt.*; import java.awt.event.*; import java.util.Arrays; import java.util.List; import static java.lang.Math.*; public class CalculatorCopy { public static void main(String[] args) {
I'm trying to loop through a string and depending on the character, add a JLabel to a game. The problem is the character 'L' represents a lantern but is also used in the reply the game gives which is "LOOKREPLY". I've tried to use some code to ignore the LOOKREPLY bit but it's not working. Here's what I've tried.
But the first image on all of the JLabels is always a lantern, which is what L represents. As it's only 1 lantern this leads me to believe that it's ignoring the first 'L' but for some reason it's not ignoring the 'L' at the end of LOOKREPLY.
As of right now my code can take characters from a string to an array from a string like "ABCD" but the project says I have to take it from a string like "A B C D" how can I correct my code to grab the characters from a single spaced line?
Scanner sc = new Scanner(System.in); System.out.println("Enter Order of Cars:"); String carsInput = sc.next(); int x = carsInput.length(); int[] cars = new int[x]; for (int i=0; i < cars.length; i++) { cars[i] = carsInput.charAt(i)-64; }
So what my program is supposed to do is take a number inputted by the use and then take a phrase. It then changes that phrases letter by the number inputted prior for example if you type in 2 as your int and Hello as your phrase you should get JGNNQ, which i can do. but the problem is that when i run it, it outputs like this:
J G N N Q
As separate characters how can I combine those characters in 1 string so it looks like JGNNQ? this is my code
import java.util.Scanner; public class Dcod_MAin { private static final Object[] String = null; public static void main(String[] args){ Scanner input = new Scanner (System.in); System.out.println("What is the day of the month"); int shift;
I need to make a string filled with naughts and crosses like this one : "xxx ooo xox". There are 3 groups separated with a space. how to fill the string randomly ?
I just need to write a simple program/function that replaces certain letters from a string (i.e. censor( "college", "aeiou" ) returns "cllg"). I'm trying to get the code right first, and then write a function for it.I basically just thought that I would iterate over the first string, and once I had the first character, I would then iterate over the second string, to see if the character exists. I'm getting a "dead code" error on my second loop because I put the second "break."
public class ap { public static void main(String [] args){ String s = "Hello"; String s2 = "aeiou";
I am trying to count the number of non_blank characters in a string. If there are no leading blank spaces it works fine but say i add three spaces in front it doubles the non blank characters.
import java.io.*; import java.util.*; public class countCharacters { public static void main(String[] args) throws Exception { String str1; int count; count = 0;
Error:public String front3(String str) { ^^^^^^^^^^^^^^^^^^ This method must return a result of type String
Possible problem: the if-statement structure may theoretically allow a run to reach the end of the method without calling return. Consider adding a last line in the method return some_value; so a value is always returned.
I am trying to change an input String to an array of characters, but it only stores the word before the space into the array. Here is the code:
Scanner scanner = new Scanner(System.in); System.out.println(" Enter text: " ); String text = scanner.next(); char[] characterArray = text.toCharArray(); // convert string to array of characters String char = ""; for( i = 0; i < characterArray.length; i++) { char = char + characterArray[i] } System.out.println(char);
Just typing hello gives me hello, but when I type hello world it does not type in the word "world".I am trying to change an input String to an array of characters, but it only stores the word before the space into the array.Here is the code:
Scanner scanner = new Scanner(System.in); System.out.println(" Enter text: " ); String text = scanner.next(); char[] characterArray = text.toCharArray(); // convert string to array of characters String char = ""; for( i = 0; i < characterArray.length; i++) { char = char + characterArray[i] } System.out.println(char);
Just typing hello gives me hello, but when I type hello world it does not type in the word "world".
I was trying to create a java program which can remove the repeated characters in a String. For ex-
Input: kamehamehaaa Output: kameh
Here is my code:-
import java.util.Scanner; class replace { public static void main (String args[]) { Scanner br = new Scanner(System.in); System.out.println("Enter a word");
[Code] ....
On executing the program, StringOutOfBoundsIndex error occurs.
I do have a quick question about string manipulation. You see I've been given a simple exercise that involves asking the user to input a number between 1,000 and 999,999 and displaying the result. Simple enough, but the caveat is that if the user keys in the comma, say 24,000 instead of 24000 for example, the program is not to display the comma. I don't see how to do this without an 'if' statement. The book says the 'if' is not necessary but does offer this hint: "Read the input as a string. Measure the length of the string. Suppose it contains n characters. Then extract the substrings consisting of the first n-4 characters and the last three characters."
What good is n-4 going to do if the string's lengths varies?
Here's what I have written thus far:
import java.util.Scanner; public class P13 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Please enter a number between 1,000 and 999,999: ");
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.
I have a college question ask me to write a class StringRevert which does the following:
-prompting user for an interger n -creating an array of n string -repeatedly read character string from user input and store them in the array until end of array is reached or user input -quit(quit should not saved in array) -print the string from array in reverse order, from last enter to first enter. -assume user always supplie correct input
This is what I've done so far but how to get it working.
import java.util.Scanner; public class StringRevert { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println("enter value of n: "); int n1 = in.nextInt(); String[] n = new String[n1];
I am currently writing the Quick Sort implementation where the pivot is chosen to be the medianOf3 element in the sub array. The program should output the total number of comparisons (excluding the ones needed to compute the median itself).I cannot spot any mistake anywhere - yet I am getting the wrong output in the end.
Input file (100.txt) attached. Current output: 513. Correct output: 518. Where are the missing 5 comparisons? />
My objective is to execute quick sort ( i was told to convert the pseudocode from the Cormen book) using arrays of increasing sizes and find the average number of comparisons for each of those sizes over 100 iterations. This is a school project and the numbers I am getting are far larger than those of my friends, so I am clearly doing something wrong. I believe it must be in the way that I am collecting and averaging my number of comparisons. I will first give the method in which most of that calculating is done, then I will include the whole program.
public static void tests(int arraySize) { long numComparisons = 0; long averageComparisons = 0; long[] numComparisonsArray = new long[100]; for(int i = 0; i<100; i++) { int[] array= genRandomArray(arraySize);
I have an error using quicksort and this is a project ... The error occurs for numbers such as 7500 and bigger ...
Exception in thread "main" java.lang.StackOverflowError at QuickSort.QuickSort(QuickSort.java:45) at QuickSort.QuickSort(QuickSort.java:46) at QuickSort.QuickSort(QuickSort.java:46) ...
Java Code:
import javax.swing.JOptionPane; public class QuickSort{ public static void main(String[] args){ int p=new Integer(JOptionPane.showInputDialog("Jepni numrin e kufizave: ")); int[] ListaNumrave= new int[p]; //QuickSort Zbrites
One of the random number generators in Java extract the higher-order bits of the random number in order to get a longer period.
I'm not sure if I understand how this is done. Suppose that the random number r = 0000 1100 1000 1101. If we extract the 16 most significant bits from r; is the new number r = 0000 1100 or r = 0000 1100 0000 0000?
I have a custom linkedList(single) class that uses the provided node class. Now I have another class to QuickSort this.(left out for brevity, i just wanna focus on editing the L.head). However, instead of passing the quicksort method the entire linkedList, I want to pass it just the head from the linkedlist.
My problem is accessing this head node and changing it from the quckSort method/class, and I dont want to delete it or simply just change the element value
Main:
public class TestLinkedList { public static <E extends Comparable<E>> void main(String[] args) { MyLinkedList<Integer> L = new MyLinkedList<Integer>(); L.add(3); L.add(1); L.add(2); System.out.println("Initial=" + L); MySort.quickSort(L.head); System.out.println("After ="+L); } }
QuickSort:
public class MySort { public static <E extends Comparable<E>> void quickSort(MyNode<E> list) { list = list.next; }
Node Class:
public class MyNode<E extends Comparable<E>> { E element; MyNode<E> next; public MyNode(E item) { element = item; next = null;