Jump to content

input and print...

- - - - -

  • Please log in to reply
6 replies to this topic

#1
speachy_15

speachy_15

    Learning Programmer

  • Members
  • PipPipPip
  • 31 posts
hi evr1... im new to java and i am lost in this problem... help please...

input one number consisting of five digits from the user, separate the number into its individual digits and print the digits separated from one another by spaces each...
like when i enter 42339... the output should be:
4 2 3 3 9


help.....


#2
Xdawn90

Xdawn90

    Learning Programmer

  • Members
  • PipPipPip
  • 55 posts
There are many ways in doing this.

I used the substring method and a for loop to do it. I know maybe this is not the best way but you can try it. :)

#3
speachy_15

speachy_15

    Learning Programmer

  • Members
  • PipPipPip
  • 31 posts
i really am new... *sigh* i tried to make one earlier.. and it went like this...
public class prelimActivity3 {

 public static void main (String args[]) {

    	String inarray=JOptionPane.showInputDialog ("Enter number array: ");

    	int a=Integer.parseInt (inarray);

    	int [] b={a};


	System.out.println (b[]);

    }



}
it had an error of .class expected...
at the System.out part...
i'm really really new... and i don't know what to do.... thanks for the help...

#4
Xdawn90

Xdawn90

    Learning Programmer

  • Members
  • PipPipPip
  • 55 posts
I amended your code a bit.

public static void main (String args[]) {
        int counter = 0;
        int[] b;
        b = new int[5];
        while (counter < 5){
            String inarray = JOptionPane.showInputDialog ("Enter 5 numbers: ");
            int a = Integer.parseInt(inarray);
            b[counter] = a;
            counter++;
        }

        for(int i=0; i<b.length; i++){
            System.out.print(b[i] + " ");
        }

    }

Actually I consider myself a beginner in java and I am not really good in it also. Hopefully it helps.

Regards.

#5
speachy_15

speachy_15

    Learning Programmer

  • Members
  • PipPipPip
  • 31 posts
waaaaaaiiiii!!!!! Thank you very much!!!!! Thanks a lot!!!!:thumbup:
it was what i was looking for!!!!
acutally... i took up on your suggestion earlier about using substring... and it worked too... although the one i was looking for was using arrays... and you did it... waaaaaaaaaiiii!!!!!!!! Thank you again!!!!
you just saved meh life!!!!:thumbup:

#6
Xdawn90

Xdawn90

    Learning Programmer

  • Members
  • PipPipPip
  • 55 posts
Glad that it helps.

Anyway, hands-on practices will surely improve your skills in programming.

Good luck and happy programming.

Regards.

#7
speachy_15

speachy_15

    Learning Programmer

  • Members
  • PipPipPip
  • 31 posts
Thanks again!!!! waaaaaaaaaaaiiiii!!!!!!!!!!!!!:thumbup:




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users