2014-10-22 96 views
-6

我不知道這是什麼問題。我用其他代碼的例子做了這個。現在我因爲這件事而陷入困境。Java中的「Missing return statement」錯誤

import java.util.Scanner; 
import java.util.Calendar; 

public class Praktikum3 
{ 
Scanner scan = new Scanner(System.in); 
Calendar cal = Calendar.getInstance(); 

String NIP, nama, kelamin, jabat, sub1, sub2, sub3, sub4; 
int a, b, c, d, e, umur, menjabat, tgllhr, blnlhr, thnlhr, lhr, blnjbt, thnjbt, date, month, year; 

public Praktikum3() //constructor 
{ 
    System.out.printf("DATA KARYAWAN\n\n"); //menginisialkan constructor 
} 

public void setNama() 
{ 
    System.out.printf("Nama: "); 
    nama = scan.next(); 
} 

public String getNama() 
{ 
    return nama; 
} 

public void setNIP() 
{ 
    System.out.printf("NIP : "); 
    while (!scan.hasNextLong()) 
    { 
     System.out.printf("!HARUS ANGKA!\n"); 
     System.out.printf("NIP : "); 
     scan.next(); 
    } 
    NIP = scan.next(); 

    if(NIP.length() != 18) 
    { 
     System.out.printf("!HARUS 18 KARAKTER!\n"); 
     System.out.printf("NIP : "); 
     scan.next(); 
    } 

    sub1 = NIP.substring(0,8); /*substring Tanggal lahir*/ 
    sub2 = NIP.substring(8,14); /*substring Masa menjabat*/ 
    sub3 = NIP.substring(14,15); /*substring Jenis kelamin*/ 
    sub4 = NIP.substring(15,18); /*substring Nomor urut*/ 

    date = cal.get(Calendar.DATE); 
    month = cal.get(Calendar.MONTH); 
    year = cal.get(Calendar.YEAR); 

    a = Integer.parseInt(sub1.substring(6,8)); 
    b = Integer.parseInt(sub1.substring(4,6)); 
    c = Integer.parseInt(sub1.substring(0,4)); 

    d = Integer.parseInt(sub2.substring(4,6)); 
    e = Integer.parseInt(sub2.substring(0,4)); 
} 

public String getSub1() 
{ 
    return sub1; 
} 

public String getSub2() 
{ 
    return sub2; 
} 

public String getSub3() 
{ 
    return sub3; 
} 

public String getSub4() 
{ 
    return sub4; 
} 

public int getTgllhr() 
{ 
    return a; 
} 

public int getBlnlhr() 
{ 
    return b; 
} 

public int getThnlhr() 
{ 
    return c; 
} 

public int getUmur() 
{ 
    tgllhr = date - a; 

    blnlhr = month - b; 
    if(blnlhr < 0) 
     lhr = -1; 
    else if(tgllhr < 0) 
     lhr = -1; 
    else 
     lhr = 0; 

    thnlhr = year - c; 

    umur = thnlhr + lhr; 

    return umur; 
} 

public String getKelamin() 
{ 
    if(sub3.equals("1")) 
     kelamin = "Pria"; 
    else if(sub3.equals("2")) 
     kelamin = "Wanita"; 
    else 
     kelamin = "Ganda"; 
} 

public int getMenjabat() 
{ 
    blnjbt = month - d; 
    if(blnjbt < 0) 
     blnjbt = -1; 
    else 
     blnjbt = 0; 

    thnjbt = year - e; 

    menjabat = thnjbt + blnlhr; 

    return menjabat; 
} 

public static void main(String[]arbi) 
{ 
    Praktikum3 dt = new Praktikum3(); 

    dt.setNama(); 
    dt.setNIP(); 

    System.out.printf("\nData\n----\n"); 
    System.out.printf("Nama   : %s\n", dt.getNama()); 
    //System.out.printf("NIP   : %s.%s.%s.%s\n", dt.getSub1, dt.getSub2, dt.getSub3, dt.getSub4); 
    //System.out.printf("Tanggal lahir : %d-%d-%d\n", dt.getTgllhr, dt.getBlnlhr, dt.getThnlhr); 
    System.out.printf("Umur   : %d tahun\n", dt.getUmur()); 
    System.out.printf("Jenis kelamin : %s\n", dt.getKelamin()); 
    System.out.printf("Lama menjabat : %d tahun", dt.getMenjabat()); 
} 
} 

這是印度尼西亞語。這是我用OOP練習子串的一個。即使是get substring方法也是錯誤的。

錯誤消息:

Praktikum3.java:125: error: missing return statement }

+3

什麼是您的實際和完整的錯誤信息?涉及哪一行代碼? – 2014-10-22 00:22:41

+3

Chit,您的標題完全誤導您的問題與構造函數問題無關。下一次,請多花點精力問一個完整而準確的問題。 – 2014-10-22 00:26:29

+1

Arbintoro,請閱讀http://stackoverflow.com/help/how-to-ask另外,你的函數名稱是印度尼西亞語,請用代碼註釋說明它們,因爲它們不是英文的 – Parker 2014-10-22 00:28:25

回答

3

