• Post Reply Bookmark Topic Watch Topic
  • New Topic

Substitute ternary operator, sentence reversed  RSS feed

 
Milan Milanovic
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to substitute the following ternary operator but keep the logic, I have troubles writing it with the basic if else statement.
Here is the code...

  temp +=i!=0?str.charAt(i):str.charAt(i)+" ";

I think I know the logic behind it, if temp fulfills the condition assign str.CharAt(i) to temp else assign the other condition to it.
But still, I can't write it down correctly.



 
Junilu Lacar
Sheriff
Posts: 11485
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a problem meant to see if you understand operator precedence rules. It will help if you understand those rules then put parentheses to group the operators and operands that will be evaluated first. Then work your way outwards.

Search for Java Operator Precedence Table
 
Milan Milanovic
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:This is a problem meant to see if you understand operator precedence rules. It will help if you understand those rules then put parentheses to group the operators and operands that will be evaluated first. Then work your way outwards.

Search for Java Operator Precedence Table


Perfect answer, I made a silly mistake until I thought of this.
Here is the answer...

class StringRev{
    public static void main(String args[]){
    String str = "He is the one";
    String temp = "";
    String finalString = "";
        for(int i =str.length()-1;i>=0;i--){
            if(i!=0){
             temp = temp + str.charAt(i);
            }
            else
            {
              temp = temp+ str.charAt(i) + " ";
           
            }
            if(str.charAt(i) == ' '||i==0){
                for(int j=temp.length()-1;j>=0;j--){
                    finalString += temp.charAt(j);
                }
                temp = "";
            }
        }
            System.out.println(finalString);
    }
}
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seem to be ok.

Anyway, as you see, you always do temp = temp + ... and you make a decision only about the space character either to concatenate it or not. You think you could simplify that logic?
However, I don't know if that is what your teacher is expecting.

On a more important side - your code's indentation and formatting is less than optimum. Don't cram everything that way. Don't squash operators like that - leave some spaces around.
 
Fred Kleinschmidt
Bartender
Posts: 571
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps a simpler way iis:
Even better would be to use a StringBuilder instead of String. That way you won't keep creating new String instances for every iteration of the loop.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!