ISSN验证..无法读取字符串中的整数值.. [英] ISSN validation.. cannot read integer values from a string..

查看:56
本文介绍了ISSN验证..无法读取字符串中的整数值..的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试读取一个字符串,该字符串应包含8个字符,因此我可以根据下面描述的文章将数字相加并计算ISSN的校验位。



到目前为止,问题是找到一种方法来计算数组的内容,即

ISSN = 0378-5955

ie

1.为前七位数添加每个数字的数值

2.计算的模数(1)/ 11

如果没有剩余的支票数字为0,否则从11中减去余数值给出校验位



来源: ISSN



I am trying to read a string which should consist of eight characters, so I can add up the digits and compute the check digit of an ISSN, according to the article described below.

So far the problem is to find a way to count the contents of the array i.e.
ISSN=0378-5955
i.e.
1. add the numeric value of each number for the first seven digits
2. modulus of (1)/11 calculated
if there is no remainder the check digit is 0, otherwise the remainder value is subtracted from 11 to give the check digit

Source:ISSN

public void setISSN(String ISSN)
  {
      String[] ISSNArray = ISSN.Split();
      int [] ISSNSequence = { };
      int SumOfFIrstSevenDigits = 0;
      for (int i = 0; i <= ISSNArray.Length - 1; i++)
      {

      }
  }

推荐答案

int lastDigit = 0;
// Unicode guarantees the digits are in order and consecutive values
int sum = ISSN.Sum(c => char.IsDigit(c) ? lastDigit = (int)c - (int)'0' : 0);
sum -= lastDigit;
int mod = sum % 11;
int checkDigit = 0;
if (mod != 0)
  checkDigit = 11 - mod;

if (checkDigit == lastDigit)
{
  // true if the lastDigit IS the correct checkDigit
}



您给出的示例(0378-5955)似乎是无效的 ISSN。

校验位应为4.








The example you gave (0378-5955) appears to be of an invalid ISSN.
The check digit should be 4.



int lastDigit = 0;
int digitPosition = 8;
int sum = ISSN.Sum(c => {
  if (!char.IsDigit(c))
    return 0;
  lastDigit = (int)c - (int)'0';  // Unicode guarantees the digits are in order and consecutive values
  return digitPosition-- * lastDigit;
});
if (digitPosition != 0)
  throw new ArgumentException("Incorrect number of digits in ISSN");
sum -= lastDigit;
int mod = sum % 11;
int checkDigit = 0;
if (mod != 0)
  checkDigit = 11 - mod;

if (checkDigit == lastDigit)
{
  // true if the lastDigit IS the correct checkDigit
}


public void setISSN(String ISSN)
   {
       // validate ISSN
       int[] d = new int[ISSN.Length];
       int c1 = 0; int c2 = 0; int c3 = 0;
       for (int i = 0; i <= ISSN.Length; i++)
       {
           if (i <= 6)
           {
               d[i] = int.Parse(ISSN[i].ToString());
               c1 += d[i];
           }

           c2 = c1 % 11;
           c3 = 0;
           if (c2 == 0) { c3 = 0; }
           else { c3 = 11 - c2; }

           /* Sourced from http://en.wikipedia.org/wiki/International_Standard_Serial_Number,
            * last updated on 23rd September 2013 */
       }
       if (ISSN[6] != c3) throw new System.ArgumentException("ISSN may be invalid", "ISSN Validation");
   }


这篇关于ISSN验证..无法读取字符串中的整数值..的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