-2

User input a number such as 1234569775 of 10 digit. may on of the digit has a Value of X and this mean its equal to number 10 SO i write my code

    Scanner in=new Scanner(System.in);
    String a=in.next();
    String arr[]=new String[10];
    for (int i=0;i<arr.length;i++)
    {
        arr[i]=String.valueOf(a.charAt(i));
    }
    for (int i=0;i<arr.length;i++)
    {
        if (arr[i]=="X")
        {
            arr[i]="10";
        }
    }
    System.out.println(Arrays.deepToString(arr));
}

but its the value of X does not change and I try to make a new array and does not make any change too
last thing i want to change this String array all value in it to Integer so I can make any Mathematical operation on them So I want

  1. Change value of X to 10
  2. Change the array values to Integer

How can I do that in Java?

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
user7179690
  • 1,051
  • 3
  • 17
  • 40
  • it does not change the value of x too i try now – user7179690 Dec 08 '15 at 20:44
  • okay work what about change array to integer – user7179690 Dec 08 '15 at 20:45
  • iam sorry i really dont know this point i start java from 2 day only and i dont know this point – user7179690 Dec 08 '15 at 20:48
  • This is pretty nit picky Amr but rather you learn early then later. In Java we define arrays like this `String[] arrayName` instead of how you have done it, `String arrayName[]`. They do the same thing, but the java convention is to do it the first way. I almost missed your array at the beginning. Depending on your coding experience, it might be a hard habit to break but someone here will mention it to you sooner or later. – gonzo Dec 08 '15 at 21:07
  • thank you for your advice you welcome :D – user7179690 Dec 08 '15 at 21:57

2 Answers2

1

This is how you can turn a String[] into a int[].

String[] arr = {"1","2"};
int[] intArray = new int[arr.length];
for (int i=0; i<arr.length; i++){
    intArray[i] = Integer.parseInt(arr[i]);
}

You essentially loop through the String[] and parse each String using Integer.parseInt.

Or if you are working in Java 8:

String[] arr = {"1","2"};
int[] intArray = Arrays.stream(arr).mapToInt(i -> Integer.parseInt(i)).toArray();
gonzo
  • 2,103
  • 1
  • 15
  • 27
-1

In java it is not possible to compare strings using " == "

Try:

if (firstString.equals(secondString){
//code...
}

I know thats confusing, as in many other programming languages a double = can compare strings, but again in Java that is not possible.

Edit: to compare Strings ignoring lower and upper case, use

string1.equalsIgnoreCase(string2)

Thats also possible:

if(string.equals("X"))

So in your case, use

if(arr[i].equals("10")
infinitecode
  • 33
  • 1
  • 1
  • 7