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