The sum of certain digits from an input numberHow do you assert that a certain exception is thrown in JUnit 4 tests?Create ArrayList from arrayHow do I call one constructor from another in Java?How to get an enum value from a string value in Java?Java - summing up Array list valueshow to calculate the sum of digits for a number in java?Why is executing Java code in comments with certain Unicode characters allowed?Java - Method executed prior to Default ConstructorPassing Multiple Numbers in a Parameter for an ArrayWould it make any difference giving arguments using scanner class instead of command line arguments?

Two researchers want to work on the same extension to my paper. Who to help?

Is a diamond sword feasible?

Guns in space with bullets that return?

Why use steam instead of just hot air?

Can I use my laptop, which says 240V, in the USA?

Is calcium chloride an acidic or basic salt?

about the word 地図

Why was this sacrifice sufficient?

How to pronounce "r" after a "g"?

Does the 500 feet falling cap apply per fall, or per turn?

Is it a bad idea to replace pull-up resistors with hard pull-ups?

Noob at soldering, can anyone explain why my circuit won't work?

How could a Lich maintain the appearance of being alive without magic?

On what legal basis did the UK remove the 'European Union' from its passport?

Pre-1993 comic in which Wolverine's claws were turned to rubber?

How can I make a vertical rule that extends to the edges of an fbox?

How can this pool heater gas line be disconnected?

On studying Computer Science vs. Software Engineering to become a proficient coder

Was there a contingency plan in place if Little Boy failed to detonate?

Make all the squares explode

Is there a faster way to calculate Abs[z]^2 numerically?

Why was the Ancient One so hesitant to teach Dr. Strange the art of sorcery?

Why do Thanos's punches not kill Captain America or at least cause some mortal injuries?

What is the best way for a skeleton to impersonate human without using magic?



The sum of certain digits from an input number


How do you assert that a certain exception is thrown in JUnit 4 tests?Create ArrayList from arrayHow do I call one constructor from another in Java?How to get an enum value from a string value in Java?Java - summing up Array list valueshow to calculate the sum of digits for a number in java?Why is executing Java code in comments with certain Unicode characters allowed?Java - Method executed prior to Default ConstructorPassing Multiple Numbers in a Parameter for an ArrayWould it make any difference giving arguments using scanner class instead of command line arguments?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I've been trying to sum a certain digit from a number, for example



Number: 5
Input: 54365
Output: The sum of 5's is 10


Second example:



Number: 5
Input: 5437555
Output: The sum of 5's is 20


I've managed to separate the digits yet I couldn't find the condition to sum
a certain number (for instance number 5 like the examples).



I'd appreciate to hear your idea of doing it



public static void main(String[] args) 
int sum = 0;
int number = MyConsole.readInt("Enter a number:");
while (number > 0)
if (number % 10 == 8)
sum += 8;
else
number /= 10;


System.out.println("The sum of 8's is:" + sum);



My way of separating the numbers.

Also i have a small program to get input instead of using scanner since we still haven't went through it in class.










share|improve this question
























  • All inputs are in String format?

    – Nicholas K
    Mar 23 at 11:15






  • 5





    Hi and welcome to Stackoverflow! Please provide what you have tried so far so we could help you.

    – Mustahsan
    Mar 23 at 11:15






  • 1





    If you could provide some code, it would be great :)

    – Yassin Hajaj
    Mar 23 at 11:16











  • You haven't posted any single line of your code, so we can't tell you how to fix/improve it.

    – JB Nizet
    Mar 23 at 11:16






  • 1





    Please read and follow How do I ask a good question? to get helped.

    – BionicCode
    Mar 23 at 11:19

















0















I've been trying to sum a certain digit from a number, for example



Number: 5
Input: 54365
Output: The sum of 5's is 10


Second example:



Number: 5
Input: 5437555
Output: The sum of 5's is 20


I've managed to separate the digits yet I couldn't find the condition to sum
a certain number (for instance number 5 like the examples).



I'd appreciate to hear your idea of doing it



public static void main(String[] args) 
int sum = 0;
int number = MyConsole.readInt("Enter a number:");
while (number > 0)
if (number % 10 == 8)
sum += 8;
else
number /= 10;


System.out.println("The sum of 8's is:" + sum);



My way of separating the numbers.

Also i have a small program to get input instead of using scanner since we still haven't went through it in class.










share|improve this question
























  • All inputs are in String format?

    – Nicholas K
    Mar 23 at 11:15






  • 5





    Hi and welcome to Stackoverflow! Please provide what you have tried so far so we could help you.

    – Mustahsan
    Mar 23 at 11:15






  • 1





    If you could provide some code, it would be great :)

    – Yassin Hajaj
    Mar 23 at 11:16











  • You haven't posted any single line of your code, so we can't tell you how to fix/improve it.

    – JB Nizet
    Mar 23 at 11:16






  • 1





    Please read and follow How do I ask a good question? to get helped.

    – BionicCode
    Mar 23 at 11:19













