Urgent! Need help with Java programme!

I’m working on a java programmeI’m working on a java programme:

Write a program that asks the user to enter two words. The program then prints out both words on one line. The words will be separated by enought dots so that the total line length is 30:

Enter first word:
turtle
Enter second word
153

turtle…153
This could be used as part of an index for a book. To print out the dots, use System.out.print(“.”) inside a loop body.

My work:

import java.io.*;
class project
{
  public static void main (String[] args) throws IOException
  {
    InputStreamReader inStream = 
        new InputStreamReader( System.in ) ;
    BufferedReader stdin = 
        new BufferedReader( inStream );
 
    String inData;

    System.out.println("Enter the first word:turtle");
    inData = stdin.readLine();

    System.out.println("First word you entered:turtle" + inData );
     String inData1;

    System.out.println("Enter the second word: 153");
    inData1 = stdin.readLine();

    System.out.println("Second word you entered: 153" + inData1 );
  }

Can anybody help me to finish it cuz I don’t know what to do next!!! Thanks!!!

Hi Miz_Hana, welcome to the forum

What does the compiled code output when run as it is now?

the output is:

Enter the first word:turtle

First word you entered:turtle
Enter the second word: 153

Second word you entered: 153

And I don’t know how to calculate the length :worried:

Well, it’s been a while since I touched any Java, but I think if you
import java.lang.String
and
import java.lang.StringBuilder
you should have the tools you need. eg. try something like

int firstwordlength = (inData.length());
int secondwordlength = (inData1.length());
int bothwordslength = firstwordlength + secondwordlength;
int numberofdots = 30 - bothwordslength;
StringBuilder sb = new StringBuilder();
sb.append(inData);
for (int i = 0; i < numberofdots ; ++i)
{
    sb.append(".");
}
sb.append(inData1);
String result = sb.toString();
System.out.println(result);

Thank you so much :slight_smile:

Has @Mittineague just done your homework for you?

I would say yes.

Take a look at Chapter 15, Example 3
You’ll find that it’s identical, word for word for what the OP was asking, from the compsciprep website.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.