Page 1 of 2 12 LastLast
Results 1 to 20 of 25
  1. #1
    Join Date
    Jan 2009
    Posts
    90
    Rep Power
    0

    Default what about this?

    i need help on where do i start

    i want to write a program after a user's 4-digit input like 1234 and output display each digit like this
    1
    2
    3
    4

  2. #2
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default What have you tried?

    ... and what have you tried? I would think something like the following would work:
    • get user's input as a string (probably use the scanner class)
    • then in a "for" loop, exact each character (using String's charAt() method) and print (using println) each character.

    Some useful links:
    String (Java Platform SE 6)

    Scanner (Java Platform SE 6)

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  3. #3
    Join Date
    Jan 2009
    Posts
    90
    Rep Power
    0

    Default

    the user input will not be constant tho.

    im scratching my head, i dont know how to separate the user's 4-digit input number.

  4. #4
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default Don't scratch too hard....

    Did you read the links ? ... specially the String charAt() method. Before going on, It would be good to know how much Java you know. What have you studied? Can you answer the following questions:
    • Do you know the basics of Java programing? The parts of a program?
    • Have you made it to the "Hello World" program?
    • Do you know how a "for" loop works?

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  5. #5
    Join Date
    Jan 2009
    Posts
    90
    Rep Power
    0

    Default

    Quote Originally Posted by CJSLMAN View Post
    Did you read the links ? ... specially the String charAt() method. Before going on, It would be good to know how much Java you know. What have you studied? Can you answer the following questions:
    • Do you know the basics of Java programing? The parts of a program?
    • Have you made it to the "Hello World" program?
    • Do you know how a "for" loop works?

    Luck,
    CJSL

    im taking this class for the first time, so consider me as a blank sheet of paper.

  6. #6
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    "Have you made it to the "Hello World" program?" LOL, thats too harsh
    Last edited by angryboy; 01-17-2009 at 04:53 AM.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  7. #7
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default Sample

    I see that in your other thread, you used the Scanner class correctly. So I'll give you this sample for String.charAt(). The java doc can be confusing at first when you're not acustom to it.

    PHP Code:
    public class HW{
      public static void main(String[] args){
        String str = "Hello World";
    	
    	System.out.println( str.charAt(0) );
    	System.out.println( str.charAt(1) );
    	System.out.println( str.charAt(2) );
    	System.out.println( str.charAt(3) );
    	System.out.println( str.charAt(4) );
    	
    	for(int i=5; i<str.length(); i++){
    		System.out.println( str.charAt(i) );
    	}
      }
    }
    Java Code:
    OUTPUT:
    s:\temp>java HW
    H
    e
    l
    l
    o
    
    W
    o
    r
    l
    d
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  8. #8
    Join Date
    Jan 2009
    Posts
    90
    Rep Power
    0

    Default

    Thanks alot for you help!

    but i want the program to display the input from the user.

    example of an output:
    Enter an integer value: 3419
    The digits of the 3419 are
    3
    4
    1
    9
    Last edited by PureAwesomeness; 01-17-2009 at 10:33 AM.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    well how about lets see you give it a try. You've got some great starting code to work from.

  10. #10
    MuslimCoder is offline Senior Member
    Join Date
    Jan 2009
    Posts
    119
    Rep Power
    0

    Default

    Hello,

    you should use a while loop to get the these types of results, but you would also have to use an Array.

    Java Code:
    int count = 0 ;
    int n;
    int Arrnum;
    
    
    while(n != 0)
    {
    //get the last digit of the number using modulus.
        Arrnum[count] = n%10;
    //divide to get rid of the read number. Since n is an int, it would //cut off the decimal value.
    
        n = n /10;
       
    
    // increment the counter
       count = count  + 1;
    }
    or you can use it without the arrays. doing it manually for each number.
    Java Code:
    int a, b, c,d;
    a = n%10;
    n=n/10;
    
    b= n%10;
    n=n/10;
    
    c = n%10;
    n=n/10;
    
    d = n%10;
    n=n/10;
    :)

  11. #11
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    hey MC, did u post in the wrong thread??

    PureAwesomeness: You would need to use the Scanner class to read user input from the console. From your other thread, I see you used it correctly. Why not post what you done so far, so we can help you edit it.

    OH! I get it, Fubar ABLE!! LOL. Why didn't I see that before.
    Last edited by angryboy; 01-17-2009 at 06:55 PM.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  12. #12
    Join Date
    Jan 2009
    Posts
    90
    Rep Power
    0

    Default

    this is what i have
    the problem with this one is that the user have to enter the number separate with a space:mad:
    it should be too complicated because this program was given as hw after three lectures.
    Java Code:
    import java.util.Scanner;
    
    	public class hw1 {
    
    	public static void main (String[]args){
    
    	Scanner input =  new Scanner(System.in);
    
    	System.out.println("Enter 4-digit of integer: ");
    
    	int a = input.nextInt();
    
    	int b = input.nextInt();
    
    	int c = input.nextInt();
    
    	int d = input.nextInt();
    
    	System.out.println(a);
    
    	System.out.println(b);
    
    	System.out.println(c);
    
    	System.out.println(d);
    	}
    }
    Last edited by PureAwesomeness; 01-17-2009 at 07:51 PM.

  13. #13
    MuslimCoder is offline Senior Member
    Join Date
    Jan 2009
    Posts
    119
    Rep Power
    0

    Default

    hey MC, did u post in the wrong thread??
    No, I was just suggesting a way that he could use.

    I think using modulus you can do it...
    take for example : scanner gets 1000

    n = 1000;
    a = n % 10; (would return 0);
    n = n /10; (would cut off .0 from 100.0)
    b = n % 10;
    n = n /10;
    ...so on

  14. #14
    2potatocakes is offline Member
    Join Date
    Sep 2008
    Posts
    43
    Rep Power
    0

    Default

    mc that just looks bonkers!? PureAwesomness, trust me it's a very simple question and it's reasonable for your teacher to ask you to do this after 3 lectures. Problem is, it's tough to dumb this down so that your teacher won't realise you've cheated and asked somebody for the answer. Think angry boy was closer to the mark tho.. But if you want it to output the users input just use this:

    public class HW{
    public static void main(String[] args){

    String str = JOptionPane.showInputDialog("Enter 4-digit of integer: ");

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

    Haven't tested that but 99% sure it'll work. nice angry boy... hello world ;)
    Last edited by 2potatocakes; 01-17-2009 at 08:30 PM.

  15. #15
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default Honest to Pete...

    MC & specially 2potatocakes... please realize (be aware) that the OP is completely clueless about Java. Notice that the OP didn't even try to answer my questions about his level of Java knowledge. I know that you think you're helping him by throwing all sorts of code at the OP, but try to understand that:
    • The OP doesn't have an idea what the code is doing that you are giving him (specially what MC posted)
    • Giving complete solutions to OPs doesn't help. It makes things worse.
    • The OP is not showing any effort to understand the information that's being given and is hoping that somebody will give him the complete solution and he can presentent it as his own
    .
    If you want to help him, please explain it to him. Give him hints, code snippets, links, etc. You guys should know this (I've ranted about it enough). If you want to create a genetration of programmers who can't think for themselves, can't investigate and who are helpless if somebody doesn't give them the answer/solution, then keep on throwing solutions.

    I don't want to sound harsh, ungrateful or down right mean, but if you want to help other people on the forum, do it the right way.

    Thanks for understanding...
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  16. #16
    Join Date
    Jan 2009
    Posts
    90
    Rep Power
    0

    Default

    The OP is not showing any effort to understand the information that's being given and is hoping that somebody will give him the complete solution and he can presentent it as his own.
    why you have to be so negative?

  17. #17
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    why you have to be so negative?
    He's not. What he says is completely correct. For you to learn, you must put in the effort, to struggle; it's as simple as that. Getting nudges in the right direction can help you, but getting complete answers here will only set you behind on your next project. If you are completely baffled by the nudges, then ask. If you are still baffled, then you should consider arranging some face to face one-on-one time with your teacher or with a tutor.

    Best of luck.
    Last edited by Fubarable; 01-17-2009 at 11:03 PM.

  18. #18
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default Just being realistic...

    Like I said, I'm not being mean or negative... believe it or not, I'm trying to help you. I asked you if you knew how a "for" loop works? If you had avswered me with a no, I would have tried to explain it to you. I gave you some links to help you. Have you reviewed them? Did you understand them?
    I don't know.
    Since you state that you are a "blank sheet a paper" then please start here:
    The Java™ Tutorials
    It's a great way to start to learn Java.

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  19. #19
    Join Date
    Jan 2009
    Posts
    90
    Rep Power
    0

    Default

    i did take look at the links that you gave me. i understand the concept of using Scanner but not strings. in most of my assignments contain Scanner so i got that part. Strings i have little knowledge on it and dont really know how to use it exactly. As for loop it is completely out of my knowledge and i believe my instructor didnt teach the class yet.

    Sorry i misunderstanded you
    and thanks for your help :)

  20. #20
    MuslimCoder is offline Senior Member
    Join Date
    Jan 2009
    Posts
    119
    Rep Power
    0

    Default

    Hello CJSLMAN,

    I understand. You have spoken the truth. :)

    I was thinking up of something and I think I have an idea... How about say for example if some one is a begginer, just like I am. You guyz can post up a thread that asks us to make programs.

    What do you people all think?

    Then afterward everyone can suggest a best way to write it: fastest, smallest, and simplest.
    :)

    I think that would be a boost.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •