简体   繁体   中英

Java generating a random ISBN number with strings?

I want to create an ISBN-Number according to this scheme:

L1L2 −B1B2B3 −V1V2 −C 

The title number B1B2B3 has to be ≥ 100 while L1L2 and V1V2 have to be greater than 0. The check digit 0 ≤ C ≤ 9 should be calculated like this:

C = L1#2 + L2 + B1#2 + B2 + B3#2 + V1 + V2#2 mod 10 

with

i#2 = i∗2 (or) i#2 = i∗2−9 (if i∗2 ≥ 10 )

Now, I need to do this with Strings within this method here:

public static String makeISBN()
{
    // Do NOT change the declaration of the following variables!
    String L1L2;
    String B1B2B3;
    String V1V2;
    String C;

    // generate randoom ISBN here   

    // Do not change the following line
    return L1L2+ "-" + B1B2B3+ "-" + V1V2+ "-" + C;
}

Within this method the numbers L1L2, B1B2B3, V1V2, and C have to be randomly generated or be computed (in the case of C). They should be assigned to the predefined String variables and it is important that the numbers L1L2 and V1V2 are saved as Strings of two digits. For instance, the number 3 should be saved as the String “03“. Generate a total of three random numbers, one for each block.

I really have trouble understanding this, it says that I may use the DecimalFormat thingy but well.. I don't know how to do this, has anyone got an idea how this is supposed to be solved?

the hashtag operation is calculated with this method here:

// multiplies i with 2 and subtracts 9 if result is >= 10
public static int hashOp( int i )
{
    // Do NOT change this method!
    int doubled = 2 * i;
    if ( doubled >= 10 ) {
        doubled = doubled - 9;
    }
    return doubled;
}

and in the main string method I'm just going to call the makeISBN() method and print it the the console a bunch of times.. Thanks for any help!

edit: The question is to generate the entire ISBN-Number according the definitions stated in my question.. I may have not been precise enough - it's the strings that confuse me because since now I've never worked with strings in this fashion - how would I change the random numbers into strings again? - this exercise really confuses me..

I tried to do something with Math.random() but I'll only get numbers below 0, so I surely go about this the wrong way - what's the right way?

I will give you some clues.

  1. Use Math.random() to generate your number (multiply by example 10 to have between 0,10), be careful B1B2B3 needs to be above 100 so you can't just multiply with 100, this is a clue: Min + (int)(Math.random() * ((Max - Min) + 1))

  2. Use DecimalFormat to keep the zeros since if we get 1 we like to have 01 new DecimalFormat("00") DecimalFormat

  3. Use charAt or substring on your String to get individual values of es. L1 (Integer.parseInt).

  4. Do you calculations.

And you should be home free...

Okey, it took me a while to do these exercises, I was occupied with something else in the mean time but yea, here's my solution:

    public static String makeISBN()
    {
        String laendercode;
        String bandnr;
        String verlagsnr;
        String checksum;

// Generate Random Numbers for L1L2-B1B2B3-V1V2
        double L1 = Math.random()*(10);
        double L2 = Math.random()*(10);

        double B1 = Math.random()*(10);
        double B2 = Math.random()*(10);
        double B3 = Math.random()*(10);

        double V1 = Math.random()*(10);
        double V2 = Math.random()*(10);

// Check that L1L2 > 0  
        if((int)L1 == 0 && (int)L2 == 0) {
            L2++;
        }
// Check that L1B2B3 >= 100         
        if((int)B1 == 0) {
            B1++;
        }
// Check that V1V2 > 0          
        if((int)V1 == 0 && (int)V2 == 0) {
            V2++;
        }
// Compute check digit with hashOp method       
        double C = (hashOp((int)L1) +L2 + hashOp((int)B1) +B2 + hashOp((int)B3) +V1 + hashOp((int)V2))%10;

// Convert the generated numbers to String      
        laendercode     = (int)L1+""+(int)L2;
        bandnr          = (int)B1+""+(int)B2+""+(int)B3;
        verlagsnr       = (int)V1+""+(int)V2;
        checksum        = (int)C+"";

        return laendercode + "-" + bandnr + "-" + verlagsnr + "-" + checksum;
    }

and this here is the hashOp method:

public static int hashOp(int i)
{
    // used to determine C
    int doubled = 2 * i;
    if ( doubled >= 10 ) {
        doubled = doubled - 9;
    }
    return doubled;
}

I hope this will help someone else too, thanks for the clues.. it wasn't that hard, I don't know why I was confused at the beginning.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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