Jump to content
xisto Community
Sign in to follow this  
kvarnerexpress

Java Basic Program Guidance

Recommended Posts

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 beturtle...............................abcThe 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 loopsIt 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.

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

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:

 

int total_word_length = string1.length() + string2.length();

system.out.print(string1); //*darn*, don't remember the syntax here :lol:

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 :) )

Share this post


Link to post
Share on other sites

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:

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);

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
multithreading applicationJava Basic Program Guidance

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

Share this post


Link to post
Share on other sites
Need helpJava Basic Program GuidanceHi ! My name is Arash and I study biology . I need a help to make a mobile java application to send our posts to our weblog with cellphone and we have force to do this unfortunately we don't have any exprience in programming !Please help us ! How to start ?Please send your tips and helps to my mail : max.Alpha.89@gmail.ComThank you !-question by Arash

Share this post


Link to post
Share on other sites
help me plsJava Basic Program Guidance

hi...I have to make a program that can print the selected number.Can you help me?

plllssss

-reply by madeth

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.