Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to organize strings through a sort method
    primarykey
    data
    text
    <p>This is a program that lets user input 6 random letters, and the program sorts these letters in order. Ex user inputs j, d, e, l, w, q and program outputs d, e, j, l, q, w.</p> <p>Unfortunately the code freaks out and does not sort a thing. Note: I am using a GUI</p> <p>Under public I created a class and an array to eventually house all inputted letters</p> <pre><code>class Abc { String letter; Abc (String _letter) { letter = _letter; } } ArrayList &lt;Abc&gt; alphabet = new ArrayList &lt;Abc&gt;(3); //note its 3, not 6 like in the example </code></pre> <p>After user types in a letter in a textField, they press the "addButton" which adds and saves the value in the array.</p> <pre><code>String letter = letterField.getText(); //Store values in array Abc a = new Abc(letter); alphabet.add(a); </code></pre> <p>Now for the actual 'sorting' part. Which takes place after user presses a "Play" button.</p> <pre><code>String[] abc = new String[3]; //LINE I FORGOT TO ADD for (int k = 0; k &lt; abc.length; k++) { abc[k] = letterField.getText(); int x; for (int i = 0; i &lt; abc.length; i++) { // Asume first value is x x = i; for (int j = i + 1; j &lt; abc.length; j++) { //find smallest value in array (random) if (abc[j].compareToIgnoreCase(abc[x]) &lt; 0) { x = j; } } if (x != i) { //swap the values if not in correct order final String temp = abc[i]; abc[i] = abc[x]; abc[x] = temp; } textArea.append(abc[i] + "\n");// Output correct order } } </code></pre> <p>I had originally used this code to sort integers, the only difference between that program and this program is the int/String and this one I am currently working on allows the user to input the letters and the program does not randomize them like it did with the integer program. </p> <p>I had thought this would be enough code to do the trick and organize some letters, but apparently not.</p> <p>For the actually problem, when I input the letters and add them to the array and press "play" the program freaks and a lovely error pops up...</p> <pre><code>Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "d" </code></pre> <p>Note: 'd' being the last letter I inputted for the last time I tested the program...all of five seconds ago.</p> <p>Any hints or advice would be greatly appreciated! </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload