Discuss Java basic program guidance in the Java Help forum on Dev Shed. Java basic program guidance Java Help forum discussing all Java platforms - J2ME, J2SE and J2EE - as well as relevant standards, APIs and frameworks such as Swing, Servlets, JSPs, Applets, Struts, Spring, Hibernate, ANT, EJB, and other Java-related topics.
Posts: 19
Time spent in forums: 5 h 35 m 15 sec
Reputation Power: 0
Java basic program guidance
Having 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.
Quote:
Moderator's note: Read at least until the second page for best info
Last edited by Nemi : November 17th, 2005 at 10:46 AM.
Reason: Added comment
Posts: 5,817
Time spent in forums: 3 Months 4 Days 12 h 16 m 39 sec
Reputation Power: 3461
How much programming do you understand.
Loops
Calling Methods
Arrays
Doesn't really matter what language as long as you understand those concepts although it would help if you were familiar with them in java.
__________________
Dear God. What is it like in your funny little brains? It must be so boring.
It's an extremely important skill to learn to read the API and become familiar with the tools you will use to program Java. Java has an extensive set of documentation that you can even download for your convenience. These "javadocs" are indexed and categorized so you can quickly look up any class or method. Take the time to consult this resource whenever you have a question - you'll find they typically contain very detailed descriptions and possibly some code examples.
Posts: 10,135
Time spent in forums: 3 Months 1 Week 4 Days 1 h 22 m 45 sec
Reputation Power: 5052
Quote:
Originally Posted by tfecw
this step is so important. I wish it was stressed more in programming 101
One thing that really drove home the point for me recently was a book containing a series of old Scientific American articles. One article was about a fictional tribe called the "Apraphulians" (April fools) that built a series of logical gates from ropes, boxes, and pulleys. Another was about some MIT students who built a computer that plays tic-tac-toe out of Tinkertoys. Not only were the articles fascinating, but they reveal the importance of thinking about the problem domain first and foremost. The computer (and the language used to communicate with it) is just another tool; what really matters is the algorithm...
Posts: 5,817
Time spent in forums: 3 Months 4 Days 12 h 16 m 39 sec
Reputation Power: 3461
I so wish more people understood that necessity in programing. I had the benefit of a teacher who cared enough about our proper education in programming that he didn't let us touch a computer or teach us a scrap of any programming language until he taught us how algorithms work. I swear we spent less than half the semester in the lab but when we finished that course we were better coders than most of the CS majors at my university. (This was in high school.) I've tutored programming students for years and only a few have ever been able to grasp that the algorithm is the solution not the syntax.
Posts: 1,501
Time spent in forums: 1 Month 3 Weeks 5 Days 18 h 14 m
Reputation Power: 1697
Quote:
only a few have ever been able to grasp that the algorithm is the solution not the syntax.
I also wish people would grasp the concept of only optimizing when it becomes necessary. How frequently do we see code that is terribly convoluted in the quest for a 1% improvement in speed, when in fact the code in question is only accessed occasionally?
This goes back to the statement that it's best to understand the algorithm ahead of time. It's much easier to write clean, clear, correct code if the necessary steps for accomplishing a task are understood ahead of time.
Posts: 19
Time spent in forums: 5 h 35 m 15 sec
Reputation Power: 0
my attempt
Ok so I have given it ago and this is what I have come up with.
Code:
import javax.swing.*;
class While
{
public static void main (String args[])
{
//This is what I have so far its not perfect and it has holes all over it
int length1, length2;
{
String input1 = JOptionPane.showInputDialog("Enter first word");
length1 = input1.length();
if (input1.length() > 37)
JOptionPane.showMessageDialog(null,"Please re-enter word",
"Error word length exceeds 37", JOptionPane.ERROR_MESSAGE);
String input2 = JOptionPane.showInputDialog("Enter second word");
length2 = input2.length();
if (input2.length() > (38-length1))
JOptionPane.showMessageDialog(null,"Error total length of words exceeds 38",
"Error", JOptionPane.ERROR_MESSAGE);
int i = input1.length();
System.out.print(input1);
while (i < (40 - input2.length())) {
System.out.print(".");
i++;
}
//print input 2
System.out.println(input2);
}
while (length1+length2+2 <= 40);
}
}
The main problem for me is I find I dont really understand it at all :-(
How do I make it loop back from the first input entry if its over 37 in length? And the same for the 2nd input if both together are over 38?
Posts: 3,828
Time spent in forums: 1 Month 1 Week 6 Days 11 h 21 m 4 sec
Reputation Power: 1248
Keep in mind that most of the frustration you have is with the basic idea of programming, not with Java. People at my University normally take out frustrations on Java when they are really frustrated about programming. Trust me, you would have just as much trouble trying to make this work in C++.
First, you can't use a Java reserved word like While as an identifier.
Next, you want to make sure you understand the basic difference between an if and a while.
An if checks a condition, and based on that condition, may execute a statement, and then control passes to the next executable statement that follows the if block.
A while checks a condition, and will continue to execute the statements in the body of the while until the condition becomes false.