0

Given String[] c I am trying to make it so that if "X" is present in the array 10 is added to int xvalue. Otherwise something else happens. This is what I have tried so far I am not sure what exactly is going wrong.

if (i==c.length - 1 && !c[i].contains("X") )
  sum += 0 ;
else if (c[i].contains("X"))
xvalue+=10;
else
  sum += (Integer.parseInt(c[i])*pos) ;

Also the "X" is only ever present in the last value of the array. Thanks for the help.

  • 2
    Possible duplicate of [How can I verify that an array of strings contain a certain string?](https://stackoverflow.com/questions/9105358/how-can-i-verify-that-an-array-of-strings-contain-a-certain-string) – Drew Delano Sep 22 '19 at 22:55

1 Answers1

1

Don't know your actual logic make it simple

if (c[i].contains("X") )
xvalue+=10
else
sum += (Integer.parseInt(c[i])*pos) 

Whats value is printing? Assuming i is a loop value..

Edit: It will check if the value X or not then add 10 value, if its other value its escape...

public void xCheck(String c[]){
    int sum = 0;
    int xValue = 0;
    int pos = 1;
    for (int i = c.length - 1; i >= 0; i--) {
        if (c[i].contains("X")) {
            xValue += 10;

        } else if (pos == 1) {
            pos++;
            continue;
        } else {
            //if(sum==0)
            sum += pos * (Integer.parseInt(c[i]));
        }
        pos++;
    }
    System.out.println(xValue);
    System.out.println(sum);
}
Sourav
  • 9
  • 2
  • This does not seem to work either but to answer your question the first part of the If statement is supposed to make it so the last value of the array is ignored unless it is "X" – Caleb Christensen Sep 22 '19 at 22:46
  • Okay if am I not wrong, your if statement check last char is not X. Okay so if u pass value CAR always C and A, ur default else block to hit and R for if condition. And if you pass value FOX. For first F and O it will hit default else and for X it should hit else if... – Sourav Sep 22 '19 at 23:02
  • I am sorry I did not really show what pos is. What I am trying to do is more like if the array is {"1","2","X"} it should add 10 to integer xvalue and also add (2*2) then add (1*3) (pos represents the position in the array from right to left) So X is in position 1, 2 is in position 2 etc. If it is {"1","2","3"} it should basically ignore "3" and add 0 to sum then continue to 2 and 1. – Caleb Christensen Sep 22 '19 at 23:11
  • As per my understanding check the actual answer's Edit section. – Sourav Sep 23 '19 at 00:13
  • Thank you for trying to help. I think it is a little bit too complicated to communicate over text. I think what you gave me may work but I will have to change some things and try it. – Caleb Christensen Sep 23 '19 at 00:33
  • This has the same problem I discovered I was having earlier. If I replace "X" in the array with "10" and change c[i].contains("X") to c[i].contains("10") than it works but for some reason it does not work. – Caleb Christensen Sep 23 '19 at 00:40
  • It gives me this message: Exception in thread "main" java.lang.NumberFormatException: For input string: "X"at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.base/java.lang.Integer.parseInt(Integer.java:652) at java.base/java.lang.Integer.valueOf(Integer.java:983) at MyClass.main(MyClass.java:34) – Caleb Christensen Sep 23 '19 at 00:40
  • You told that Your X will come always end of String array. I have tried with {"1","2","X"} and {"1","2","3"} both values. Take the data on First line Integer.parseInt(c[i] on iteger catch exception accordingly specify the value on Catch block for X. Hope its help. – Sourav Sep 23 '19 at 00:50