你錯過了return statement這個method.you應返回String因爲在這個method.and "Constructor Missing Return Statement"return type is string是沒有意義的,因爲constructors haven't a return type

public String getKelamin() 
{ 
    if(sub3.equals("1")) 
     kelamin = "Pria"; 
    else if(sub3.equals("2")) 
     kelamin = "Wanita"; 
    else 
     kelamin = "Ganda"; 
    return kelamin; 
} 
+0

這是錯誤消息 Praktikum3.java:125:錯誤:缺少返回語句 } 現在它的構造函數工作正常。謝謝:) – 2014-10-22 01:17:03

+0

@ArbintoroMas它說,你不會返回一個字符串從這個method.so你需要返回一個字符串。我希望這個問題是fixed.i認爲你是新手爲java.good運氣你 – 2014-10-22 01:23:09

+1

是的先生,我剛開始使用Java編程。感謝您的幫助,儘管這只是我的小錯誤。學到新的錯誤問題。 :) – 2014-10-22 03:26:06

0

終極密碼

import java.util.Scanner; 
import java.util.Calendar; 

public class Praktikum3 
{ 
Scanner scan = new Scanner(System.in); 
Calendar cal = Calendar.getInstance(); 

String NIP, nama, kelamin, jabat, sub1, sub2, sub3, sub4; 
int a, b, c, d, e, umur, menjabat, tgllhr, blnlhr, thnlhr, lhr, blnjbt, thnjbt, date, month, year; 

public Praktikum3() //constructor 
{ 
    System.out.printf("DATA KARYAWAN\n\n"); //menginisialkan constructor 
} 

public void setNama() 
{ 
    System.out.printf("Nama: "); 
    nama = scan.next(); 
} 

public String getNama() 
{ 
    return nama; 
} 

public void setNIP() 
{ 
    System.out.printf("NIP : "); 

    while (!scan.hasNextLong()) 
    { 
     System.out.printf("!HARUS ANGKA!\n"); 
     System.out.printf("NIP : "); 
     scan.next(); 
    } 
    NIP = scan.next(); 

    if(NIP.length() != 18) 
    { 
     System.out.printf("!HARUS 18 KARAKTER!\n"); 
     System.out.printf("NIP : "); 
     NIP = scan.next(); 
    } 

    sub1 = NIP.substring(0,8); /*substring Tanggal lahir*/ 
    sub2 = NIP.substring(8,14); /*substring Masa menjabat*/ 
    sub3 = NIP.substring(14,15); /*substring Jenis kelamin*/ 
    sub4 = NIP.substring(15,18); /*substring Nomor urut*/ 

    date = cal.get(Calendar.DATE); 
    month = cal.get(Calendar.MONTH); 
    year = cal.get(Calendar.YEAR); 

    a = Integer.parseInt(sub1.substring(6,8)); 
    b = Integer.parseInt(sub1.substring(4,6)); 
    c = Integer.parseInt(sub1.substring(0,4)); 

    d = Integer.parseInt(sub2.substring(4,6)); 
    e = Integer.parseInt(sub2.substring(0,4)); 
} 

public String getSub1() 
{ 
    return sub1; 
} 

public String getSub2() 
{ 
    return sub2; 
} 

public String getSub3() 
{ 
    return sub3; 
} 

public String getSub4() 
{ 
    return sub4; 
} 

public int getTgllhr() 
{ 
    return a; 
} 

public int getBlnlhr() 
{ 
    return b; 
} 

public int getThnlhr() 
{  
    return c; 
} 

public int getUmur() 
{ 
    tgllhr = date - a; 

    blnlhr = month - b; 
    if(blnlhr < 0) 
     lhr = -1; 
    else if(tgllhr < 0) 
     lhr = -1; 
    else 
     lhr = 0; 

    thnlhr = year - c; 

    umur = thnlhr + lhr; 

    return umur; 
} 

public String getKelamin() 
{ 
    if(sub3.equals("1")) 
     kelamin = "Pria"; 
    else if(sub3.equals("2")) 
     kelamin = "Wanita"; 
    else 
     kelamin = "Ganda"; 

    return kelamin; 
} 

public int getMenjabat() 
{ 
    blnjbt = month - d; 
    if(blnjbt < 0) 
     blnjbt = -1; 
    else 
     blnjbt = 0; 

    thnjbt = year - e; 

    menjabat = thnjbt + blnlhr; 

    return menjabat; 
} 

public static void main(String[]arbi) 
{ 
    Praktikum3 dt = new Praktikum3(); 

    dt.setNama(); 
    dt.setNIP(); 

    System.out.printf("\nData\n----\n"); 
    System.out.printf("Nama   : %s\n", dt.getNama()); 
    System.out.printf("NIP   : %s.%s.%s.%s\n", dt.getSub1(), dt.getSub2(), dt.getSub3(), dt.getSub4()); 
    System.out.printf("Tanggal lahir : %d-%d-%d\n", dt.getTgllhr(), dt.getBlnlhr(), dt.getThnlhr()); 
    System.out.printf("Umur   : %d tahun\n", dt.getUmur()); 
    System.out.printf("Jenis kelamin : %s\n", dt.getKelamin()); 
    System.out.printf("Lama menjabat : %d tahun", dt.getMenjabat()); 
} 
}