简体   繁体   English

如何解决不兼容的类型:使用 arrays 在 java 中返回错误?

[英]how do I resolve an incompatible types: return error in java with arrays?

Here is my unfinished code.这是我未完成的代码。 I have arrays in the phone book class and the main is calling a method to print out everything in the arrays, but there is an error.我在电话簿 class 中有 arrays ,主要是调用一种方法来打印出 arrays 中的所有内容,但是出现错误。

Main class:主class:

import java.util.Scanner;
public class Main {
  public static void main(String[] args) {
    
    Scanner myObj = new Scanner(System.in);
    System.out.println("What would you like to do: \n 1) ADD to phone book \n 2) DELETE from phone book \n 3) CALL \n 4) PRINT phone book \n 5) quit");
    int input = myObj.nextInt();


    //constructor scanner inputs
    String ISpeedDial = myObj.nextLine();
    String Iname = myObj.nextLine();
    String Inumber = myObj.nextLine();
    //constructor
    PhoneBook b1 = new PhoneBook(ISpeedDial, Iname, Inumber);

    if (input == 1) {//add contact
// scanner saying what is the name of your new contact? what is the # of you new contact? Would you like this contact to be on your spead dails. 
//ptints your new contat is:____ and their # is ___-___-____ and the your Spead dial number is_

    }
    else if (input == 2) {//delete contact
// 
    }
    else if (input == 3) {//make call(from contact or not?)

    }
    else if (input == 4) {//print phone book
    b1.print();
    System.out.println(b1.print() + " ");
    //System.out.print(SD0[i] + " ");
    //t1.Right();//gets right triangle t/f
    //System.out.println("It is a right triangle: " + t1.Right());

    }
    else if (input == 5) {//quit
      return;
    }
    else {//invalid number
      System.out.println("invalid input: " + input);
      return;
    }

  }
}

PhoneBook class:电话簿 class:

public class PhoneBook {

  //instance variables
  private String SpeedDial;
  private String name;
  private String number;

//Account constructor
public PhoneBook(String MSpeedDial, String Mname, String Mnumber){

SpeedDial = MSpeedDial;
name = Mname;
number = Mnumber;
}

//arrays
  String [] SD0 = new String[3];
  String [] SD1 = new String[3];
  String [] SD2 = new String[3];
  String [] SD3 = new String[3];
  String [] SD4 = new String[3];
  String [] SD5 = new String[3];
  String [] SD6 = new String[3];
  String [] SD7 = new String[3];
  String [] SD8 = new String[3];
  String [] SD9 = new String[3];

//method to deposit a specified amount into the account
public void add(){




  SD0[0] = "0";
  SD0[1] = "jo";
  SD0[2] = "123";

}

public void delete(){
  
}
//getter method to return balance
//named without void because it will return a value unlike the ones above
public String print(){
  for (int i = 0; i < SD0.length; i++) {  
    return SD0[i];  
  }
  for (int i = 0; i < SD1.length; i++) {  
    return SD1[i];  
  }
  for (int i = 0; i < SD2.length; i++) {  
    return SD2[i];  
  }
  for (int i = 0; i < SD3.length; i++) {  
    return SD3[i];
  }
  for (int i = 0; i < SD4.length; i++) {  
    return SD4[i];  
  }
  for (int i = 0; i < SD5.length; i++) {  
    return SD5[i];  
  }
  for (int i = 0; i < SD6.length; i++) {  
    return SD6[i];  
  }
  for (int i = 0; i < SD7.length; i++) {  
    return SD7[i]; 
  }
  for (int i = 0; i < SD8.length; i++) {  
    return SD8[i];  
  }
  for (int i = 0; i < SD9.length; i++) {  
    return SD9[i];  
  } 
  return;
}

}

My error is:我的错误是:

PhoneBook.java:76: error: incompatible types: missing return value
  return;

Does any one know how to fix that issue?有谁知道如何解决这个问题?

Also, is there a way to shorten lines(46-75) in the phone book class, of for-loops that prints each array?此外,有没有办法缩短电话簿 class 中打印每个数组的 for 循环中的行(46-75)?

Thank you!谢谢!

Your last return in the print method returns nothing, when String is expected:当需要 String 时,您在 print 方法中的最后一次返回什么也不返回:

  ...
  for (int i = 0; i < SD9.length; i++) {  
    return SD9[i];  
  } 
  return;

You can consider using String [][] SD = new String[10][3];您可以考虑使用String [][] SD = new String[10][3]; instead of having all the SDx as a variable而不是将所有SDx作为变量

The return statement returns immediately. return 语句立即返回。 It's not building a string.它不是在构建字符串。 So your code is going to return SD0[0] as long as SD0 has length > 0.因此,只要 SD0 的长度 > 0,您的代码就会返回 SD0[0]。

The error is the final return with no argument at all.错误是完全没有参数的最终返回。 return "" will fix the compile error, but it probably isn't doing what you really want. return "" 将修复编译错误,但它可能没有做你真正想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM