-1

I have this method.

The error says

primeList might not have been initialized.

I can't understand how this can be true. I feel like there's no way the variable wouldn't be initialized out of the for loops.

I can't figure out how to initialize a String

public void primesToANumber(long num) //finds all primes
{
    String primeList ;
    long pcount;
    long limit = num;
    JOptionPane.showMessageDialog(null,"Prime numbers between 1 and " + limit);
    for(long i = 1; i < 100; i++)
    {
        boolean isPrime = true;
        for(long j = 2; j < i ; j++)
        {
         if(i % j == 0)
            {
                isPrime = false;
                break;
            }
        }
        // print the number
        if(isPrime)
        {
            primeList += i + ", ";
            pcount++;
            if(pcount % 12 ==0){
                primeList += "\n";
            }
        }
    }
    JOptionPane.showMessageDialog(null, " " + primeList);
}
  • 10
    `String primeList =;` shouldn't even compile – JonK Oct 27 '16 at 13:03
  • 1
    You can't use `+=` on a variable that hasn't been initialised. What are you adding on to? – khelwood Oct 27 '16 at 13:05
  • What makes you think that variable is/would be initialized? – Pshemo Oct 27 '16 at 13:05
  • 1
    What is with all the downvoting? This is a pretty good question for a new user! – musefan Oct 27 '16 at 13:07
  • @Pshemo Well i only started coding about a month ago so I'm just lost on how to initialize this variable. I thought if i defined it outside the outside loop then it would work but apparently not. – Nikita Kovinskiy Oct 27 '16 at 13:07
  • @musefan Because it is basic Java... any tutorial would have covered this. Surprised they got this far – Andrew Li Oct 27 '16 at 13:08
  • You will have an issue with `pcount` as well. Try `long pcount = 0L;` – bradimus Oct 27 '16 at 13:09
  • @Jonk I fixed that. i was working on it in between and copy and pasted the bad version in. It's fixed now – Nikita Kovinskiy Oct 27 '16 at 13:09
  • @AndrewLi: It doesn't matter how *basic* it is. As long as the question meets the asking criteria (which it does). With your logic you might as well down vote all questions and say `"this is advanced Java... any masters degree would have covered this."` – musefan Oct 27 '16 at 13:10
  • 2
    @musefan Sure, but a simple search on google or *this site* would reveal dupes like this which solve the problem... http://stackoverflow.com/q/2448843/5647260 - the downvote tooltip says "does not show research" – Andrew Li Oct 27 '16 at 13:14
  • @AndrewLi: Well then perhaps you should find one that relates to strings and then vote to close this as a dupe – musefan Oct 27 '16 at 13:17
  • @musefan I've already found a duplicate... – Andrew Li Oct 27 '16 at 13:19
  • @AndrewLi: Well I hope you have downvoted that 'basic' question too... – musefan Oct 27 '16 at 13:24
  • 1
    @AndrewLi I did research and explore similar questions but I couldn't figure out how it related to mine. – Nikita Kovinskiy Oct 27 '16 at 13:27

2 Answers2

5

You need to initialise your variable, exactly like it says...

String primeList = "";

Using an empty string will then allow your code to append to it as you intend, to get your desired result.


Just for reference, if you did want to use null for some reason, you can 'convince' the compiler you know what you are doing by specifying it directly:

String primeList = null;

This would compile fine, but you would get a runtime error when you try to append to a null String, so it wouldn't make sense to do this in this instance.

musefan
  • 47,875
  • 21
  • 135
  • 185
3

Try:

String primeList = "";
long pcount = 0;

and I guess it should be:

for (long i = 1; i < limit; i++)

insted of

for (long i = 1; i < 100; i++)
Jhon D.
  • 71
  • 2