Hi, I am new to java and want to play around, I want to make a program that takes in input of lets say 5 numbers, and displays them separated by a space. For example I would enter 12345 and 1 2 3 4 5 would be returned.
this is what i have so far

package split;
import java.util.Scanner;
/**
 *
 * @author gabec94
 */
public class split {


    public static void main(String[] args) {
        System.out.printf("Enter a five digit number: ");
        Scanner input = new Scanner(System.in);
        String inputnumber = input.next();
        inputnumber = inputnumber.split("\t \t \t \t");
        System.out.printf("%s", splitnumber);

i was hoping to use the \t as tabs between each number netered, but i get errors, and cannot solve the problem. Any help is appreciated. Thanks.

read the api on String.split(). it doesn't do what you think it is doing.

ahh I see, it separates each item by lines
now what would do what I need to do to get:
123-->1 2 3?

i assume you are going to keep your input saved as a string. if so, is it always a space between each element (char)? or will there be instances where it may be "12 3"?

if it is a space between each element, it should be pretty easy, you just need to work out how to references the elements of the String individually, and add in the whitespace.

There will not be instances where it will be 12 3, just simply everything entered separated by a space. I understand what you are saying, and spent the last half hour trying various things, but nothing seems to work, I have come to this forum as my last resort, as I have tried everything that I could think of. I would like to add in the \t between each character, but am lost of how to do that. Thanks.

in C, classes will teach you to treat a String is simply an array of chars, and i think you should keep this in mind here.

i am not sure what your requirements are, but the easiest way to accomplish this, is probably to simply traverse through the String and print each element, followed by a "\t". maybe you need to actually store this as a String? (if so, shouldn't be hard to work out)

do you know how to traverse the String? (remember to consider it as a array of chars, and so as hints: String.length(), String.charAt())

Coming from python, to split a string i would do 'string.split(" ")'
and it would split 123 into 1 2 3
I don't know .length, or .charAT, or what that even is
I do not know what I should use to traverse through the string and split it by each space, I know this is simple, but for some reason can't figure it out.

Coming from python, to split a string i would do 'string.split(" ")'
and it would split 123 into 1 2 3
I don't know .length, or .charAT, or what that even is
I do not know what I should use to traverse through the string and split it by each space, I know this is simple, but for some reason can't figure it out.

In Java too its just the same thing :-

String alpha = "A B C 1 2 3";
String beta[] = alpha.split(" ");

The values is "beta" would be the following :-

beta[0]="A"
beta[1]="B"
beta[2]="C"
beta[3]="1"
.... I suppose you get the drift.

i am pretty sure you got that backwards there stephen.

"For example I would enter 12345 and 1 2 3 4 5 would be returned."

i am pretty sure you got that backwards there stephen.

"For example I would enter 12345 and 1 2 3 4 5 would be returned."

I was assuming that is some mistake my the O.P. cause that "split()" method of python too does not operate in that manner but I could be wrong.
Thats the reason why instead of using his first post I quoted his Python post.

I do not know what to look for in the string api. Isn't there a simpler way to convert a string to itself separated by spaces? I am trying what stephen said but this is what happens:

package randomstuff;
import java.util.Scanner;
/**
 *
 * @author gbaseball99
 */
public class randomstuff{

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        System.out.printf("Enter a five digit number: ");
        Scanner input = new Scanner(System.in);
        String inputnumber = input.nextString();
        String splitint[] = inputnumber.split(" ");
          

    }

}

i am getting an error in the

String inputnumber = input.nextString();

line, but I do not know what to do next.

What kind of error, and didn't you notice the methods:

subString, indexOf, or charAt.

at the String API?

the error is
"cannot find symbol
symbol: method nextString()
location: java.util.Scanner"
I also do not understand subString, indexOf, or charAt and how I'm supposed to implement them in the program

First of all.
Whenever you get an error, always check the API of the class you are trying to use. I have never used the Scanner class but obviously you are trying to use a method that doesn't exist. So check the API for Scanner class so you can see which methods it has.

Second,
The String API tells you what arguments they take what they return, and what they do.

Also run this example:

String s="abcd";

for (int i=0;i<s.length;i++) {
  System.out.println( s.charAt(i) );
}

thank you for your help
I was thinking if there is a way to use the modulus, so it would be, (string %10) / 10
(string % 100) / 100
and so on
is there something like this that would work to get each number by itself, and then print them consecutively in a printf statement by using the %d operator?
Thanks

You cannot use modulus to a String. You need to convert it to number:

String s="12345";
int i = Integer.parseInt(s);

i was assuming that it was starting out as an 'int'

sure by declaring it as a String won't "start it out as an int".

would doing the modulus that i described work?

if you messed with it enough, the modulus would work... but... it's a hassle, and breaking the String apart using the Scanner class is easier, in my opinion. If you create a Scanner that reads the specific String, you can add a delimiter to the scanner, and go through each spot and store the numbers into an array, and then print the array spaces while putting a space between each one.

Everything you need is right here in the Scanner class. http://java.sun.com/j2se/1.5.0/docs/api/java/util/Scanner.html

Knowing how to use the API efficiently is one of the most important things to know, regardless of how much experience you have. =]

thanks, but having started java a few days ago, I am completely clueless about what you are saying. The API's wording confuses me, and I am still completely lost. I would like to work with modulus because that is what I have learned, rather than teaching myself new things, such as what you suggested. I would greatly appreciate help with using the modulus.

Firstly gabec94 I would like to note a few things that I have observed while going through this thread. You are not taking any heed of the advice and help given to you and just want to stick to the method of doing something in the langauge that you have worked with in the past. While learning different langauges you will come to know that you need to appreciate each langauge for the things that can be done with it and do them the way they can be done in it.

>I would like to work with modulus because that is what I have learned, rather than teaching myself new things
If this is the case then why learn new langauges at all ?
Also even if this is somekind of assignment that you are absolutely being made to do in the Java langauge, still, why not do it in the proper way.

Coming to your problem : I suppose you want the user to insert to an n-digit number and then output to him the n digits separated by spaces.
You don't need to go to the "using-modulus-to-separate-each-digit" method over here, as it would involve more coding on your part, plus the String class of java offers much easier mechanisms to do this same thing, not to mention that treating a string like an integer and then again converting it to a string doesn't seem to be a logical flow.
To explain what you need to do here, you need to "extract/pull" each character out of the string one by one and put it into another string and while doing so, put spaces between each of the characters.
To extract a character from a String say "gabec94" what I would do is loop from the start to end of the string going over it one character at a time and putting the character into another string.
If you read the javadocs for the charAt() method of the String class, you'll notice that it takes an integer and returns the character at that position of the string. So to show this in code :

String myStr = "gabec94";
for(int i=0;i<myStr.length();i++){
    System.out.prinln("Character at position " + (i+1) + " is: " + charAt(i));
}

Ouput :
g
a
b
e
c
9
4

So if you notice the charAt method returned the character in the String at position "i", since we started i from 0 (which is the base position) we got all the characters of the String one by one.
Now having done that you can as easily put all the charcaters into another string and put a space after each.

Firstly gabec94 I would like to note a few things that I have observed while going through this thread. You are not taking any heed of the advice and help given to you and just want to stick to the method of doing something in the langauge that you have worked with in the past.

You are are right.
I have already given him an example on how to convert the String to int, so he can use his beloved modulus but he hasn't made any effort to use it.

Also the same example you gave him with the charAt(). But just because he hasn't used the language before, shouldn't stop him from thinking the solution you gave him. The method and the example was already given.

Did you run the examples and understood what they do?

@javaAddict : Yes, you had already given him enough hint for him to workout the solution, but I observed that he was busy talking about the modulus then too. Thats the reason I mentioned him that he needs to look at whats sited to him.

I understand what you are saying, and yes indeed I did read everything everyone said, and implemented what you said into my program. I however did not understand what they do, but now after the explanation given by verruckt24 I more thoroughly understand what the chatAt() and length() functions do. I thank you greatly and will try to take advice rather than sticking to the way I know. I have spent time looking over the API's and reading the code that you have given me and completely understand it, and it is very similar to python.

I get an error stating that the operator + cannot be applied to the charAt function. I tried replacing the + with commas and I still got an error, what should I do?

package random;
import java.util.Scanner;
public class random {
    public static void main(String[] args) {
        System.out.printf("Enter a five digit number: ");
        Scanner input = new Scanner(System.in);
        String inputnumber = input.next();
        for(int place=0;place<inputnumber.length();place++){
            System.out.println("Character at position " + (place+1) + " is: " + charAt(place));
        }
        

    }

}

@javaaddict
in your code:

String s="abcd";

for (int i=0;i<s.length;i++) {
  System.out.println( s.charAt(i) );
}

I get an error stating that the < operator cannot be applied, I tried making it a string and an int, but it didn't work either way.

I have been messing around with all of what you gave me, but still found it much easier to use the modulus operator. I understand everything that you have said about me and what I have done, I need to learn more java and I have learned from what you have given to me. But for now I will keep my program as:

package assignment2;
import java.util.Scanner;
/**
 *
 * @author gbaseball99
 */
public class NewMain2 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        System.out.printf("Enter a five digit number: ");
        Scanner input = new Scanner(System.in);
        int inputnumber = input.nextInt();
        int digit5 = (inputnumber % 100000) / 10000;
        int digit4 = (inputnumber % 10000) / 1000;
        int digit3 = (inputnumber % 1000) / 100;
        int digit2 = (inputnumber % 100) / 10;
        int digit1 = (inputnumber % 10);
        System.out.println(digit5 + " " + digit4 + " " + digit3 + " " + digit2 + " " + digit1);
    }

}

for it is what I understand. As I develop a further understanding of java, I will use more methods as which have been described here. I thank you all for helping.

@javaaddict
in your code:

String s="abcd";

for (int i=0;i<s.length;i++) {
  System.out.println( s.charAt(i) );
}

I get an error stating that the < operator cannot be applied, I tried making it a string and an int, but it didn't work either way.

Try this:

String s="abcd";

for (int i=0;i<s.length();i++) {
  System.out.println( s.charAt(i) );
}

I keep forgetting that for Strings this is the right one:
s.length()

Also what you wrote will work but only for 5-digit numbers. Try modifying this example to accomplish what you want

String s="abcd";

for (int i=0;i<s.length();i++) {
  System.out.print( s.charAt(i) );
}

  System.out.println();
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.