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

Thread: string length problem

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question string length problem

    Hello, I tried to get the program to tell me when I entered more or less than 11 digits, but even if I made sure to enter exactly 11, it still said " number must be 11 digits long. " What am I doing wrong?
    import java.util.Scanner;
     
    public class temp {
    	public static void main(String[] args) {
    		Scanner scanner = new Scanner(System.in);
    		System.out.println("enter a number");
    		String number = scanner.nextLine();
    		int numberlength = number.length();
    		while (numberlength != 11) {
    			System.out.println("the number must be 11 digits long");
    			number = scanner.nextLine();
    		}
    		System.out.println("done.");
    	}
    }
    Last edited by ma5sacre; February 3rd, 2014 at 01:01 PM. Reason: solved.


  2. #2
    Junior Member Tjones787's Avatar
    Join Date
    Jan 2014
    Location
    Lagos, Nigeira
    Posts
    16
    My Mood
    Asleep
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Re: string length problem

    Your program have an infinite loop, check your loop body. What counts the loop.
    Tjones787

  3. #3
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: string length problem

    Quote Originally Posted by ma5sacre View Post
    but even if I made sure to enter exactly 11, it still said " number must be 11 digits long. " What am I doing wrong?
    Simply that when you are into the loop, the numberlength variable is not updated.
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  4. The Following User Says Thank You to andbin For This Useful Post:

    ma5sacre (February 3rd, 2014)

  5. #4
    Member
    Join Date
    Oct 2013
    Location
    United Kingdom
    Posts
    62
    Thanks
    1
    Thanked 4 Times in 4 Posts

    Default Re: string length problem

    Please check for the above advise provided. If you still have issues then please can you post your input, output and error(if received) for this program.
    Thanks and regards,
    Sambit Swain

  6. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: string length problem

    What am I doing wrong?
    Try debugging the code by printing out the value of numberlength after it is assigned a value so you can see what the computer sees. Seeing the output should help you find what you are doing wrong.
    If you don't understand my answer, don't ignore it, ask a question.

  7. The Following User Says Thank You to Norm For This Useful Post:

    ma5sacre (February 3rd, 2014)

  8. #6
    Junior Member
    Join Date
    Jan 2014
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: string length problem

    Thanks guys! I forgot you have to update the numberlength variable in the loop. Should work fine now!

Similar Threads

  1. Trying to trim and get length then parse from string into Double
    By MissMandy in forum Collections and Generics
    Replies: 3
    Last Post: March 27th, 2013, 11:23 AM
  2. Trying to create an alphabet-only string of any length
    By skw4712 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 19th, 2012, 06:39 AM
  3. Weird String .length() method
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: July 11th, 2012, 10:30 AM
  4. Maximum length of a string
    By ranjithfs1 in forum Java Theory & Questions
    Replies: 3
    Last Post: March 6th, 2012, 09:47 AM
  5. [SOLVED] Problem using the length of a string to bound the number of iterations of a for loop
    By dtitt3 in forum Loops & Control Statements
    Replies: 1
    Last Post: November 3rd, 2011, 01:44 PM

Tags for this Thread