Jump to content



Welcome to KnowledgeSutra - Dear Guest , Please Register here to get Your own website. - Ask a Question / Express Opinion / Reply w/o Sign-Up!
- - - - -

Java Basic Program Guidance


9 replies to this topic

#1 kvarnerexpress

    Super Member

  • Kontributors
  • PipPipPipPipPipPipPipPipPip
  • 407 posts

Posted 15 November 2005 - 10:15 PM

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.

#2 jlhaslip

    Insert Custom Title Here

  • [MODERATOR]
  • PipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPip
  • 5,040 posts
  • Gender:Not Telling
  • Location:Linux, DOS and Windows…the good, the bad and the ugly
  • myCENT:81.07
  • Spam Patrol

Posted 15 November 2005 - 11:15 PM

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?

#3 alperuzi

    delete me

  • Kontributors
  • PipPipPipPipPipPipPipPipPip
  • 518 posts
  • Location:delete me
  • Interests:delete me

Posted 16 November 2005 - 05:14 AM

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

#4 beeseven

    Privileged Member

  • Kontributors
  • PipPipPipPipPipPipPipPipPip
  • 629 posts

Posted 16 November 2005 - 03:15 PM

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


#5 iGuest

    Hail Caesar!

  • Kontributors
  • PipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPip
  • 5,876 posts
  • Interests:Trap17 Free Web Hosting, No Ads

Posted 02 April 2008 - 07:33 AM

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

#6 iGuest

    Hail Caesar!

  • Kontributors
  • PipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPip
  • 5,876 posts
  • Interests:Trap17 Free Web Hosting, No Ads

Posted 14 April 2008 - 04:50 PM

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

#7 iGuest

    Hail Caesar!

  • Kontributors
  • PipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPip
  • 5,876 posts
  • Interests:Trap17 Free Web Hosting, No Ads

Posted 17 April 2008 - 04:07 PM

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

#8 iGuest

    Hail Caesar!

  • Kontributors
  • PipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPip
  • 5,876 posts
  • Interests:Trap17 Free Web Hosting, No Ads

Posted 24 July 2009 - 09:12 AM

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

#9 iGuest

    Hail Caesar!

  • Kontributors
  • PipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPip
  • 5,876 posts
  • Interests:Trap17 Free Web Hosting, No Ads

Posted 07 February 2010 - 01:14 PM

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

#10 iGuest

    Hail Caesar!

  • Kontributors
  • PipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPip
  • 5,876 posts
  • Interests:Trap17 Free Web Hosting, No Ads

Posted 09 March 2010 - 07:53 AM

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




Reply to this topic


This post will need approval from a moderator before this post is shown.

  


1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users