Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 14 of 14

Thread: school project

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default school project

    Hey Im new to java and have a pretty basic question. I have a homework assignment in which i have to write a program that reads a line of text as input, then moves the FIRST word to the END of the sentence.
    So if I typed "Hello my name is Robin", it would read "My name is Robin hello" (Note the first letter became capital). Any help would greatly be appreciated!


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: school project

    What have you done so far?

  3. #3
    Junior Member
    Join Date
    Nov 2010
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: school project

    not much, i really dont know where to get started...
    import java.util.Scanner; 
    public class lab4 {
    	static Scanner keyboard = new Scanner(System.in); 
        public static void main(String[]args) 
    { 
     
        System.out.print("Type a short sentence for me: " ); 
        String sentence; 
        sentence = keyboard.nextInt(); 
     
    // i really dont know where to start. If you could guide me in the right direction, i would appreciate it

  4. #4
    Member
    Join Date
    Oct 2010
    Location
    Denver, CO
    Posts
    55
    Thanks
    1
    Thanked 30 Times in 29 Posts

    Default Re: school project

    this link : Java 2 Platform SE 5.0 Will tell you about the next() methods, nextInt() is to get a number, you want next() or nextLine()

  5. #5
    Junior Member
    Join Date
    Nov 2010
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: school project

    I think it has to do with the "Methods in the Class String" section of my textbook, however, I don't see one which switches words. Also, if the user puts in their own input, I won't know which index the last word starts at. This is very frustrating! : (

  6. #6
    Junior Member
    Join Date
    Nov 2010
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: school project

    thanks I'll check that out

  7. #7
    Member
    Join Date
    Oct 2010
    Location
    Denver, CO
    Posts
    55
    Thanks
    1
    Thanked 30 Times in 29 Posts

    Default Re: school project

    Check out the charAt() or indexOf() methods of the String class

  8. #8
    Junior Member
    Join Date
    Nov 2010
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: school project

    im still stumped on this...I know how to switch letters around with a sentence, because I can see all of the indexs. But since the sentence has to be user input, how am I supposed to know what the indexs are?

  9. #9
    Junior Member
    Join Date
    Nov 2010
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: school project

    I really would appreciate some help. This project is due tomorrow....

  10. #10
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: school project

    Quote Originally Posted by robin28 View Post
    Hey Im new to java and have a pretty basic question. I have a homework assignment in which i have to write a program that reads a line of text as input, then moves the FIRST word to the END of the sentence.
    So if I typed "Hello my name is Robin", it would read "My name is Robin hello" (Note the first letter became capital). Any help would greatly be appreciated!
    If you have a space between words, read it from the document with inFile.next().

    That will stop every time it has a line.

    I'm not sure if it'll go to the next line after that, but if not, it can read in each word.

  11. #11
    Member
    Join Date
    Oct 2010
    Posts
    40
    Thanks
    0
    Thanked 2 Times in 1 Post

    Default Re: school project

    split the sentence using split("//s") method, this will return u an array of String. u can then put the entry on idex 0 to the end of the array and print.

  12. #12
    Member
    Join Date
    Oct 2010
    Posts
    40
    Thanks
    0
    Thanked 2 Times in 1 Post

    Default Re: school project

    i hope its not too late for your assignment..

     
    import java.io.InputStreamReader;
    import java.util.Scanner;
     
    class Test{
       public static void main(String[] args) {
           Scanner sc = new Scanner(System.in);
     
           String sen = sc.nextLine();
     
           String[] words = sen.split("\\s");
     
           String newS = "";
     
           for(int x=1;x<words.length;x++){
               newS = newS.concat(words[x])+" ";
           }
     
           newS = newS.concat(words[0]);
     
           newS = newS.toLowerCase();
     
           char s = newS.charAt(0);
     
           String x = (s+"").toUpperCase();
     
           String endString = x+(newS.substring(1, newS.length()));
     
           System.out.println(endString);
     
     
     
        }
     
    }

  13. #13
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: school project

    Quote Originally Posted by relixus View Post
    i hope its not too late for your assignment..
    Don't show off. Posting full solutions deprives the original poster of a learning opportunity, and is against the spirit of a forum.

    db

  14. #14
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: school project

    Quote Originally Posted by robin28 View Post
    I really would appreciate some help. This project is due tomorrow....
    Your first mistake was leaving it to the last minute to complete. Never a good idea!

    relixus has offered you a solution. Hopefully you have learnt something along the way.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

Similar Threads

  1. School Help
    By JavaNewb in forum Loops & Control Statements
    Replies: 1
    Last Post: March 24th, 2010, 09:31 PM
  2. Can anyone help me on my project?
    By alesana514 in forum Paid Java Projects
    Replies: 1
    Last Post: December 16th, 2009, 09:24 AM
  3. Need a project
    By helloworld922 in forum Project Collaboration
    Replies: 6
    Last Post: July 31st, 2009, 08:30 AM
  4. Java program for to implement supermarket menu
    By 5723 in forum AWT / Java Swing
    Replies: 1
    Last Post: April 14th, 2009, 03:14 AM
  5. How to use for loop for movement of points in a picture display?
    By Dman in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 8th, 2009, 09:19 AM