-2

I am doing this in Java

I want to split digits of an Integer,

int Pin=125;

To an array,

int Pin_Extracted={Pin[0], Pin[1], Pin[2]};

Code:

import java.io.*;
class name {
    public static void main(String args[])throws IOException {
        InputStreamReader ir = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(ir);
        int Pin;
        String Names[][] ={ {"Mr.","Mrs.","Miss.","Dr.","Sir","Late","Professor","Gadha","Master","Teacher"},{"Abhigyan","Akashdeep","Anish","Adarsh","Ashutosh","","Anik","Shivam","",""},{"Saha","Mukkherjee","Pandey","Shaw","Bannerjee","Dey","Gupta","Singh","",""} };        
        System.out.println("Enter your 3 digit code name :");
        Pin=Integer.parseInt(br.readLine());
        char cn[]=Pin.toCharArray();
        int cn1=cn[0], cn2= cn[1], cn3= cn[2];
        String Name1=Names[0][cn1], Name2=Names[1][cn2], Name3=Names[2][cn3];
        System.out.println(Name1+" "+Name2+" "+Name3);
    }
}
4
  • 1
    Please share whatever you have done so far. Commented Mar 17, 2016 at 13:23
  • Thanks for letting us know you're doing that in Java. But try asking a question and providing some code ;) Commented Mar 17, 2016 at 13:24
  • dropbox.com/s/szaoqoe1yif5i0i/drop.txt?dl=0 This is my program, Iwant to have code name for every single name but it says java ArrayIndexOutOfBoundsException : 48 Commented Mar 17, 2016 at 13:41
  • @AbhigyanSingh Editing the post instead of adding a dropbox link would be amazing :) Commented Mar 17, 2016 at 13:43

2 Answers 2

1

Try this:

public void int [] convertToArray(int pin){
     StringBuilder sB = new StringBuilder();
     sB.append(pin);
     String toConvert = sB.toString();
     int [] splittedValue = new int [toConvert.length];
     for(int i = 0; i < toConvert.length; i++){
        splittedValues[i] = Integer.parseInt(toConver.charAt(i));
    }
 return splittedValues; 
 } 

I think this works...regards

Sign up to request clarification or add additional context in comments.

2 Comments

There is no good reason to use a StringBuilder here. Just saying.
Nor is there a good reason to use Integer.parseInt - Character.getNumericValue is easier.
0

You'll need to convert the integer to a string first.

String pinStr = Integer.toString(Pin);

Then iterate over the new String as an array of characters, assigning them to a new array of ints as you go.

int [] pinExtracted = new int[pinStr.length()];

for (int i = 0; i < pinStr.length(); i++){
    pinExtracted[i] = Integer.parseInt(pinStr.charAt(i));
}

5 Comments

Please refrain from adding similar solutions to ones already available to not promote duplication [reference]
I used it but it said " some messages have been simplified,recompile with- Xdiags:verbose to get full output ".
Normally there should be a more useful error message along with that. Please check for another error message and double check the code (e.g. .length() instead of .length).
Now it says : char cannot be converted into java.lang.string
Sorry, use Character.getNumericValue(pinStr.charAt(i)) instead of Integer.parseInt().

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.