Witam mam takie pytanko gdyż chce aby program sprawdzał numer woluminu c i zapisywał go do pliku czy może mi ktoś z tym pomóc.Pozdro
import java.io.*;
public class GetVolumeSerialNumberDemo {
public static void main(String args[]) {
System.out.println("Volume Serial ID = <" + getVolumeSerialId() + ">");
}
private static String getVolumeSerialId() {
try {
Process p = Runtime.getRuntime().exec("cmd /C dir");
BufferedReader in = new BufferedReader(
new InputStreamReader(p.getInputStream()));
String line = null;
int count = 0;
while ((line = in.readLine()) != null) {
count++;
if (count<2)
continue;
if (count>2)
break;
return line.substring( line.lastIndexOf(' ')+1 );
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
znalazłem taki kodzik w sieci ale niechce sie kompilowac wyskakuje bląd missing return statement i nie wiem o co chodzi Pomoze ktos???
"Missing return statement" oznacza "brak instrukcji return".
To akurat wiedziałem bo jak wpisałem w gogle błąd to wyskoczylo ale nie znalazłem odp na ten problem i dlatego pisze
Przeanalizuj kod. Widać wyraźnie, że jeżeli warunek w pętli while zawiedzie to instrukcja return się nie wykona, a taka funkcja nie ma prawa bytu w Javie. Zatem można podejrzewać, że return jest w niewłaściwym miejscu, no nie?
import java.io.*;
public class GetVolumeSerialNumberDemo{
static String wynik=null;
private static void getVolumeSerialId() {
try {
Process p = Runtime.getRuntime().exec("cmd /C dir");
BufferedReader in = new BufferedReader(
new InputStreamReader(p.getInputStream()));
String line = null;
int count = 0;
while ((line = in.readLine()) != null) {
count++;
if (count<2)
continue;
if (count>2)
break;
wynik=line.substring( line.lastIndexOf(' ')+1 );
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String args[]) throws IOException {
getVolumeSerialId();
System.out.println("Volume Serial ID = <" + wynik + ">");
BufferedWriter writer=new BufferedWriter(new FileWriter("C:/plik.txt"));
writer.write(wynik);
writer.close();
}
}
Mozartello:
Do zwracania wyniku używa się "return" a nie jakiejś "z d**y" zmiennej. To bardzo zły nawyk.
donkey7 napisał(a)
Mozartello:
Do zwracania wyniku używa się "return" a nie jakiejś "z d**y" zmiennej. To bardzo zły nawyk.
Co to znaczy "z d**y"? Podaj twoją propozycję rozwiązania. Sam jestem ciekaw jak to zrobić
Zwracanie wartości przez przypisanie do specjalnie stworzonego pola jest idiotyzmem. A rozwiązanie jest banalne
import java.io.*;
public class GetVolumeSerialNumberDemo {
public static void main(String args[]) {
System.out.println("Volume Serial ID = <" + getVolumeSerialId() + ">");
}
private static String getVolumeSerialId() {
try {
Process p = Runtime.getRuntime().exec("cmd /C dir");
BufferedReader in = new BufferedReader(
new InputStreamReader(p.getInputStream()));
String line = null;
int count = 0;
while ((line = in.readLine()) != null) {
count++;
if (count<2)
continue;
if (count>2)
break;
return line.substring( line.lastIndexOf(' ')+1 );
}
} catch (IOException e) {
e.printStackTrace();
}
return "a kuku";
}
}