| | |
creating a method for texts
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Nov 2007
Posts: 18
Reputation:
Solved Threads: 0
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...
oh I've just figured out. however, i see see the "end". how do i remove it?
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...
Java Syntax (Toggle Plain Text)
String s = new String(""); String s1 = new String(""); System.out.println("enter String : "); Scanner keyboard = new Scanner(System.in); while(!s.equals("end")){ s = keyboard.nextLine(); s1 += s + "\n"; } System.out.println("you've typed: \n"); 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.
•
•
Join Date: Jun 2007
Posts: 59
Reputation:
Solved Threads: 3
Java Syntax (Toggle Plain Text)
String s = new String(""); String s1 = new String(""); System.out.println("enter String : "); Scanner keyboard = new Scanner(System.in); while(!s.equals("end")){ s = keyboard.next(); s1 += s+" "; // or s1 += s+"/n"; } System.out.println("you've typed: \n"); System.out.println(s1);
•
•
•
•
Java Syntax (Toggle Plain Text)
String s = new String(""); String s1 = new String(""); System.out.println("enter String : "); Scanner keyboard = new Scanner(System.in); while(!s.equals("end")){ s = keyboard.next(); s1 += s+" "; // or s1 += s+"/n"; } System.out.println("you've typed: \n"); System.out.println(s1);
Java Syntax (Toggle Plain Text)
String s = new String(""); String s1 = new String(""); System.out.println("enter String : "); Scanner keyboard = new Scanner(System.in); while(!s.equals("end")){ s = keyboard.nextLine(); if (!s.equals("end")) { s1 += s + "\n"; } } System.out.println("you've typed: \n"); System.out.println(s1);
Or keep the while-loop as it is and use the methods:
String.indexOf()
and
String.subString()
Java Syntax (Toggle Plain Text)
int index = s1.indexOf("end"); 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
•
•
•
•
That is not what enuff4life has asked. Try this:
Java Syntax (Toggle Plain Text)
String s = new String(""); String s1 = new String(""); System.out.println("enter String : "); Scanner keyboard = new Scanner(System.in); while(!s.equals("end")){ s = keyboard.nextLine(); if (!s.equals("end")) { s1 += s + "\n"; } } System.out.println("you've typed: \n"); System.out.println(s1);
Java Syntax (Toggle Plain Text)
String s = new String(""); String s1 = new String(""); System.out.println("enter String : "); Scanner keyboard = new Scanner(System.in); s = keyboard.nextLine(); while(!s.equals("end")){ s1 += s + "\n"; s = keyboard.nextLine(); } System.out.println("you've typed: \n"); System.out.println(s1);
![]() |
Similar Threads
- All You Need for Accessibility - Resources List (Site Layout and Usability)
Other Threads in the Java Forum
- Previous Thread: A method that returns a collection
- Next Thread: exception handling for wrong input.
| Thread Tools | Search this Thread |
-xlint add android api applet application array arrays automation bi binary blackberry bluetooth chat class classes client code compile compiler component converter database digit eclipse equation error event exception fractal freeze functiontesting game gameprogramming givemetehcodez graphics gui health html hyper ide idea image input int integer j2me java javame javaprojects jetbrains jni jpanel jtable julia learningresources linux list login loop main map method methods mobile myregfun netbeans newbie nonstatic notdisplaying page pearl print problem program programming project qt recursion scanner screen scrollbar server set size sms sort spamblocker sql string superclass swing system thread threads time tree variablebinding windows xor






