Programming Questions

  • Newest
  • Popular Tags
  • Ask A Question
  • First Reverse exited with nonzero status - Java
    What does "exited with nonzero status" mean?
    import java.util.*; 
    import java.io.*;
    
    class Main {  
      public static String FirstReverse(String str) { 
        
        String new_string = "";
           
        for (int i = str.length()-1; i >= 0; i++) {
            new_string = new_string + str.charAt(i);
        }
    
        return new_string;
        
      } 
      
      public static void main (String[] args) {  
        // keep this function call here     
        Scanner s = new Scanner(System.in);
        System.out.print(FirstReverse(s.nextLine())); 
      }   
      
    } 
    
    btobin86 posted this question on 1/2/17 | java, first, reverse
    Answers
  • +
  • 4
  • -
  • Variable i should be reduced, not increased in the for loop
    for (int i = str.length()-1; i >= 0; i--) {
            new_string = new_string + str.charAt(i);
        }
  • +
  • 3
  • -
  • it is better to use StringBuilder than String, it is much more memory saving option. String is creating new object every time a next letter is added. StringBuilder create one object and replace it when adding next letter.
  • +
  • 1
  • -
  • i should be reduced for (int i = str.length()-1; i >= 0; i--) {
    Log in to write an answer.