Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2005
    Posts
    19
    Rep Power
    0

    Java basic program guidance


    Having some problems taking in the Java at Uni,

    Was wondering if anyone could shine some light on it for me.

    My task is to write a program that will ask you to enter 2 words. the program will then print out both words on one line. However the words will be seperated by dots that will make the total length of the line 40. so if your first word was turtle and the second was abc, the output would be

    turtle...............................abc

    The program should check for certain conditions:

    1. a word can not be longer than 37 characters;
    2. there must always be atlest 2 dots in a line.

    The program should ask for the first word till a word of acceptable length is entered. it then does the same for the second word.

    once both words are input the prgram will either output an error message if the words are to long when combined;

    or output the required line with words.

    I have to write the program using while and/or do-while loops

    It also says something about the length of a string can be found out using myString.length().

    I can understand the basic of what I need to do but with regards to word length and adding the required number of dots im fooked.

    Any pointers would be appreciated.

    Cheers.




    Moderator's note: Read at least until the second page for best info
    Last edited by Nemi; November 17th, 2005 at 10:46 AM. Reason: Added comment
  2. #2
  3. Feelin' Groovy
    Devshed Supreme Being (6500+ posts)

    Join Date
    Aug 2001
    Location
    WDSMIA
    Posts
    10,135
    Rep Power
    5054
    Any pointers would be appreciated.
    How would you do this if you didn't have a computer? If you can explain it to me step by step in detail, translating it to Java should be a breeze...

    Comments on this post

    • tfecw agrees : this step is so important. I wish it was stressed more in programming 101
    • mrider agrees : Can't give rep points - but I agree 100%
    Yawmark
    class Sig{public static void main(String...args){\u0066or(int
    \u0020$:"v\"ʲ\"vΤ\"".to\u0043h\u0061rArray()
    )System./*goto/*$/%\u0126//^\u002A\u002Fout.print((char)(($>>
    +(~'"'&'#'))+('<'>>('\\'/'.')/\u002Array.const(~1)\*\u002F)));}}
  4. #3
  5. No Profile Picture
    rebel with a cause
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    May 2004
    Location
    The Batsh!t Crazy State.
    Posts
    5,817
    Rep Power
    3462
    How much programming do you understand.
    Loops
    Calling Methods
    Arrays
    Doesn't really matter what language as long as you understand those concepts although it would help if you were familiar with them in java.
    Dear God. What is it like in your funny little brains? It must be so boring.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2005
    Posts
    19
    Rep Power
    0
    I understand loops but very basic.

    I am not here for someone to do the thing for me I am just asking for pointers on how to work out the length of a string.
  8. #5
  9. Feelin' Groovy
    Devshed Supreme Being (6500+ posts)

    Join Date
    Aug 2001
    Location
    WDSMIA
    Posts
    10,135
    Rep Power
    5054
    Code:
    String.length()
    Example:

    Java Code:
    System.out.println("foo".length()); // 3


    It's an extremely important skill to learn to read the API and become familiar with the tools you will use to program Java. Java has an extensive set of documentation that you can even download for your convenience. These "javadocs" are indexed and categorized so you can quickly look up any class or method. Take the time to consult this resource whenever you have a question - you'll find they typically contain very detailed descriptions and possibly some code examples.

    Java API Specifications
    Java 1.5 JDK Javadocs

    Comments on this post

    • bullet agrees
    • ishnid agrees
    Yawmark
    class Sig{public static void main(String...args){\u0066or(int
    \u0020$:"v\"ʲ\"vΤ\"".to\u0043h\u0061rArray()
    )System./*goto/*$/%\u0126//^\u002A\u002Fout.print((char)(($>>
    +(~'"'&'#'))+('<'>>('\\'/'.')/\u002Array.const(~1)\*\u002F)));}}
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2005
    Posts
    19
    Rep Power
    0
    Much appreciated just what I needed.
  12. #7
  13. Feelin' Groovy
    Devshed Supreme Being (6500+ posts)

    Join Date
    Aug 2001
    Location
    WDSMIA
    Posts
    10,135
    Rep Power
    5054
    Originally Posted by tfecw
    this step is so important. I wish it was stressed more in programming 101
    One thing that really drove home the point for me recently was a book containing a series of old Scientific American articles. One article was about a fictional tribe called the "Apraphulians" (April fools) that built a series of logical gates from ropes, boxes, and pulleys. Another was about some MIT students who built a computer that plays tic-tac-toe out of Tinkertoys. Not only were the articles fascinating, but they reveal the importance of thinking about the problem domain first and foremost. The computer (and the language used to communicate with it) is just another tool; what really matters is the algorithm...

    Comments on this post

    • crownjewel82 agrees : Oh how I wish I had points to give you
    • mvantuyl agrees
  14. #8
  15. No Profile Picture
    rebel with a cause
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    May 2004
    Location
    The Batsh!t Crazy State.
    Posts
    5,817
    Rep Power
    3462
    I so wish more people understood that necessity in programing. I had the benefit of a teacher who cared enough about our proper education in programming that he didn't let us touch a computer or teach us a scrap of any programming language until he taught us how algorithms work. I swear we spent less than half the semester in the lab but when we finished that course we were better coders than most of the CS majors at my university. (This was in high school.) I've tutored programming students for years and only a few have ever been able to grasp that the algorithm is the solution not the syntax.

    Comments on this post

    • mrider agrees
    Dear God. What is it like in your funny little brains? It must be so boring.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Nov 2004
    Location
    Washington DC
    Posts
    2,756
    Rep Power
    1571
    I still get weird looks when i raid the recycling bins for scrap paper at work.
  18. #10
  19. <- My daily commute :^)
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Mar 2005
    Location
    Earth. Welcome.
    Posts
    1,501
    Rep Power
    1698
    only a few have ever been able to grasp that the algorithm is the solution not the syntax.
    I also wish people would grasp the concept of only optimizing when it becomes necessary. How frequently do we see code that is terribly convoluted in the quest for a 1% improvement in speed, when in fact the code in question is only accessed occasionally?

    This goes back to the statement that it's best to understand the algorithm ahead of time. It's much easier to write clean, clear, correct code if the necessary steps for accomplishing a task are understood ahead of time.
    A -> B: Ride.
  20. #11
  21. No Profile Picture
    rebel with a cause
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    May 2004
    Location
    The Batsh!t Crazy State.
    Posts
    5,817
    Rep Power
    3462
    I used to tutor discrete math too. Big O is a bitch to learn but once you do it's your best friend.
    Dear God. What is it like in your funny little brains? It must be so boring.
  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2005
    Posts
    19
    Rep Power
    0

    my attempt


    Ok so I have given it ago and this is what I have come up with.

    Code:
     import javax.swing.*;
     
     class While 
     {
       public static void main (String args[]) 
    	{
    		//This is what I have so far its not perfect and it has holes  all over it
    		
    		int length1, length2;
    	    
    		{
    	        String input1 = JOptionPane.showInputDialog("Enter first word");
    		    length1 = input1.length();
    		    if (input1.length() > 37)
    		    
    		    
    		      JOptionPane.showMessageDialog(null,"Please re-enter word",
                  "Error word length exceeds 37", JOptionPane.ERROR_MESSAGE);
                
                
    		    String input2 = JOptionPane.showInputDialog("Enter second word");
    		    length2 = input2.length();
    		    if (input2.length() > (38-length1))	    		    
    		    
    		    
    		    JOptionPane.showMessageDialog(null,"Error total length of words exceeds 38",
                "Error", JOptionPane.ERROR_MESSAGE);
    		    
    		    
    		    
    		    int i = input1.length();
    		   System.out.print(input1);
    		    while (i < (40 - input2.length())) {
    			System.out.print(".");
    			i++;
                 
                }
    		    //print input 2
    		    System.out.println(input2);
    		} 
    		while (length1+length2+2 <= 40);
    		
    	}
    }
    The main problem for me is I find I dont really understand it at all :-(

    How do I make it loop back from the first input entry if its over 37 in length? And the same for the 2nd input if both together are over 38?

    Its very frustrating this Java stuff.
  24. #13
  25. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,021
    Rep Power
    1285
    Keep in mind that most of the frustration you have is with the basic idea of programming, not with Java. People at my University normally take out frustrations on Java when they are really frustrated about programming. Trust me, you would have just as much trouble trying to make this work in C++.

    First, you can't use a Java reserved word like While as an identifier.

    Next, you want to make sure you understand the basic difference between an if and a while.

    An if checks a condition, and based on that condition, may execute a statement, and then control passes to the next executable statement that follows the if block.

    A while checks a condition, and will continue to execute the statements in the body of the while until the condition becomes false.
  26. #14
  27. Walrus Gramps
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2005
    Location
    San Antonio, Texas
    Posts
    1,229
    Rep Power
    620
    "if its over 37 in length"
    "if both together are over 38"

    How would you express that in Java?
  28. #15
  29. Walrus Gramps
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2005
    Location
    San Antonio, Texas
    Posts
    1,229
    Rep Power
    620
    Originally Posted by bullet
    <snip> First, you can't use a Java reserved word like While as an identifier. </snip>
    Actually, 'while' is a reserved word, but 'While' isn't. (But it still isn't a good idea to use 'While' as a class name)
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo