File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Rearranging string value Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Rearranging string value " Watch "Rearranging string value " New topic
Author

Rearranging string value

danny dich
Greenhorn

Joined: Mar 06, 2013
Posts: 13
l want to rearrangea string value from user input and display it back to the user
example "Hello my friend" will be rearranged to "friend my hello". How can l do it using the string class methods
such as substring(), replace() e.t.c
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3528
    
  15
danny dich wrote:l want to rearrangea string value from user input and display it back to the user
example "Hello my friend" will be rearranged to "friend my hello". How can l do it using the string class methods
such as substring(), replace() e.t.c

If you know what methods are available for the String class, what have you tried ? Experimentation is a much better learning aid than being told what to use.


Joanne
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Welcome to the ranch Danny. We encourage self learning here. Please study the String class and its available APIs carefully. Try out things on your own as Joanne pointed out correctly. It shall benefit you more.


~ Mansukh
danny dich
Greenhorn

Joined: Mar 06, 2013
Posts: 13
thank very much guys for the advice it was really helpful, my skills are improving . Any advice you can give me on understanding loop statement and choosing which loop statement on using a certain problem. i`m having some problems solving problems with loop statements, l want to understand the nature of loops more.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

The nature of loops is simply that they are used to repeat a set of statements zero or more times.

There are 3 kinds of loop in Java--for, while, and do/while. Any functionality implemented in any one of them can be implemented in either of them. They are entirely interchangeable.

By convention:

  • For loops are used for iterating over a set of items whose size can be determined at iteration time, or for doing something a specific number of times. "For each item in this list..." or "Do this as many times as the number the user entered."
  • While loops are used to do something repeatedly until some condition separate from the looping construct changes, where the task may be executed zero or more times. "As long as there is more data to be read from the server..." or "As long as the user keeps entering 'Y' to go again."
  • Do/while are the least common. They're like while loops, but the condition is tested after the body is executed, so where while loops execute zero or more times, do/while execute one or more. They're typically used like while loops, but when you want to perform the task at least once, and then see if there's a need to continue performing it. "Ask the user for input, and keep asking until he gives valid input." User will provide input at least once. If it's valid, that one time will be enough, but if not, we'll keep looping until he gets it right.


  • Other than that, what sepcifically are you having trouble with, loop-wise?
    danny dich
    Greenhorn

    Joined: Mar 06, 2013
    Posts: 13
    thank you, that was really helpful information, l can really use it. l was having problems to identify the right loop for the problem, but l think now l have a bit of more understanding of the loops with the information you gave me. Maybe you can help me with string problem. l want to check if the user string line has a question mark at the end and that will be boolean for the if statement. l have this written the code below.

    String sentence;
    Scanner Keyboard = new Scanner(System.in);
    System.out.println("Enter a sentence");
    sentence = Keyboard.nextLine();
    int text = sentence.length();



    if(sentence.charAt() ='?')
    {

    System.out.println("Its a question")

    }

    l having problems comparing the last character of the user input with char '?', check if it has a question at the end of it. l have read through the API i can`t find anything, and l can`t understand some of the String methods in the API, im still starting on java. any advice?
    Jeff Verdegan
    Bartender

    Joined: Jan 03, 2004
    Posts: 6109
        
        6

    There are two small things wrong with this line:


    Do you understand the docs for charAt()? It's a pretty straightforward method. Go back and read them and then describe here what you think the method does.
    danny dich
    Greenhorn

    Joined: Mar 06, 2013
    Posts: 13
    Returns the character at index of the string more like:
    sentence.charAt(9);
    That is if l know the index of the question mark, but im dealing with user input, the character '?' the question mark can be sitting at any index. The user input can be an length.
    Jeff Verdegan
    Bartender

    Joined: Jan 03, 2004
    Posts: 6109
        
        6

    danny dich wrote:Returns the character at index of the string more like:
    sentence.charAt(9);
    That is if l know the index of the question mark, but im dealing with user input, the character '?' the question mark can be sitting at any index. The user input can be an length.


    So you don't know ahead of time what index to look at. But you're only concerned if it comes at the end of the String right?

    So, you need a way to find the index of the end of the String.

    "A?"
    "AB?"
    "ABC?"

    What is the index of the question mark in each of those Strings? How do you think you might be able to get the equivalent index on any arbitrary String? What information would you need? Where do you think you might get that information?
    danny dich
    Greenhorn

    Joined: Mar 06, 2013
    Posts: 13
    yes l`m only concerned if it comes at the and of the string. Getting the equivalent index on any arbitrary String. l would use the method, indexOf(string) more like:

    int position = sentence.indexOf("ABC?");
    System.out.println(int);

    that will get me the index of the question mark

    "A?" index of question mark = 1
    "AB?" index of question mark = 2
    "ABC?" index of question mark = 3
    Jeff Verdegan
    Bartender

    Joined: Jan 03, 2004
    Posts: 6109
        
        6

    danny dich wrote:yes l`m only concerned if it comes at the and of the string. Getting the equivalent index on any arbitrary String. l would use the method, indexOf(string) more like:

    int position = sentence.indexOf("ABC?");
    System.out.println(int);

    that will get me the index of the question mark


    No, it will get you the index of "ABC?", which will be 0.

    "A?" index of question mark = 1
    "AB?" index of question mark = 2
    "ABC?" index of question mark = 3


    Okay, but you need to find the pattern so that you can describe the index in terms of that pattern for any arbitrary string. If I give you

    String s = "...?";

    where "..." might be any number of characters, including 0, can you tell me what the index of the ? is, in terms of some property of the String?
    danny dich
    Greenhorn

    Joined: Mar 06, 2013
    Posts: 13
    the index of string s = 3
    Jeff Verdegan
    Bartender

    Joined: Jan 03, 2004
    Posts: 6109
        
        6

    No. You missed the point.

    I have a String. I'm not telling you how long it is. There is an X at the end of that String. It's the last character. What is the index of that X?
    danny dich
    Greenhorn

    Joined: Mar 06, 2013
    Posts: 13
    Thanks jeff l manage to figure out something, indexOf() was the right method, all l needed to do was to make a boolean expression, below is the code l was working, it works perfectly!

    public class OneLineSentence {


    public static void main(String[] args)
    {
    Scanner keyboard = new Scanner(System.in);
    System.out.println("Enter a sentence.");
    String sentence;
    sentence = keyboard.nextLine();
    int text = sentence.length(); // for even and odd boolean

    boolean checkQuestionMark = sentence.indexOf("?") > 0;
    boolean checkExclamationMark = sentence.indexOf("!") > 0;



    int even, odd;

    even = text % 2;

    if(checkQuestionMark && even == 0)
    System.out.println("Yes");

    else if(checkQuestionMark && !(even ==0))
    System.out.println("No");

    else if(checkExclamationMark)
    System.out.println("Wow");

    else
    System.out.println("Always say " + "\"" +sentence+ "\"");
    Jeff Verdegan
    Bartender

    Joined: Jan 03, 2004
    Posts: 6109
        
        6

    You're on the right track, but sentence.indexOf("?") > 0 only tells you if the string contains a '?' somewhere. It doesn't tell you if it's at the end or in the middle or the beginning or if half the characters are '?'. All of those cases will return true.

    You want to check if there's a '?' at a particular index, and I've been trying to get you to think about how you can describe that index in terms of what you know or can find out about the String.

    Going back to our earlier exercise, you correctly stated that:

    "A?" -- index is 1
    "AB?" -- index is 2
    "ABC?" -- index is 3

    But why is that the case? What's the pattern, the rule? Or, to put it differently, how could you fill in the blank to make the following sentence true for ALL of those strings, as well as any other string with a '?' at the end?
    "The index of '?' is equal to ___________ ."

    (And by the way, it's so blasted simple and obvious that once you get the answer, you're going to kick yourself. Or me. )
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Rearranging string value