-4

Example: If i give a number 12345 , i should get an answer like 15243

in the same way if it is 123456 , i should get 162534

i have already tried getting the first value and appending the last value using reverse technique

public class MyFirstJavaProgram {

  public static void main(String []args) {

    String str = "12345";
    String val = str;

    char a;
    int num=0;
    int d=0;
    int n;

    for(int i=0; i<=str.length()/2; i++) {
      a = str.charAt(i);

      num = num*10+Character.getNumericValue(a);
      if(Integer.parseInt(str)!=0){
          d=Integer.parseInt(str)%10;
          num = num*10+d;
          n=Integer.parseInt(str)/10;
          str = Integer.toString(n);
      }          
    }
    System.out.println(num);

  }
}

i should get the result if they give even number or odd number

Prasad Karunagoda
  • 2,048
  • 2
  • 12
  • 16
Sravya
  • 65
  • 13
  • Don't tag posts with irrelevant tags just to get more visibility for your question. That is obnoxious. – Ian Kemp Jan 07 '19 at 10:50
  • https://stackoverflow.com/questions/8033550/convert-integer-to-array-of-digits Possibly this as a starting point – JamesS Jan 07 '19 at 10:53
  • What's the logic? 12345 becoming 15234 I understand, but why 15243? Same for 123456 becoming 162534, I'd expect 162345. – Dominique Jan 07 '19 at 10:53
  • A `String` is basically an array of characters. So what I assume you want to do is basically rearranging an array. So there should be no point in using `Integer.parseInt` or `Integer.toString` whatsoever. Also, you seem to manipulate the same `str` (`str = Integer.toString(n)`) that you are getting your values from (`a = str.charAt(i)`) while in a `for` loop. That's probably not a good idea... you might want to store the inbetween results in a different variable. – Corak Jan 07 '19 at 10:57
  • appending first and last digits of the number – Sravya Jan 07 '19 at 10:58
  • 1
    @Dominique - I *think* the algorithm is: first + last, then second + second to last, third + third to last etc. – Corak Jan 07 '19 at 10:59
  • @Corak that is the assumption I made. it is not clear. – NickJ Jan 07 '19 at 11:01

5 Answers5

3

Without doing what is presumably homework for you, imagine you have a loop in which there are two integer variables a and b. Variables a and b are string indexes.

You are taking characters from the string at positions a,b,a,b,a,b etc.

BUT the values of a and b need to change for each iteration. If the length of the String is n, a will follow the sequence 0,1,2,3... and b will follow the sequence (n-1),(n-2),(n-3) etc

The loop should continue while a < b.

NickJ
  • 9,380
  • 9
  • 51
  • 74
0

This is my solution for your exercise:

Method parameter "A" is Integer number you want to parse.

First you create Char Array from given number and then iterate through it. If i%2 == 0 it means that you take number from beginning otherwise from the end

public static int algorithm(int A) {
    StringBuilder shuffleNumber = new StringBuilder();
    char[] numbersArray = Integer.toString(A).toCharArray();
    for (int i = 0; i < numbersArray.length; i++) {
        if (i % 2 == 0)
            shuffleNumber.append(numbersArray[i / 2]);
        else
            shuffleNumber.append(numbersArray[numbersArray.length - i / 2 - 1]);
    }
    return Integer.parseInt(shuffleNumber.toString());
}
Rafał Sokalski
  • 1,817
  • 2
  • 17
  • 29
0

If you want a solution without string methods, there is a not so complicated one:

public static void main(String[] args) throws IOException {
    String str = "1234567";
    int len = str.length();
    int num=0;
    char a;

    for(int i = 0; i < len / 2; i++) {
        a = str.charAt(i);
        num = num * 10 + Character.getNumericValue(a);
        a = str.charAt(len -1 - i);
        num = num * 10 + Character.getNumericValue(a);
    }

    if (len % 2 == 1) {
        a = str.charAt(str.length() / 2);
        num = num * 10 + Character.getNumericValue(a);
    }

    System.out.println(num);
}

will print

1726354

Check the last if that takes care the case of odd number of digits in the number.

forpas
  • 160,666
  • 10
  • 38
  • 76
0
public class MyFirstJavaProgram {

public static void main(String []args) {

String str = "12345678";
int val = str.length();

char a;
int num=0;
int d=0;
int n;

for(int i=0; i<=str.length()-2; i++)
{
      a = str.charAt(i);
      num = num*10+Character.getNumericValue(a);
      if(Integer.parseInt(str)!=0)
      {
          d=Integer.parseInt(str)%10;
          num = num*10+d;
          n=Integer.parseInt(str)/10;
          str = Integer.toString( n );
      }

}

if(val%2!=0)
{
    num = num*10+Integer.parseInt(str)%10;
     System.out.println(num);
}
else{System.out.println(num);}

}
}

this is working for my question... Thanks all

Sravya
  • 65
  • 13
0

Following is my solution -

 public static void solution(String s) {
        StringBuffer st = new StringBuffer();

        for (int i = 0; i < s.length() / 2; i++) {
            st.append(s.charAt(i));
            st.append(s.charAt(s.length() - 1 - i)); // appending characters from last
        }
        if (s.length() % 2 != 0) {
            st.append(s.charAt(s.length() / 2));
        }
        System.out.println(Integer.parseInt(st.toString()));
    }

my logic is to keep appending first and last character to new string till i < s.length/2. If string is of odd length , it means only last character is remaining, append it to your resultant string. Else , no character is left and you have your complete string.

prachi
  • 305
  • 1
  • 7