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 9 of 9

Thread: How to place a number in front of a string?!!

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to place a number in front of a string?!!

    Hello
    Guys i working in a program that i enter strings
    until the user write Stop, STOP, stop
    how do i stop it

    i did this

    [
    I'm going out
    Today is hot
    I love to play
     
     
     
    it should print
     
    1 I'm going out
    2 Today is hot
    3 I love to play.
    Last edited by WantHelp; July 4th, 2011 at 01:50 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How to place a number in front of a string?!!

    it didn't work!!!
    Your posted code does NOT show any print statements. You need to show the code where the problem is.
    Read the String class API doc for a method that may do what you want when comparing Strings with different cases.

    Another problem: Use the equals method to compare String objects, not the == operator.

  3. #3
    Member
    Join Date
    Oct 2010
    Location
    UK
    Posts
    42
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: How to place a number in front of a string?!!

    WantHelp are you deleting any posts? I can't see any of your attempted code & I'd like to help

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: How to place a number in front of a string?!!

    Quote Originally Posted by Stockholm Syndrome View Post
    WantHelp are you deleting any posts?
    Yes. I believe OP removes their code as soon as the problem is resolved. Which does make it confusing and frustrating for others who come along afterwards.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How to place a number in front of a string?!!

    In this case the code that was posted was mostly junk. It didn't have any print statements as the title implies. Mostly it was a bunch of if tests using == for Strings.

  6. #6
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: How to place a number in front of a string?!!

    Posters who persistently remove their code from posts should be warned, and banned if they continue to do it.

  7. #7
    Junior Member
    Join Date
    Mar 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to place a number in front of a string?!!

    I used KeyboardReader from TerminalIO, you can change it if you need too. Also change the array size as you need and modify it as you want, I don't know exactly what you're looking for. Hopefully my comments help you, good luck.

    import TerminalIO.KeyboardReader;
     
    public class NumberStrings {
    	public static void main (String[] args) {
     
    		String[] phrases = new String[10]; // Create a string array to hold values you enter
    		phrases[0] = "placeholder"; // Initialize the first position as a placeholder
    		int x = 0; // Create an integer value used to update the array
    		KeyboardReader reader = new KeyboardReader(); // Create a keyboard reader
     
    		// Store the strings until the user enters "stop"
    		while (true) {
    			phrases[x] = reader.readLine();
    			phrases[x+1] = "placeholder";
     
    			// Test whether the current entry contains "stop"
    			if (phrases[x].equalsIgnoreCase("stop")) {
    				break;
    			} else {
    				x++;
    			}
    		}
     
    		System.out.println(); // Create a line space
     
    		// Print each entry
    		for (int y = 0; y < x; y++) {
    			System.out.println((y+1) + ". " + phrases[y]);
    		}
     
    		reader.pause(); // Prevent fly-by
    	}
    }

  8. #8
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: How to place a number in front of a string?!!

    phrases[0] = "placeholder"; // Initialize the first position as a placeholder
    phrases[x+1] = "placeholder";
    Why???

    A. Don't spoonfeed code.
    B. Don't spoonfeed code it if contains bad practises.

    Also, your code will throw an ArrayIndexOutOfBoundsException if user never enters "stop".
    Improving the world one idiot at a time!

  9. #9
    Junior Member
    Join Date
    Mar 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to place a number in front of a string?!!

    Quote Originally Posted by Junky View Post
    phrases[0] = "placeholder"; // Initialize the first position as a placeholder
    phrases[x+1] = "placeholder";
    Why???

    A. Don't spoonfeed code.
    B. Don't spoonfeed code it if contains bad practises.

    Also, your code will throw an ArrayIndexOutOfBoundsException if user never enters "stop".
    Yeah I don't really know what he is looking for and got lazy and made it really fast.

Similar Threads

  1. Replies: 5
    Last Post: April 22nd, 2013, 07:27 AM
  2. Where to place my own library?
    By hexwind in forum Java Theory & Questions
    Replies: 3
    Last Post: June 22nd, 2011, 06:25 AM
  3. Stupid thing can't find image icon right in front of it!!!
    By javapenguin in forum What's Wrong With My Code?
    Replies: 14
    Last Post: July 9th, 2010, 01:02 AM
  4. great place to start
    By flotsam in forum The Cafe
    Replies: 2
    Last Post: April 22nd, 2010, 10:52 AM
  5. [SOLVED] How to string a decimal number in Java?
    By Lizard in forum Loops & Control Statements
    Replies: 6
    Last Post: May 14th, 2009, 03:59 PM