|
|
|
|
![]() ![]() |
Nov 15 2005, 10:15 PM
Post
#1
|
|
|
Super Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 407 Joined: 13-December 04 Member No.: 2,696 |
aving 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. |
|
|
|
Nov 15 2005, 11:15 PM
Post
#2
|
|
|
A computer once beat me at chess, but it was no match for me at kick boxing. ![]() Group: [MODERATOR] Posts: 3,882 Joined: 24-July 05 From: In Trouble Again... still? Member No.: 9,787 ![]() |
I don't do Java, so bear with me on this one.
In another life (it seems) there once was a programming language named Cobol. I used to do some coding in that. The logic shouldn't be much different,though. Check the first word and determine its length. Check the second word length. Total the two lengths. Add the first word to the output line, then a string of periods as determined by a for / while loop based on the sum of the word lengths and the length of the output line. Finally, the second word. Might think about using an array to store all that in, too. As stated above, I don't have the foggiest idea of the specifics of Java for this code, but that logic should work. Is this what you needed? |
|
|
|
Nov 16 2005, 05:14 AM
Post
#3
|
|
|
delete me ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 518 Joined: 18-September 04 From: delete me Member No.: 1,185 |
yes, basically your string_var.length() is going to be the boundary for the loop, if you can use 'for' loops instead. I haven't written Java programs for over a year now, but it'll be something like this:
[CODE] int total_word_length = string1.length() + string2.length(); system.out.print(string1); //*darn*, don't remember the syntax here for ( int i=word_length; i<40; i++) { system.out.print( "." ); } system.out.print(string2); (PS, this was written just before me falling to sleep and could be very wrong... in that case, sorry |
|
|
|
Nov 16 2005, 03:15 PM
Post
#4
|
|
|
Privileged Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 629 Joined: 26-February 05 Member No.: 3,995 |
Usually for input where not everything is acceptable, you use a while(true) loop, then if what the user enters is acceptable, you break, otherwise go back to the beginning. Since you have two things, use two while(true)s. Something like this:
CODE String firstWord, secondWord;
while(true) { firstWord = JOptionPane.showInputDialog("First word"); if(firstWord.length() == 0 || firstWord.length() > 37) { JOptionPane.showMessageDialog(null, "Must be between 1 and 37 characters"); continue; } break; } while(true) { secondWord = JOptionPane.showInputDialog("Second word"); if(secondWord.length() == 0 || secondWord.length() > 37 || firstWord.length() + secondWord.length() > 38) { JOptionPane.showMessageDialog(null, "Must be between 1 and 37 characters and total length must be less than or equal to 38 characters"); continue; } break; } System.out.print(firstWord); for(int x = 0; x < 40 - (firstWord.length() + secondWord.length()); x++) System.out.print("."); System.out.println(secondWord); |
|
|
|
Apr 2 2008, 07:33 AM
Post
#5
|
|
|
Trap Double Mocha Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 2,360 Joined: 21-September 07 Member No.: 50,369 |
about java programming
Java Basic Program Guidance hi, I m prasad ,I m t.Y.Bsc(physics) graduate I have done c,c++ programming through domestic institute but because of financial condition I fail to learn about java .I want to go for java throgh self study .Is this possible for me? Your reply and help is needed . Thank you. -reply by prasad |
|
|
|
Apr 17 2008, 04:07 PM
Post
#6
|
|
|
Trap Double Mocha Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 2,360 Joined: 21-September 07 Member No.: 50,369 |
Self-study
Java Basic Program Guidance Replying to Trap FeedBacker Of course you can self-study. What else do we have internet tutorials for... I have done this with several languages with great success. -reply by selfStudyer16 |
|
|
|
Apr 14 2008, 04:50 PM
Post
#7
|
|
|
Trap Double Mocha Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 2,360 Joined: 21-September 07 Member No.: 50,369 |
project
Java Basic Program Guidance Hi,iam a 10th std student in banglore...I hav to code a java programe to book a railway or airway ticket reservation!without using ne applets! I am aloowed to use only basic loops and statements and arrays!pls can you help me? -question by madhu |
|
|
|
![]() ![]() |
Similar Topics
|