creating a method for texts

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Nov 2007
Posts: 18
Reputation: enuff4life is an unknown quantity at this point 
Solved Threads: 0
enuff4life enuff4life is offline Offline
Newbie Poster

creating a method for texts

 
0
  #1
Nov 11th, 2008
I need to creat a method something like... when the user inputs

test1
test2 haha

test3
end


and display that should contain exact same one as above except "end":
test1
test2 haha

test3

so the "end" is the keyword to finish the inputing...

  1. String s = new String("");
  2. String s1 = new String("");
  3. System.out.println("enter String : ");
  4. Scanner keyboard = new Scanner(System.in);
  5.  
  6. while(!s.equals("end")){
  7. s = keyboard.nextLine();
  8. s1 += s + "\n";
  9.  
  10. }
  11. System.out.println("you've typed: \n");
  12. System.out.println(s1);

oh I've just figured out. however, i see see the "end". how do i remove it?
Last edited by enuff4life; Nov 11th, 2008 at 4:53 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 59
Reputation: Chaster is an unknown quantity at this point 
Solved Threads: 3
Chaster Chaster is offline Offline
Junior Poster in Training

Re: creating a method for texts

 
0
  #2
Nov 11th, 2008
  1. String s = new String("");
  2. String s1 = new String("");
  3. System.out.println("enter String : ");
  4. Scanner keyboard = new Scanner(System.in);
  5.  
  6. while(!s.equals("end")){
  7. s = keyboard.next();
  8. s1 += s+" ";
  9. // or s1 += s+"/n";
  10.  
  11. }
  12.  
  13.  
  14. System.out.println("you've typed: \n");
  15. System.out.println(s1);
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,677
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 226
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: creating a method for texts

 
0
  #3
Nov 12th, 2008
Originally Posted by Chaster View Post
  1. String s = new String("");
  2. String s1 = new String("");
  3. System.out.println("enter String : ");
  4. Scanner keyboard = new Scanner(System.in);
  5.  
  6. while(!s.equals("end")){
  7. s = keyboard.next();
  8. s1 += s+" ";
  9. // or s1 += s+"/n";
  10.  
  11. }
  12.  
  13.  
  14. System.out.println("you've typed: \n");
  15. System.out.println(s1);
That is not what enuff4life has asked. Try this:

  1. String s = new String("");
  2. String s1 = new String("");
  3. System.out.println("enter String : ");
  4. Scanner keyboard = new Scanner(System.in);
  5.  
  6. while(!s.equals("end")){
  7. s = keyboard.nextLine();
  8.  
  9. if (!s.equals("end")) {
  10. s1 += s + "\n";
  11. }
  12. }
  13. System.out.println("you've typed: \n");
  14. System.out.println(s1);

Or keep the while-loop as it is and use the methods:
String.indexOf()
and
String.subString()

  1. int index = s1.indexOf("end");
  2. System.out.println(s1.subString(0,index));

Link for the String class.
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 706
Reputation: stultuske is a jewel in the rough stultuske is a jewel in the rough stultuske is a jewel in the rough 
Solved Threads: 84
stultuske's Avatar
stultuske stultuske is offline Offline
Master Poster

Re: creating a method for texts

 
0
  #4
Nov 12th, 2008
Originally Posted by javaAddict View Post
That is not what enuff4life has asked. Try this:

  1. String s = new String("");
  2. String s1 = new String("");
  3. System.out.println("enter String : ");
  4. Scanner keyboard = new Scanner(System.in);
  5.  
  6. while(!s.equals("end")){
  7. s = keyboard.nextLine();
  8.  
  9. if (!s.equals("end")) {
  10. s1 += s + "\n";
  11. }
  12. }
  13. System.out.println("you've typed: \n");
  14. System.out.println(s1);
or this way:

  1. String s = new String("");
  2. String s1 = new String("");
  3. System.out.println("enter String : ");
  4. Scanner keyboard = new Scanner(System.in);
  5. s = keyboard.nextLine();
  6. while(!s.equals("end")){
  7. s1 += s + "\n";
  8. s = keyboard.nextLine();
  9. }
  10. System.out.println("you've typed: \n");
  11. System.out.println(s1);
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC