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

Thread: Whats wrong? s.lengt - Counting characters

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

    Default Whats wrong? s.lengt - Counting characters

    public class Setning 
    {
     
    	public static void main(String[] setning) 
    	{		
    				skrivHeleSetningen(setning);				
    	}
     
    	private static void skrivHeleSetningen(String[] setning) 
    	{
     
    		System.out.print("du skrev: ");  
     
            for (String arg : setning) 
            {
                  System.out.print(arg + " ");       //Writes out the whole sentence
            }             
     
            antallOrd(setning);
    			antallTegn(setning);
    	}	
     
     
    		private static void antallTegn(String[]setning)
    		{
    			String s = new String();
    			/*String s= new setning(setning).toString();*/
     
    			/*for (int i=0; i<1; i++)*/
    			while ( s.length() <6 ) s = " "+s;
    			/*if ( setning.length <10)*/
    			{
    			System.out.print("Antall tegn: " + s.length());
    			}
    		}
     
    	private static void antallOrd(String[] setning) 
    	{
    		System.out.println("\nantall ord:" +(setning.length));   // Writes out number of words 
    	}
    }


    What this do is:
    First write out the whole sentence (WORKS)
    Write out number of words (WORK)
    Then I would like it to write out number of characters (DOESN'T WORK)

    --- Update ---

    btw the arguments are given in "run configurations" using ecplise


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Whats wrong? s.lengt - Counting characters

    Welcome to the Forum! Thanks for taking the time to learn to post code correctly, and if you haven't already, please read this topic to see other useful info for newcomers.

    I would challenge that the number of words is not correct.

    How do you imagine the number of characters could be counted? Have you reviewed the String API for methods that might be helpful?

    Unless your instructor requires it, please don't write statements all in one line as in:

    while ( s.length() <6 ) s = " "+s;

    They are hard to read and can easily contain mistakes that are hard to find. Instead write it as:
    while ( s.length() <6 )
    {
        s = " "+s;
    }
    And why 6? If you must use "magic" numbers, please explain why they are significant in a comment.

    Just a suggestion: Write test code with needed input values already built in so that performing multiple runs that require changing the run configurations in between are not required. That same code would be useful for posting at a site like this to show where the errors or problems lie. When the code is well tested, remove or comment out the test code to default to the code required for the assignment.

  3. #3
    Junior Member
    Join Date
    Sep 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Whats wrong? s.lengt - Counting characters

    Ok, Thanks for reply... I'm very new to this... So
    Whats String API?
    And ...
    How to write test code the way you describe?

    such newbie! :p

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Whats wrong? s.lengt - Counting characters

    There is an API page for every Core SE Java class on the web which can be found easily by searching "java string doc", probably the first result of that search. Substitute 'string' with any other class name for that class' API page. You must know about this documentation but perhaps it has a different name in your language or classroom. If you don't know about these API pages, you might consider another book/class/instructor/institution for learning Java.

    As for writing code to be easily tested, I modified your main() method as below to test your program:
        public static void main(String[] args) 
        {  
            // set setning to a test value and call the method(s) to test
            String[] setning = { "This is a sentence of English words." };
            skrivHeleSetningen(setning);    
     
            // the same can be repeated as needed:
            String[] setning2 = { "This is another sentence of a different"
                + " number of words." };
            skrivHeleSetningen(setning2);    
     
        } // end method main()
    My approach above changed your code a bit more than I normally would, but since your variable names were foreign to me anyway, I was more comfortable changing them to ones I understood.

Similar Threads

  1. Whats wrong here??
    By cbplayer in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 16th, 2013, 02:19 PM
  2. Whats going wrong here?
    By choll4c in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 24th, 2012, 03:39 PM
  3. Whats wrong?
    By help2 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 4th, 2012, 04:22 AM
  4. whats wrong?
    By whattheeff in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 25th, 2011, 02:35 PM
  5. could you tell me whats wrong....
    By gonfreecks in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 6th, 2010, 04:35 PM

Tags for this Thread