具有字符串void java的返回值的不兼容类型

时间:2011-09-12 19:40:10

标签: java

我在尝试运行代码时遇到错误

Project1.java:5:不兼容的类型 发现:无效 必需:java.lang.String         String creditcard = mainintro();                                    ^ Project1.java:14:无法从结果类型为void的方法返回值         返回信用卡;                ^ 2个错误

import java.util.*; // so that I can use Scanner

public class Project1{
public static void main(String[] arg){
    String creditcard=mainintro();
    checksize(creditcard);
    checkprefix(creditcard);
}

public static void mainintro(){//Displays the intro message
    Scanner console = new Scanner(System.in);
    System.out.println("Enter a credit card number from 13 to 16 digits      long:");
    String creditcard = console.next();
    return creditcard;
}

public static void checksize(String size){

int stringsize=size.length();//The Length of the creditcard

if(stringsize>=17||stringsize<=12){//Checks the size
System.out.println("The Credit card must be 13 to 16 numbers.");
System.out.println("");
System.out.println(size);
mainintro();
}else{System.out.println(size);}

}

public static void checkprefix(String creditcard){
System.out.println(creditcard);
}
}

如果我定义String creditcard =“”;在主要它工作,但只使用“”作为变量?我该如何解决这个问题?

1 个答案:

答案 0 :(得分:3)

变化:

public static void mainintro()

要:

public static String mainintro()