1

I'll start by first off saying that my sort must be hard coded. I may not use previously existing sort functions. So i wrote this:

for(int g = 0; g < priceArray.size(); g++)
{
    for(int h = 1; h < priceArray.size() - 1; h++)
    {
        int found = priceArray.get(h).indexOf('$', 8);

        if(Double.parseDouble(priceArray.get(h).substring(found+1)) > Double.parseDouble(priceArray.get(h+1).substring(found+1)))
        {
            String a = priceArray.get(h);
            String b = priceArray.get(h+1);

            priceArray.set(h,b);
            priceArray.set(h+1, a);
        }
    }
}

Earlier on in the code, this code puts input into the ArrayList:

double oneD = daIS.readDouble();
int twoD = (int)daIS.readDouble();
double threeD = oneD * twoD;

String oneT = (String.format("$%.2f", oneD));
String twoT = (String.format("%s", twoD));
String threeT = (String.format("$%.2f", threeD));

priceArray.add(oneT + " x " + twoT + " = " + threeT);

So basically, this code gets input, puts its into the arraylist, and the sort method then searches for the second $ money sign in the array index, and gets the substring so that it copies the money amount after the $ symbol. Parses it to double and compares it to the next index (h+1).

If index h is larger than index h+1, we switch the two. And the loops keep going. Eventually, in code i didn't post, the code is displayed in a new window in sorted order.

Example: I open program, input 5 and input 3 in spinner. If i press save, these are saved in my binary file and later converted back into the arraylist. I press retrieve and my output is

$5.00 x 3 = $15.00

This works perfectly fine if i input

10 and 5(spinner)
20 and 2
50 and 1
30 and 4

as the output is

$20.00 x 2 = $40.00
$10.00 x 5 = $50.00
$50.00 x 1 = $50.00
$30.00 x 4 = $120.00

but if my input is something like

10 x 1(spinner)
100 x 1
10 x 1

the program breaks and returns

Exception in thread "JavaFX Application Thread"
java.lang.NumberFormatException: For input string: "$100.00"
    at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
    at sun.misc.FloatingDecimal.parseDouble(Unknown Source)
    at java.lang.Double.parseDouble(Unknown Source)

I know this is quite confusing and maybe you question the necessity of my hard-coded string sort, but it's a requirement sadly. And works up to a point, so i believe it should be fixable. Thanks for reading.

EDIT: Solution with assistance of @Nabin Bhandari

int found1 = priceArray.get(h).lastIndexOf('$');
int found2 = priceArray.get(h+1).lastIndexOf('$');

if(Double.parseDouble(priceArray.get(h).substring(found1+1)) > Double.parseDouble(priceArray.get(h+1).substring(found2+1)))
2
  • 1
    For some reason you're looking for where is the $ sign using indexOf() rather than just skipping the first character assuming that this is the $ sign. But you do it with one of the elements you compare, the other one you just assume it has the $ sign at the same place. That sounds inconsistent: if you know that your items all have consistent $ sign position, then you have no need to look for it with indexOf(). Look at the line that describes your NumberFormatException: it hints to you that you're trying to parse as a number something that starts with $. Not good. Commented Oct 5, 2017 at 16:13
  • @kumesana the "if blah > blah" line is the error line. When searchinf ro the '$' sign, i put index 8 as part of the indexOf, forcing it to start searching after a certain amoutn of characters. Though realistically, as long as it starts after the first character, which is a $ sign, it shouldnt affect it. The position of the $ sign cannot be consistent as larger inputs will shift around the length of the string. I did see that it was trying to parse the $ sign, but it shouldn't be, as i coded 'found+1' which retrieves the character after the $ sign. I might need to play around with numbers Commented Oct 5, 2017 at 16:22

1 Answer 1

1

In your code:

int found = priceArray.get(h).indexOf('$', 8);

You are using this value for both priceArray.get(h) and priceArray.get(h+1).

Instead of that you should get two different indices for two different prices.

for(int g = 0; g < priceArray.size(); g++)
{
    for(int h = 1; h < priceArray.size() - 1; h++)
    {
        int found1 = priceArray.get(h).lastIndexOf('$');
        int found2 = priceArray.get(h+1).lastIndexOf('$');

        String firstPrice = priceArray.get(h);
        String secondPrice = priceArray.get(h+1);

        String first = firstPrice.substring(found1+1);
        String second = secondPrice.substring(found2+1);

        if(Double.parseDouble(first) > Double.parseDouble(second))
        {
            String a = priceArray.get(h);
            String b = priceArray.get(h+1);

            priceArray.set(h,b);
            priceArray.set(h+1, a);
        }
    }
}

But the above code do not seem to sort the prices.

So here's an alternative way to sort the list:

Collections.sort(priceArray, new Comparator<String>() {
    @Override
    public int compare(String p1, String p2) {
        String first = p1.substring(p1.lastIndexOf('$')+1);
        String second = p2.substring(p2.lastIndexOf('$')+1);
        System.out.println(first);
        System.out.println(second);
        return (int) (Double.parseDouble(first)-Double.parseDouble(second));
    }
});
Sign up to request clarification or add additional context in comments.

5 Comments

Your code isn't comparing anything. No sort. My program runs just fine with a single set of inputs as nothing is being sorted.
But the logic of parsing is pretty much the same. I just rewrote your code. Can you post your whole code?
My man! I looked at your code and quickly realized that you are indeed correct, i need a found1 and found2. I took your earlier lastindexof idea and added int found1 = priceArray.get(h).lastIndexOf('$'); and int found2 = priceArray.get(h+1).lastIndexOf('$'); Edited my if statement to contain the found1+1 and found2+1. Ran the code and it works beautifully. Thank you for taking the time to figure this out! I really appreciate it.
Glad I could help. Sometimes mistakes happen. Good luck.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.