-2

Thank you in advanced for your help. I am still a beginner java student and I realize there are similar questions, but none specifically answer the question at my level. Essentially a user inputs a long number that requires I treat it like a string, and then turn it into an array. The assignment specifically states, "The .parseInt() method and the Integer class can be your friend in this assignment" I've read and searched through my textbook, scoured the internet, and have not been able to find anything helpful. How can I create an integer array from the string described above? When I call the method later I receive an error. Below is what I have so far. If you could direct me to a question I have not seen that's fine, but it would really help if someone could answer my question and correct my logic. Thanks again!

**I did turn the string into a char array, but it's not necessary. If I need to change it I can.

public int [] ctCd(char [] cardArray){
    int [] creditCard = new int [cardArray.length];
    for (int i=0; i <= 15; i++){
        int newNum = Integer.parseInt(cardArray[i]);
        creditCard [i] = newNum;
    }
    return creditCard;
}
jjacobe
  • 33
  • 5
  • In order for us to be able to answer your question, we need... well... a question. – Joe C Nov 12 '17 at 23:02
  • i was also searching for the question ...thaught i miss sth. – IEE1394 Nov 12 '17 at 23:02
  • i don't know if you've been told to use `Integer.parseInt` to parse a character but that's not valid. I believe the method parameter should be `String [] cardArray` instead of `char [] cardArray`. this would prevent the compiler error at `int newNum = Integer.parseInt(cardArray[i]);` – Ousmane D. Nov 12 '17 at 23:02
  • Sorry! I was so focused on describing my problem I forgot to state my question. – jjacobe Nov 12 '17 at 23:06
  • Thanks for our response, Aomine! Follow up question, would I just use the split function later, or is it not necessary? – jjacobe Nov 12 '17 at 23:07
  • 1
    *"The .parseInt() method and the Integer class can be your friend in this assignment"* I don't know how much clearer that hint can be, frankly. – Joe C Nov 12 '17 at 23:09
  • @jjacobe if you've got to the point of passing the String array into the provided method then there is nothing else to be done as far as i know but you know better than me about your requirements. – Ousmane D. Nov 12 '17 at 23:10
  • @JoeC thank you for being frank, but if you are not going to help please stop commenting. I am not a senior software developer, so that hint is not super useful to me. If it is helpful to you please explain. – jjacobe Nov 12 '17 at 23:14
  • `creditCard[i] = Integer.parseInt(String.valueOf(cardNumber.charAt(i)));` - or even better, `creditCard[i] = Character.digit(cardNumber.charAt(i), 10);` – Elliott Frisch Nov 12 '17 at 23:22

1 Answers1

0

The problem you are running into is that .parseInt() takes in a String argument, not a char argument. In your code, you are trying to pass in a char, which is why you are getting an error. You simply have to convert the char object to a String object, which would look like this:

int newNum = Integer.parseInt(String.valueOf(cardArray[i]));
Zebs
  • 27
  • 1
  • 1
  • 10