[Java]Was ist hier falsch?

06/29/2012 15:00 Regen.#1
hey,

bin noch nicht so die Leuchte in Java :o .

kann mir jemand sagen was an diesem Script ausschnitt falsch ist bzw. es Korrigieren?

Code:
for (int i = 0; i < urlList.length; i++) {
            if (skip[i] != 0) {
                percentage = (initialPercentage + fileSizes[i] * 45 / totalSizeDownload);
            }
            else
            {
                try
                {
                    md5s.remove(getFileName(urlList[i]));
                    md5s.store(new FileOutputStream(versionFile), "md5 hashes for downloaded files");
                } catch (Exception e) {
                    e.printStackTrace();
                }

                int unsuccessfulAttempts = 0;
                int maxUnsuccessfulAttempts = 3;
                boolean downloadFile = true;

                while (downloadFile) {
                    downloadFile = false;

                    URLConnection urlconnection = urlList[i].openConnection();

                    String etag = "";

                    if ((urlconnection instanceof HttpURLConnection)) {
                        urlconnection.setRequestProperty("Cache-Control", "no-cache");

                        urlconnection.connect();

                        etag = urlconnection.getHeaderField("ETag");
                        etag = etag.substring(1, etag.length() - 1);
                    }

                    String currentFile = getFileName(urlList[i]);
                    InputStream inputstream = getJarInputStream(currentFile, urlconnection);
                    FileOutputStream fos = new FileOutputStream(path + currentFile);

                    long downloadStartTime = System.currentTimeMillis();
                    int downloadedAmount = 0;
                    int fileSize = 0;
                    String downloadSpeedMessage = "";

                    MessageDigest m = MessageDigest.getInstance("MD5");
                    int bufferSize;
                    while ((bufferSize = inputstream.read(buffer, 0, buffer.length)) != -1)
                    {
                        int bufferSize;
                        fos.write(buffer, 0, bufferSize);
                        m.update(buffer, 0, bufferSize);
                        currentSizeDownload += bufferSize;
                        fileSize += bufferSize;
                        percentage = (initialPercentage + currentSizeDownload * 45 / totalSizeDownload);
                        subtaskMessage = ("Retrieving: " + currentFile + " " + currentSizeDownload * 100 / totalSizeDownload + "%");

                        downloadedAmount += bufferSize;
                        long timeLapse = System.currentTimeMillis() - downloadStartTime;

                        if (timeLapse >= 1000L) {
                            float downloadSpeed = downloadedAmount / (float)timeLapse;
                            downloadSpeed = (int)(downloadSpeed * 100.0F) / 100.0F;
                            downloadSpeedMessage = " @ " + downloadSpeed + " KB/sec";
                            downloadedAmount = 0;
                            downloadStartTime += 1000L;
                        }

                        subtaskMessage += downloadSpeedMessage;
                    }

                    inputstream.close();
                    fos.close();
                    String md5 = new BigInteger(1, m.digest()).toString(16);
                    while (md5.length() < 32) {
                        md5 = "0" + md5;
                    }
                    boolean md5Matches = true;
                    if (etag != null) {
                        md5Matches = md5.equals(etag);
                    }

                    if ((urlconnection instanceof HttpURLConnection)) {
                        if ((md5Matches) && ((fileSize == fileSizes[i]) || (fileSizes[i] <= 0)))
                        {
                            try {
                                md5s.setProperty(getFileName(urlList[i]), etag);
                                md5s.store(new FileOutputStream(versionFile), "md5 hashes for downloaded files");
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        } else {
                            unsuccessfulAttempts++;
                            if (unsuccessfulAttempts < maxUnsuccessfulAttempts) {
                                downloadFile = true;
                                currentSizeDownload -= fileSize;
                            } else {
                                throw new Exception("failed to download " + currentFile);
                            }
                        }
                    }
                }
            }
        }
IntelliJ zeigt mir den Fehler in Zeile 2 an. Das wäre dann das hier:
Code:
if (skip[i] != 0) {
Hier ist nochmal ein Bild von intelliJ:
[Only registered and activated users can see links. Click Here To Register...]

gruß
Regen.
06/29/2012 15:26 Dr. Coxxy#2
kein java experte, aber der fehlermeldung entnehme ich, dass "skip" ein array von booleans ist, und es in java nicht erlaubt ist einen bool mit einem int zu vergleichen.

folgende möglichkeiten:

0 auf ungleichheit mit 0 prüfen:
Code:
if (skip[i] != (0 != 0))
false benutzen:
Code:
if (skip[i] != false)
unnötigen mist lassen:
Code:
if (skip[i])
06/29/2012 23:38 manniL#3
Quote:
Originally Posted by Dr. Coxxy View Post
kein java experte, aber der fehlermeldung entnehme ich, dass "skip" ein array von booleans ist, und es in java nicht erlaubt ist einen bool mit einem int zu vergleichen.

folgende möglichkeiten:

0 auf ungleichheit mit 0 prüfen:
Code:
if (skip[i] != (0 != 0))
false benutzen:
Code:
if (skip[i] != false)
unnötigen mist lassen:
Code:
if (skip[i])
Genau so sieht es aus.

Da wir keine näheren Infos zu deiner skip() Methode haben können wir nur den oben genannten Rat geben.