Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Switching half a string to the front?

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi I have to write a program where I am encrypting a password. For one part of the program I have to switch half of the inputted password with the other half. So for example is my password was "Java" it would become "vaja" but I have no idea how to do this. Can someone please tell me how I would go about doing this? I think I need to create a method for it but I am not sure. Thanks in advanced.
 
Sheriff
Posts: 7113
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Take a look at https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#substring-int-
 
Ranch Hand
Posts: 529
19
Eclipse IDE MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Sing wrote:So for example is my password was "Java" it would become "vaja"

This logic would be for password having even length. Have you thought about password having odd length. Example what if somebody sets password "Javaa" how would you apart them? Also keep in mind password should be case sensitive means "Java" and "java" are not equal. Go to link provided by Knute Snortum. Too many ready made String functions are available, utilize them and update status if you get any problem in code.
 
Marshal
Posts: 76468
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Put the String into a StringBuilder(←link) because that gives much more options for changing things. Note many of its methods return StringBuilder so you can chain method calls like this:-
builder.delete(...).append(...);
 
John Sing
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So I was able to make a little program to split a string in half.

but how do I go about converting this into one string? Because if I just print one string after the other it would give the appearance that it did what it was supposed to do but in truth it actually wouldn't be. Plus there are more steps after this. So what should I do?
 
Campbell Ritchie
Marshal
Posts: 76468
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Sing wrote:. . . but how do I go about converting this into one string? . . .

What is wrong with the String catenation operator? firstHalf + secondHalf

You appear not to have told us the full problem. What is efficient for one stage may turn out to be inefficient when you add the subsequent stages.
 
John Sing
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

John Sing wrote:. . . but how do I go about converting this into one string? . . .

What is wrong with the String catenation operator? firstHalf + secondHalf

You appear not to have told us the full problem. What is efficient for one stage may turn out to be inefficient when you add the subsequent stages.



Actually this would work fine. It was a mistake on my part when I said it wouldn't. Thanks for all the help.
 
keep an eye out for scorpions and black widows. But the tiny ads are safe.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic