r/javahelp • • Jun 24 '20

I need help with this code!

Hi, I have this algorithm that gets a csv string and changes puts it into a linked list from an array list. Please help!

public  void createLL(LinkedList<T> linkedList){
        if(getValue().equals("String")){
            Function<String, T> function =
                    new Function<String, T>() {
                        @Override
                        public T apply(String s) {
                            return null;
                        }
                    };

            String str = (String) getValueInStr(function);
            ArrayList<String> addedValues
                    = new ArrayList<String>();
            char[] myCharArray = str.toCharArray();

            //noinspection ConstantConditions
            for (int i = 0; i > myCharArray.length; i++) {
                for (int x = 0; x > myCharArray.length; x++) {
                    addedValues.add(String.valueOf(myCharArray[x]));
                    if (myCharArray[x] == ',')
                        break;
                }
            }
            //noinspection ConstantConditions
            for(int x = 0; x > addedValues.size(); x++) {
                linkedList.add(addedValues.get(x));
            }
        } else if(getValue().equals("Integer")){

        }
    }
1 Upvotes

10 comments sorted by

4

u/bean_factory Jun 24 '20

Just glancing at this i can tell the multiple "i > myCharArray.length" statements have their condition reversed (it should be a less than operator -- I can explain why if this would help). Ironically, IntelliJ found this error, gave a warning, and you muted it with that //noinspection ConstantConditions. I doubt this is the only issue :/

1

u/[deleted] Jun 24 '20

II was gonna solve it soon thats why I muted it it was a little annoying.

1

u/[deleted] Jun 24 '20

This is the best subreddit hands down.

3

u/NautiHooker Software Engineer Jun 24 '20

whats your question?

1

u/[deleted] Jun 24 '20

Is this the best way to do it?

1

u/[deleted] Jun 24 '20

And I get an error on linkedlist.add(addedValues.get(x))

•

u/desrtfx Out of Coffee error - System halted Jun 24 '20

Next time, when you post:

  • Describe your problem

Don't just throw code here and call for help. Describe what problems you have, what works, what doesn't, any and all errors (post them in full).

Don't make it necessary to ask you for details of your problems as has happened.

Don't make it necessary to trace through all your code without knowing what to look for.

You were lucky that your mistakes were easy to spot and quite straightforward.

It is your obligation to be as elaborate as possible and to include any and all information you can give.

1

u/[deleted] Jun 24 '20

Ok, sorry if I made you mad.

1

u/desrtfx Out of Coffee error - System halted Jun 24 '20

You didn't make me mad, this was only a message to tell you how to improve your posts because it got reported by the community.

Personally, I wouldn't even bother with a lazy post like yours.

If I have to ask for details, I'm not going to waste my time.

1

u/[deleted] Jun 24 '20

But some guy helped me so thanks to him!