Java Help

Calaen

I am a massive cock who isn't firing atm!
Joined
Dec 22, 2003
Messages
9,538
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.
 

Panda On Smack

Can't get enough of FH
Joined
Dec 22, 2003
Messages
1,030
Dunno how to do it in Java but the method would be something like

Check length of First word with your function and store it
if its greater than 37, error

Check length of Second word with your function and store it
Add its length to First stored word length and check to see that its a number less than or equal to 38 (so you have 2 dots)

Take the number away from 40 to find out how many dots you need

Print the first word, loop through printing the dots until you reach the amount it should be, print your second word

Or did you already know that?

:)
 

Calaen

I am a massive cock who isn't firing atm!
Joined
Dec 22, 2003
Messages
9,538
no tbh I have just started a degree after deciding I should try and get one :p

I am finding it difficult to work Full Time and stick enough hours of study in. So although what your saying makes sense I have no odea how to put it into a program :p
 

JingleBells

FH is my second home
Joined
Mar 25, 2004
Messages
2,224
Basically it's
Code:
public class WordTest {
	public static void main(String args[]) {
		//Check if there are 2 arguments given
		//These bits should be a while loop taking in user input and checking it with the conditions
		//But that is for you to do ;)
		if(args.length < 2) {
			System.out.println("Not enough arguments");		
			System.out.println("Usage: java WordTest word1 word2");
			System.exit(1);
		}
		//allocate arguments to variables
		String input1 = args[0];
		String input2 = args[1];
		//make sure argument 1 is less than 37
		if (input1.length() > 37) {
			System.out.println("Word 1 is too long");
			System.exit(1);
		}
		//need to check that you can fit the 2 dots in, hence 38 (40-2)
		if (input2.length() > (38-input1.length())) {
			System.out.println("Word 2 is too long");
			System.exit(1);
		}
		//how long is input1
		int i = input1.length();
		//print input 1
		System.out.print(input1);
		//keep printing dots till there is only room left for input 2
		while (i < (40 - input2.length())) {
			System.out.print(".");
			i++;
		}
		//print input 2
		System.out.println(input2);
	}
}
 

Calaen

I am a massive cock who isn't firing atm!
Joined
Dec 22, 2003
Messages
9,538
Thanks alot mate. I owe you a pint or 2.
 

wyrd_fish

Fledgling Freddie
Joined
Dec 27, 2003
Messages
537
but surley they want you to use buffered readers and std input, rather than command line args?
 

Calaen

I am a massive cock who isn't firing atm!
Joined
Dec 22, 2003
Messages
9,538
OK this is where I am at, it works ok when putting normal words in but if I exceed the limit it tells me there is an error but it wont loop round. I dont actually know how to do this so again any pointers would be welcome.

Code:
import javax.swing.*;
 
 class Homework9 
 {
   public static void main (String args[]) 
	{
	  int length1, length2;
	  //input a word  
	  String input1 = JOptionPane.showInputDialog("Enter first word");
      length1 = input1.length();
	  if (input1.length() > 37)
	  
	  //prints an error message if the length of word 1 exceeds 37 characters	    		    
	  JOptionPane.showMessageDialog(null,"Error",
      "Error word length exceeds 37", JOptionPane.ERROR_MESSAGE);
      
      //input a second word      
      String input2 = JOptionPane.showInputDialog("Enter second word");
	  length2 = input2.length();
	  if (input2.length() > (38-length1))	    		    
		    
	  //prints an error message if the length of word 1 and word 2 exceeds 38 characters    
	  JOptionPane.showMessageDialog(null,"Error total length of words exceeds 38 characters",
      "Error", JOptionPane.ERROR_MESSAGE);
            
      //how long is input1
	  int i = input1.length();
	  //print input 1
	  System.out.print(input1);
      //keep printing dots till there is only room left for input 2
	  while (i < (40 - input2.length())) 
	  {
	    System.out.print(".");
		i++;
             
      }
		//print input 2
	    System.out.println(input2);
		 
		
		
	}
}
 

JingleBells

FH is my second home
Joined
Mar 25, 2004
Messages
2,224
try:
Code:
String input1 = JOptionPane.showInputDialog("Enter first word");
      length1 = input1.length();
	 while (input1.length() > 37) {
	    //prints an error message if the length of word 1 exceeds 37 characters  
	     JOptionPane.showMessageDialog(null,"Error",
                         "Error word length exceeds 37", JOptionPane.ERROR_MESSAGE);
             input1 = JOptionPane.showInputDialog("Enter first word");
}
basically, it's until the user enters a word shorter than 37 chars, keep asking them for a word.
 

Calaen

I am a massive cock who isn't firing atm!
Joined
Dec 22, 2003
Messages
9,538
Thanks very much again matey, Just what I needed I was using if's instead of whiles. If your ever up in Newcastle make a post I'll get you pissed :p
 

Users who are viewing this thread

Top Bottom