Im trying to get the output to read on one line such as this:
Reference String 1 2 3 where 1,2,3 is the number the user enters.
Instead the output is as such:
1 2 3 Reference String.

How can I improve this to read correctly?

import java.util.Scanner;


public class test {

    public static void main(String[] args)
    {
    Scanner input = new Scanner(System.in);
    System.out.println("Please enter length of string "); 
    int str_len=input.nextInt();
    System.out.println("Please enter reference string ");
    int[] count = new int[str_len];
    //int str =input.nextInt();
       int i;
    for ( i=0; i<=str_len-1;i++)
    {
    count[i] = input.nextInt();
    System.out.print( " " + count[i]);
    }//ends for
    System.out.print("Reference string is ");
    }//ends class

Recommended Answers

All 6 Replies

Move your line 20 to be before the for-loop.

Thank you but when I do that, it read as such:
Please enter reference string
Reference string is 1 2 3.

It should be able to prompt user for reference string.
1.Please enter reference string
Then, user should be able to enter reference string
1 2 3
Next, should be able to print Reference string with numbers entered by users
Reference string is 1 2 3

Anymore suggestions?

Hmm... Wait... Do you mean the program will keep asking for reference string, and then output the reference string with number? Is it something similar to below example?

/*
Output example...

>java YourProgramName
 Please enter reference string: _
 // then user enter My First String Ever
 // then the output would be...
 1.My
 2.First
 3.String
 4.Ever
*/

When I run mines, there is no space between so it looks as such
Please enter reference string
Reference string is
1 2 3 4 5
The code hasn't done anything here yet. This is just what has been entered by user.

Once its done, here is the output:
Please enter length of string
5
Please enter reference string
Reference string is
1 2 3 4 5
12345BUILD SUCCESSFUL (total time: 14 seconds)

That is incorrect.

I'm still unsure about your "reference string" word. Do you mean a user first enters how long a string should be, and then enters each character of the string? Then your program will join each character together and display it as a whole string? That's what I got from your last example.

i think u can try to separate the blocks, as are different function, like:
1 cicle for reading the user's data
and after 1cicle for writing (is also more clear to read, and expand)

int[] count = new Int[strlen];
int i;

System.out.println("Please digit the numbers");
for (i=0; i<strlen; i++){
   count[i]=input.nextInt();
}
System.out.println("Digited value are:");
for (i=0; i<strlen; i++){
   System.out.pritnln(count[i]);
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.