0












0








0








I've been trying to sum a certain digit from a number, for example



Number: 5
Input: 54365
Output: The sum of 5's is 10


Second example:



Number: 5
Input: 5437555
Output: The sum of 5's is 20


I've managed to separate the digits yet I couldn't find the condition to sum
a certain number (for instance number 5 like the examples).



I'd appreciate to hear your idea of doing it



public static void main(String[] args) 
int sum = 0;
int number = MyConsole.readInt("Enter a number:");
while (number > 0)
if (number % 10 == 8)
sum += 8;
else
number /= 10;


System.out.println("The sum of 8's is:" + sum);



My way of separating the numbers.

Also i have a small program to get input instead of using scanner since we still haven't went through it in class.










share|improve this question
















I've been trying to sum a certain digit from a number, for example



Number: 5
Input: 54365
Output: The sum of 5's is 10


Second example:



Number: 5
Input: 5437555
Output: The sum of 5's is 20


I've managed to separate the digits yet I couldn't find the condition to sum
a certain number (for instance number 5 like the examples).



I'd appreciate to hear your idea of doing it



public static void main(String[] args) 
int sum = 0;
int number = MyConsole.readInt("Enter a number:");
while (number > 0)
if (number % 10 == 8)
sum += 8;
else
number /= 10;


System.out.println("The sum of 8's is:" + sum);



My way of separating the numbers.

Also i have a small program to get input instead of using scanner since we still haven't went through it in class.







java






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 23 at 13:29









LppEdd

10.1k31749




10.1k31749










asked Mar 23 at 11:14









Aviv David MalkaAviv David Malka

112




112












  • All inputs are in String format?

    – Nicholas K
    Mar 23 at 11:15






  • 5





    Hi and welcome to Stackoverflow! Please provide what you have tried so far so we could help you.

    – Mustahsan
    Mar 23 at 11:15






  • 1





    If you could provide some code, it would be great :)

    – Yassin Hajaj
    Mar 23 at 11:16











  • You haven't posted any single line of your code, so we can't tell you how to fix/improve it.

    – JB Nizet
    Mar 23 at 11:16






  • 1





    Please read and follow How do I ask a good question? to get helped.

    – BionicCode
    Mar 23 at 11:19

















  • All inputs are in String format?

    – Nicholas K
    Mar 23 at 11:15






  • 5





    Hi and welcome to Stackoverflow! Please provide what you have tried so far so we could help you.

    – Mustahsan
    Mar 23 at 11:15






  • 1





    If you could provide some code, it would be great :)

    – Yassin Hajaj
    Mar 23 at 11:16











  • You haven't posted any single line of your code, so we can't tell you how to fix/improve it.

    – JB Nizet
    Mar 23 at 11:16






  • 1





    Please read and follow How do I ask a good question? to get helped.

    – BionicCode
    Mar 23 at 11:19
















All inputs are in String format?

– Nicholas K
Mar 23 at 11:15





All inputs are in String format?

– Nicholas K
Mar 23 at 11:15




5




5





Hi and welcome to Stackoverflow! Please provide what you have tried so far so we could help you.

– Mustahsan
Mar 23 at 11:15





Hi and welcome to Stackoverflow! Please provide what you have tried so far so we could help you.

– Mustahsan
Mar 23 at 11:15




1




1





If you could provide some code, it would be great :)

– Yassin Hajaj
Mar 23 at 11:16





If you could provide some code, it would be great :)

– Yassin Hajaj
Mar 23 at 11:16













You haven't posted any single line of your code, so we can't tell you how to fix/improve it.

– JB Nizet
Mar 23 at 11:16





You haven't posted any single line of your code, so we can't tell you how to fix/improve it.

– JB Nizet
Mar 23 at 11:16




1




1





Please read and follow How do I ask a good question? to get helped.

– BionicCode
Mar 23 at 11:19





Please read and follow How do I ask a good question? to get helped.

– BionicCode
Mar 23 at 11:19












1 Answer
1






active

oldest

votes


















1














Your requirement seems reasonably clear to me.
Given a number



final int number = 5;


And a starting number



final int input = 54365;


The easiest way is to convert that number to a String



final int inputStr = String.valueOf(input);


Then, you can filter each char for it, and sum them



final int sum =
inputStr.chars() // Get a Stream of ints, which represent the characters
.map(Character::getNumericValue) // Convert a char to its numeric representation
.filter(i -> i == number) // Filter for the designated number
.sum(); // Sum all the filtered integers


Output: 10




Using the same approach, but with an old-style for loop



final int input = 54365;
final int inputStr = String.valueOf(input);
final int number = 5;
int sum = 0;

for (final char c : inputStr.toCharArray())
final int n = Character.getNumericValue(c);

if (n == number)
sum += number;





Your solution can work



int number = 5;
int input = 543655;
int sum = 0;

while (input > 0)
if (input % 10 == number)
sum += number;


input /= 10;






share|improve this answer

























  • I am a rookie in computer programming, I was a bit ashamed to post my code here.

    – Aviv David Malka
    Mar 23 at 11:30











  • @AvivDavidMalka do this, post the code you tried. Don't worry about it. We all were rookies. So that we can remove that "put on hold" thing.

    – LppEdd
    Mar 23 at 11:31












  • i was wondering if this could have been done by using while/for loops

    – Aviv David Malka
    Mar 23 at 11:49











  • @AvivDavidMalka see updated answer. Is that acceptable?

    – LppEdd
    Mar 23 at 11:54






  • 1





    Thanks a lot @LppEdd learnt a great deal^^

    – Aviv David Malka
    Mar 23 at 12:52











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55313116%2fthe-sum-of-certain-digits-from-an-input-number%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Your requirement seems reasonably clear to me.
Given a number



final int number = 5;


And a starting number



final int input = 54365;


The easiest way is to convert that number to a String



final int inputStr = String.valueOf(input);


Then, you can filter each char for it, and sum them



final int sum =
inputStr.chars() // Get a Stream of ints, which represent the characters
.map(Character::getNumericValue) // Convert a char to its numeric representation
.filter(i -> i == number) // Filter for the designated number
.sum(); // Sum all the filtered integers


Output: 10




Using the same approach, but with an old-style for loop



final int input = 54365;
final int inputStr = String.valueOf(input);
final int number = 5;
int sum = 0;

for (final char c : inputStr.toCharArray())
final int n = Character.getNumericValue(c);

if (n == number)
sum += number;





Your solution can work



int number = 5;
int input = 543655;
int sum = 0;

while (input > 0)
if (input % 10 == number)
sum += number;


input /= 10;






share|improve this answer

























  • I am a rookie in computer programming, I was a bit ashamed to post my code here.

    – Aviv David Malka
    Mar 23 at 11:30











  • @AvivDavidMalka do this, post the code you tried. Don't worry about it. We all were rookies. So that we can remove that "put on hold" thing.

    – LppEdd
    Mar 23 at 11:31












  • i was wondering if this could have been done by using while/for loops

    – Aviv David Malka
    Mar 23 at 11:49











  • @AvivDavidMalka see updated answer. Is that acceptable?

    – LppEdd
    Mar 23 at 11:54






  • 1





    Thanks a lot @LppEdd learnt a great deal^^

    – Aviv David Malka
    Mar 23 at 12:52















1














Your requirement seems reasonably clear to me.
Given a number



final int number = 5;


And a starting number



final int input = 54365;


The easiest way is to convert that number to a String



final int inputStr = String.valueOf(input);


Then, you can filter each char for it, and sum them



final int sum =
inputStr.chars() // Get a Stream of ints, which represent the characters
.map(Character::getNumericValue) // Convert a char to its numeric representation
.filter(i -> i == number) // Filter for the designated number
.sum(); // Sum all the filtered integers


Output: 10




Using the same approach, but with an old-style for loop



final int input = 54365;
final int inputStr = String.valueOf(input);
final int number = 5;
int sum = 0;

for (final char c : inputStr.toCharArray())
final int n = Character.getNumericValue(c);

if (n == number)
sum += number;





Your solution can work



int number = 5;
int input = 543655;
int sum = 0;

while (input > 0)
if (input % 10 == number)
sum += number;


input /= 10;






share|improve this answer

























  • I am a rookie in computer programming, I was a bit ashamed to post my code here.

    – Aviv David Malka
    Mar 23 at 11:30











  • @AvivDavidMalka do this, post the code you tried. Don't worry about it. We all were rookies. So that we can remove that "put on hold" thing.

    – LppEdd
    Mar 23 at 11:31












  • i was wondering if this could have been done by using while/for loops

    – Aviv David Malka
    Mar 23 at 11:49











  • @AvivDavidMalka see updated answer. Is that acceptable?

    – LppEdd
    Mar 23 at 11:54






  • 1





    Thanks a lot @LppEdd learnt a great deal^^

    – Aviv David Malka
    Mar 23 at 12:52













1












1








1







Your requirement seems reasonably clear to me.
Given a number



final int number = 5;


And a starting number



final int input = 54365;


The easiest way is to convert that number to a String



final int inputStr = String.valueOf(input);


Then, you can filter each char for it, and sum them



final int sum =
inputStr.chars() // Get a Stream of ints, which represent the characters
.map(Character::getNumericValue) // Convert a char to its numeric representation
.filter(i -> i == number) // Filter for the designated number
.sum(); // Sum all the filtered integers


Output: 10




Using the same approach, but with an old-style for loop



final int input = 54365;
final int inputStr = String.valueOf(input);
final int number = 5;
int sum = 0;

for (final char c : inputStr.toCharArray())
final int n = Character.getNumericValue(c);

if (n == number)
sum += number;





Your solution can work



int number = 5;
int input = 543655;
int sum = 0;

while (input > 0)
if (input % 10 == number)
sum += number;


input /= 10;






share|improve this answer















Your requirement seems reasonably clear to me.
Given a number



final int number = 5;


And a starting number



final int input = 54365;


The easiest way is to convert that number to a String



final int inputStr = String.valueOf(input);


Then, you can filter each char for it, and sum them



final int sum =
inputStr.chars() // Get a Stream of ints, which represent the characters
.map(Character::getNumericValue) // Convert a char to its numeric representation
.filter(i -> i == number) // Filter for the designated number
.sum(); // Sum all the filtered integers


Output: 10




Using the same approach, but with an old-style for loop



final int input = 54365;
final int inputStr = String.valueOf(input);
final int number = 5;
int sum = 0;

for (final char c : inputStr.toCharArray())
final int n = Character.getNumericValue(c);

if (n == number)
sum += number;





Your solution can work



int number = 5;
int input = 543655;
int sum = 0;

while (input > 0)
if (input % 10 == number)
sum += number;


input /= 10;







share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 23 at 12:40

























answered Mar 23 at 11:25









LppEddLppEdd

10.1k31749




10.1k31749












  • I am a rookie in computer programming, I was a bit ashamed to post my code here.

    – Aviv David Malka
    Mar 23 at 11:30











  • @AvivDavidMalka do this, post the code you tried. Don't worry about it. We all were rookies. So that we can remove that "put on hold" thing.

    – LppEdd
    Mar 23 at 11:31












  • i was wondering if this could have been done by using while/for loops

    – Aviv David Malka
    Mar 23 at 11:49











  • @AvivDavidMalka see updated answer. Is that acceptable?

    – LppEdd
    Mar 23 at 11:54






  • 1





    Thanks a lot @LppEdd learnt a great deal^^

    – Aviv David Malka
    Mar 23 at 12:52

















  • I am a rookie in computer programming, I was a bit ashamed to post my code here.

    – Aviv David Malka
    Mar 23 at 11:30











  • @AvivDavidMalka do this, post the code you tried. Don't worry about it. We all were rookies. So that we can remove that "put on hold" thing.

    – LppEdd
    Mar 23 at 11:31












  • i was wondering if this could have been done by using while/for loops

    – Aviv David Malka
    Mar 23 at 11:49











  • @AvivDavidMalka see updated answer. Is that acceptable?

    – LppEdd
    Mar 23 at 11:54






  • 1





    Thanks a lot @LppEdd learnt a great deal^^

    – Aviv David Malka
    Mar 23 at 12:52
















I am a rookie in computer programming, I was a bit ashamed to post my code here.

– Aviv David Malka
Mar 23 at 11:30





I am a rookie in computer programming, I was a bit ashamed to post my code here.

– Aviv David Malka
Mar 23 at 11:30













@AvivDavidMalka do this, post the code you tried. Don't worry about it. We all were rookies. So that we can remove that "put on hold" thing.

– LppEdd
Mar 23 at 11:31






@AvivDavidMalka do this, post the code you tried. Don't worry about it. We all were rookies. So that we can remove that "put on hold" thing.

– LppEdd
Mar 23 at 11:31














i was wondering if this could have been done by using while/for loops

– Aviv David Malka
Mar 23 at 11:49





i was wondering if this could have been done by using while/for loops

– Aviv David Malka
Mar 23 at 11:49













@AvivDavidMalka see updated answer. Is that acceptable?

– LppEdd
Mar 23 at 11:54





@AvivDavidMalka see updated answer. Is that acceptable?

– LppEdd
Mar 23 at 11:54




1




1





Thanks a lot @LppEdd learnt a great deal^^

– Aviv David Malka
Mar 23 at 12:52





Thanks a lot @LppEdd learnt a great deal^^

– Aviv David Malka
Mar 23 at 12:52



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55313116%2fthe-sum-of-certain-digits-from-an-input-number%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현