简体   繁体   English

有人可以帮我解决这个toString方法吗

[英]Can someone help me fix this toString method please

I have a method which converts string to a MeterNumber. 我有一种将字符串转换为MeterNumber的方法。 The code is as follows. 代码如下。

public static MeterNumber fromString(String number) {
      MeterNumber mn = new MeterNumber(false);
      int i = 0, // position in number
          n = 0, // number of digits
          len = number.length();
      boolean valid = true;
      while (valid && i < len) {
         char c = number.charAt(i);
         i = i + 1;
         if (n < 9 && Character.isDigit(c)) {
            mn.digits[n] = c - '0';
            n = n + 1;
         } else if (Character.isWhitespace(c)) { 
            // skip spaces
         } else {
            valid = false;
         }
      }
      int cs = mn.checksum();
      if (valid && i == len && n == 9 &&
          cs / 10 == mn.digits[7] && cs % 10 == mn.digits[8]) {
         return mn;
      } else { 
         return null;
      }
   }

It's suppose to read an input like this: 假设读取这样的输入:

531 481 889 O 788.5
652 364 795 P 2442.7

It will work fine if I just provide: 如果我只提供以下内容,它将正常工作:

531 481 889
652 364 795

However it returns null if my input is in this format: 但是,如果我的输入采用以下格式,它将返回null:

531 481 889 O 788.5
652 364 795 P 2442.7

Can you please help me see the problem. 您能帮我看看问题吗? Why is the Char and double at the end of the line interfering? 为什么行末尾的Char和double会干扰?

This is the code calling for the method. 这是调用该方法的代码。

 private MeterNumber meterNumber; 


public static Meter load(Scanner sc) {
      MeterNumber meterNumber = MeterNumber.fromString(sc.nextLine());
      int n = 1;
      sc.nextLine();
      Tariff[] tariffs = new Tariff[n];
      for (int i = 0; i < n; i++) {
         String kind = sc.next();
         sc.nextLine();
         if (kind.equals("P")) {
            tariffs[i] = PeakTariff.PEAK_TARIFF;
         } else if (kind.equals("O")) {
            tariffs[i] = OffPeakTariff.OFF_PEAK_TARIFF;
         } 
      }
      return new Meter(meterNumber, tariffs);
   }

public MeterNumber getMeterNumber() {
      return meterNumber;
   }

}

This is the code calling from my main method: 这是从我的main方法调用的代码:

Scanner sc2 = new Scanner(new java.io.File("Readings.txt"));
        Meter meter = Meter.load(sc2);

        System.out.println(meter.getMeterNumber());

Thanks in advance. 提前致谢。

It returns null because you said it wasn't valid. 它返回空值,因为您说这是无效的。

  while (valid && i < len) {
     char c = number.charAt(i);
     i = i + 1;
     if (n < 9 && Character.isDigit(c)) {
        mn.digits[n] = c - '0';
        n = n + 1;
     } else if (Character.isWhitespace(c)) { 
        // skip spaces
     } else { //this else is the problem
        valid = false;
     }
  }

You told it to set valid to false if it is neither whitespace nor a digit. 您告诉它如果既不是空格也不是数字,则将valid设置为false。 Since O and P are not digits or whitespace, it sets valid to false. 由于O和P不是数字或空格,因此将有效设置为false。

  if (valid && i == len && n == 9 &&
      cs / 10 == mn.digits[7] && cs % 10 == mn.digits[8]) {
     return mn;
  } else { 
     return null;
  }

The first thing the if statement checks is if it is valid. if语句检查的第一件事是它是否有效。 Since it is not, it return null. 由于不是,它返回null。

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

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