• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Reading a String backward

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In my very last method ( int BTD) I am trying to read a string input form right to left when the input for the integer starts with one ( in this case a negative number , since I am testing for binary ) . Basically I am doing the � Two�s Complement� method on the negative number turn it into a positive binary number and just add the �-� negative sign in front of it , something like ( result = �-� + result ) . So I have no problem taking the positive binary number and converting to decimal , but can not figure out the correct way to code the negative number block of code ? Please any suggestions would be of great help !! listed below is my code , it attaches to a user class that gets the I/O but my professor did not give us access to that code . Thank you everyone for the insight !

 
Marshal
Posts: 79039
375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To reverse a String: easy.
  • Use its toCharArray() method to get it into an array.
  • Write a [public static void] swapPair(char[] arr, int i, int j) method.
  • Write a [public static void] reverseArray(char[] arr) method which iterates along � the array and repeatedly swaps pairs ef elements. Be careful about the numbering of the elements. to avoid ArrayIndexOutOfBoundsExceptions.
  • Pass the resulting char[] array to the constructor of the String class.
  • I am sure there are other ways to do it.

    It would have been a lot easier if you had been told to use 32-character Strings, since int numbers use 32 bits.

    I hope that has given you something to get on with. Good luck with the exercise.
     
    Sheriff
    Posts: 22780
    131
    Eclipse IDE Spring VI Editor Chrome Java Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    new StringBuilder(myString).reverse().toString();

    Although this does create a new object.
     
    I claim this furniture in the name of The Ottoman Empire! You can keep this tiny ad:
    a bit of art, as a gift, that will fit in a stocking
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic