Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 13 of 13

Thread: Help Needed In the Code: Occurrence of Digits in a Number

  1. #1
    Junior Member
    Join Date
    Aug 2014
    Location
    India
    Posts
    6
    My Mood
    Tired
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Help Needed In the Code: Occurrence of Digits in a Number

    Hey!
    So I got the following assignment- Make a program which takes input from the user (6-digit number) and check how many times each digit gets repeated in the number. Java GUI was used (Netbeans 6.5.1).

    This is my code. I took the input via a JTextField named tf and displayed the output on JTextArea ta.

    PS- arrays not used as we haven't studied them yet
    - Sorry if there are any problems with indentations/documentation.


    int num= Integer.parseInt(tf.getText());
            int no=num;
            if(num<100000)
                ta.setText("Invalid Input. Please Enter 6 digit number");
     
            int n1= num%10;
            num=num/10;     //stores last digit
     
            int n2= num%10;
            num=num/10;
     
            int n3= num%10;
            num=num/10;
     
            int n4= num%10;
            num=num/10;
     
            int n5= num%10;
            num=num/10;
     
            int n6= num%10;
            num=num/10;
     
     
            //Having stored all the digits in separate variables, we will now run loops to check the number's frequency 
     
     
            int i1 = 0,i2 = 0,i3 = 0,i4 = 0,i5 = 0,i6=0;
     
            while(no>0)
            {
              int rem= no%10;
     
              if (rem==n1)
                  i1++;
              else
                  if (rem==n2)
                   i2++;
                  else
                      if (rem==n3)
                      i3++;
                      else
                            if (rem==n4)
                             i4++;
                            else
                                 if (rem==n5)
                                    i5++;
                                 else
                                      if (rem==n6)
                                        i6++;
              no=no/10;
            }
     
            //Printing the output below
     
            ta.append(n1+" "+"occured"+" "+i1+" "+"times"+"\n"+n2+" "+"occured"+" "+i2+" "+"times"+"\n");
            ta.append(n3+" "+"occured"+" "+i3+" "+"times"+"\n"+n4+" "+"occured"+" "+i4+" "+"times"+"\n");
            ta.append(n5+" "+"occured"+" "+i5+" "+"times"+"\n"+n6+" "+"occured"+" "+i6+" "+"times");


    The problem is that in case of numbers like 666666, the output is -
    6 occured 6 times
    6 occured 0 times
    6 occured 0 times
    6 occured 0 times
    6 occured 0 times
    6 occured 0 times

    How do I omit the last 5 lines? This problem occurs whenever I input a digit with any recurring digit.
    What changes/additions should I make?


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,659
    Thanks
    25
    Thanked 325 Times in 302 Posts

    Default Re: Help Needed In the Code: Occurrence of Digits in a Number

    Out of curiosity, why don't you keep the input as a String and simply parse the String?

    For example, here is sample code which would parse a number (stored as a String) for a specific digit:
    String input = 57426
    int toFind = 4;
    System.out.println(indexOf(input,toFind)+"");
     
    public int indexOf(String input,int toFind) {
    	// Loop the length of the String
    	for(int i=0;i<input.length;i++) {
    		// Get the digit by pulling the character from the index and converting it
    		int digit = Character.getNumericValue(input.charAt(i));
    		// Compare the number you are searching for with the digit
    		if(toFind==digit) {
    			// Return the index if it is found
    			return i;
    		}
    	}
    	return -1;
    }

    And this code would output: 2
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,126
    My Mood
    Mellow
    Thanks
    186
    Thanked 658 Times in 645 Posts

    Default Re: Help Needed In the Code: Occurrence of Digits in a Number

    Welcome to the forum! Thanks for taking the time to learn how to post code correctly. If you haven't already, please read this topic to learn other useful info for new members.

    Use in if() statement to exclude the output of undesired results.

  4. #4
    Junior Member
    Join Date
    Aug 2014
    Location
    India
    Posts
    6
    My Mood
    Tired
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help Needed In the Code: Occurrence of Digits in a Number

    Quote Originally Posted by GregBrannon View Post
    Welcome to the forum! Thanks for taking the time to learn how to post code correctly. If you haven't already, please read this topic to learn other useful info for new members.

    Use in if() statement to exclude the output of undesired results.
    Could you be a bit more specific about it with reference to the code?
    Thanks!

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,420
    Thanks
    49
    Thanked 2,215 Times in 2,188 Posts

    Default Re: Help Needed In the Code: Occurrence of Digits in a Number

    How do I omit the last 5 lines?
    What needs to be compared so the code does not print those lines?
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Aug 2014
    Location
    India
    Posts
    6
    My Mood
    Tired
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help Needed In the Code: Occurrence of Digits in a Number

    Quote Originally Posted by Norm View Post
    What needs to be compared so the code does not print those lines?
    The value of counter variables i1,i2,etc I guess

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,420
    Thanks
    49
    Thanked 2,215 Times in 2,188 Posts

    Default Re: Help Needed In the Code: Occurrence of Digits in a Number

    The value of counter variables i1,i2,etc I guess
    Are you sure?

    A problem with understanding the code is the way the variables are named.
    What do the variables that begin with n hold? And those that begin with i?

    Variable names should describe what the variable holds. The variables in this program hold the value of the digit and the count of the uses of that digit. i1 and n1 don't say that.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Junior Member
    Join Date
    Aug 2014
    Location
    India
    Posts
    6
    My Mood
    Tired
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help Needed In the Code: Occurrence of Digits in a Number

    Quote Originally Posted by Norm View Post
    Are you sure?

    A problem with understanding the code is the way the variables are named.
    What do the variables that begin with n hold? And those that begin with i?
    variables i1,i2,etc. are the counter variables which check how many times the loop has run so as to get the number of times the digit get repeated.
    n1-n6 represent the individual digits stored separately. For example for 123456,
    n1=6
    n2=5
    n3=4
    n4=3
    n5=2
    n6=1

    So sorry about the confusing names... I couldn't figure out what to name them

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,420
    Thanks
    49
    Thanked 2,215 Times in 2,188 Posts

    Default Re: Help Needed In the Code: Occurrence of Digits in a Number

    How about: theDigit1, theDigit2, etc
    and countOfDigit1, countOfDigit2, etc

    Back to post#5 about this question: How do I omit the last 5 lines?
    What is there about those lines that the code can detect to not print them?
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Junior Member
    Join Date
    Aug 2014
    Location
    India
    Posts
    6
    My Mood
    Tired
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help Needed In the Code: Occurrence of Digits in a Number

    Quote Originally Posted by Norm View Post

    What is there about those lines that the code can detect to not print them?
    I guess we could check if the value of any of the counter variables is zero and then not rpint it...the part that is ci=onfusing me is how to do it without making the code inefficient

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,420
    Thanks
    49
    Thanked 2,215 Times in 2,188 Posts

    Default Re: Help Needed In the Code: Occurrence of Digits in a Number

    how to do it without making the code inefficient
    Not sure what you think would be inefficient.
    Get the code to work first. Then if there is a problem change it.

    The main inefficiency problem with the code is all the variables instead of using arrays.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #12
    Junior Member
    Join Date
    Aug 2014
    Location
    India
    Posts
    6
    My Mood
    Tired
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help Needed In the Code: Occurrence of Digits in a Number

    Quote Originally Posted by Norm View Post

    The main inefficiency problem with the code is all the variables instead of using arrays.
    So sorry about that...It is my first year learning Java GUI programming and we haven't touched the topic of arrays yet!

  13. #13
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    583
    Thanks
    0
    Thanked 105 Times in 90 Posts

    Default Re: Help Needed In the Code: Occurrence of Digits in a Number

    Then you shouldnt even think about such things as efficiency or inefficiency. Even some more advanced programmers dont understand much about it.

Similar Threads

  1. URGENT HELP NEEDED IN AN HOUR:Printing first number in input
    By Java girl in forum What's Wrong With My Code?
    Replies: 16
    Last Post: March 22nd, 2014, 10:47 AM
  2. HELP Please- Putting Digits within a number in ascending order
    By Java girl in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 21st, 2014, 01:46 AM
  3. Replies: 2
    Last Post: October 25th, 2013, 06:31 AM
  4. [SOLVED] Extracting whole number digits from a float
    By Johnny Bravo in forum Algorithms & Recursion
    Replies: 2
    Last Post: August 12th, 2012, 04:04 PM
  5. Replies: 3
    Last Post: October 19th, 2011, 02:17 PM

Tags for this Thread