Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> Java Basic Program Guidance
kvarnerexpress
post 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.
Go to the top of the page
 
+Quote Post
jlhaslip
post 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 Icon

Group: [MODERATOR]
Posts: 3,882
Joined: 24-July 05
From: In Trouble Again... still?
Member No.: 9,787
Spam Patrol



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?
Go to the top of the page
 
+Quote Post
alperuzi
post 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 biggrin.gif
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 smile.gif )
Go to the top of the page
 
+Quote Post
beeseven
post 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);
Go to the top of the page
 
+Quote Post
iGuest
post 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
Go to the top of the page
 
+Quote Post
iGuest
post 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
Go to the top of the page
 
+Quote Post
iGuest
post 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
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Java editor(72)
  2. Auto Run Java Program(11)
  3. Java Script Drop Down Menu With Css(2)
  4. [help] Java Script: Window.open(8)
  5. How To Create Java Button Or Frame(12)
  6. Java Helpdesk(8)
  7. Problem With Java Script Popup Form(1)
  8. The Power Of Java(14)
  9. Ajax: Achieve Ajax Program In 5 Lines Of Code!(1)
  10. Jsp Or Java Chat Script Like Mig33(4)
  11. Eclipse Exporting .jar Files(4)
  12. I Need Help With Java(3)
  13. Help Deploying Java(0)
  14. Which Is The Best Java Web Framework?(0)
  15. Helpful Registry Edit For Java Programmers(2)
  1. Java Exercise Help(2)
  2. Java Iterator Help(2)
  3. Beginning Java(10)
  4. Java In A High School Software Design Course(5)
  5. Java Part 1(2)
  6. Circular Movment/animation In Java?(6)
  7. Java Applet Query(1)
  8. Java Or C++(2)
  9. Java Vs Javascript(11)
  10. Java Object[][] Help(2)
  11. Learn Java Programming Language Online Step By Step(0)
  12. Java Game(2)
  13. Call Pdf995 From Java(0)


 



-