Home > OS >  For loop is not returning a different value for every array index
For loop is not returning a different value for every array index

Time:11-23

I've written a code about a program where a word gets encrypted as a number dependent on a fixed parameter such as "Software" turning into 76389273. However the for loop is not returning different values for every letter, and returns the same value (value of the first letter) for all letters.

import java.util.Scanner;

public class StringtoNumber {
    public static int getNumber(String word) {
        int value = 0;
        int intArray[] = new int[word.length()];
        for (int i = 0; i < word.length(); i  ) {
            switch (word.charAt(i)) {
                case 'A':
                case 'B':
                case 'C':
                case 'a':
                case 'b':
                case 'c':
                    value = 2;
                    break;
                case 'D':
                case 'E':
                case 'F':
                case 'd':
                case 'e':
                case 'f':
                    value = 3;
                    break;
                case 'G':
                case 'H':
                case 'I':
                case 'g':
                case 'h':
                case 'i':
                    value = 4;
                    break;
                case 'J':
                case 'K':
                case 'L':
                case 'j':
                case 'k':
                case 'l':
                    value = 5;
                    break;
                case 'M':
                case 'N':
                case 'O':
                case 'm':
                case 'n':
                case 'o':
                    value = 6;
                    break;
                case 'P':
                case 'Q':
                case 'R':
                case 'S':
                case 'p':
                case 'q':
                case 'r':
                case 's':
                    value = 7;
                    break;
                case 'T':
                case 'U':
                case 'V':
                case 't':
                case 'u':
                case 'v':
                    value = 8;
                    break;
                case 'W':
                case 'X':
                case 'Y':
                case 'Z':
                case 'w':
                case 'x':
                case 'y':
                case 'z':
                    value = 9;
                    break;
            }
            intArray[i] = value;
            return intArray[i];
        }

        int result = 0;
        for (int i = 0; i < intArray.length; i  ) {
            result  = Math.pow(10, i) * intArray[intArray.length - i - 1];
        }
        return result;
    }

        public static void main (String[]arg){

            Scanner input = new Scanner(System.in);
            System.out.println("what word do you want to encrypt");
            String word = input.nextLine();
            int counter = 0;
            while (counter < word.length()) {
                System.out.print(getNumber(word));
                counter  ;
            }
        }
    }

I've tried finding mistake but I haven't seen anything, is there something I am not taking into account?

CodePudding user response:

Please do some progress to debug your program. return intArray[i] will return the value of the first letter when the for loop execute the first time.

CodePudding user response:

You shouldn't put return intArray[i] inside the for loop, as it breaks the loop at the first iteration and finishes getNumber method.

Also you have doubled the for/while loop. As a result, if you fix the problem with getNumber method, you will get encrypted word as many times as word.length().

You should either:

  • iterate the word in main function and return single integer in getNumber method

or

  • change return type for getNumber to int[] (or even better String) and use for loop inside the getNumber - that would be preffered solution

CodePudding user response:

you are returning inside the for loop. So the loop won't continue, and will return the value after the 1st iteration.

  • Related