| |
|
Welcome to KnowledgeSutra - Dear Guest | |
Java Basic Program Guidance
#1
Posted 15 November 2005 - 10:15 PM
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.
#2
Posted 15 November 2005 - 11:15 PM
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?
#3
Posted 16 November 2005 - 05:14 AM
[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
#4
Posted 16 November 2005 - 03:15 PM
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);
#5
Posted 02 April 2008 - 07:33 AM
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
#6
Posted 14 April 2008 - 04:50 PM
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
#7
Posted 17 April 2008 - 04:07 PM
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
#8
Posted 24 July 2009 - 09:12 AM
Hi
I have an application that calls 5 different systems I am trying to replace queues with direct method call messaging. The problem is my stack get very big as I call lots of methods, meaning that lots of threads are created and my stack becomes so big...I wanna come up with the implementation where I will use blocking queues to make sure there's no stack overflow...Help pls
-question by Mike Nhlengethwa#9
Posted 07 February 2010 - 01:14 PM
Reply to this topic

1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users















