Java Sum an integer
I want to read a number from the user and then sum the last seven digits of the entered number. What is the best way to do this? This is my code, but unfortunately it does not work:
class ersteAufgabe
public static void main (String args)
Scanner s = new Scanner(System.in);
double a = new double[10];
for (int i = 0;i<10;i++)
a[i]=s.nextInt();
s.close();
System.out.println(a[0]);
I wanted only one number to be read and used as an array. Only now he expects 10 inputs from me.
java arrays
|
show 2 more comments
I want to read a number from the user and then sum the last seven digits of the entered number. What is the best way to do this? This is my code, but unfortunately it does not work:
class ersteAufgabe
public static void main (String args)
Scanner s = new Scanner(System.in);
double a = new double[10];
for (int i = 0;i<10;i++)
a[i]=s.nextInt();
s.close();
System.out.println(a[0]);
I wanted only one number to be read and used as an array. Only now he expects 10 inputs from me.
java arrays
Please read "How to create a Minimal, Complete, and Verifiable example". Then use the edit link to improve your question (do not add more information via comments). Otherwise we are not able to answer your question and help you. "isnt working" ... isnt a working problem description when asking us for help.
– GhostCat
Nov 11 at 16:41
java.util.Scanner
– K.Nicholas
Nov 11 at 16:43
ok thanks, now i used the java util scanner
– Max
Nov 11 at 16:49
Your question says: I want to read a number. And yet you have a loop iterating 10 times, and asking a number at each iteration. So you're asking 10 numbers, not one. So that can't be right. Take a step back, and think about what you're really being asked to do.
– JB Nizet
Nov 11 at 16:50
I wanted only one number to be read and used as an array. Only now he expects 10 inputs from me.
– Max
Nov 11 at 16:51
|
show 2 more comments
I want to read a number from the user and then sum the last seven digits of the entered number. What is the best way to do this? This is my code, but unfortunately it does not work:
class ersteAufgabe
public static void main (String args)
Scanner s = new Scanner(System.in);
double a = new double[10];
for (int i = 0;i<10;i++)
a[i]=s.nextInt();
s.close();
System.out.println(a[0]);
I wanted only one number to be read and used as an array. Only now he expects 10 inputs from me.
java arrays
I want to read a number from the user and then sum the last seven digits of the entered number. What is the best way to do this? This is my code, but unfortunately it does not work:
class ersteAufgabe
public static void main (String args)
Scanner s = new Scanner(System.in);
double a = new double[10];
for (int i = 0;i<10;i++)
a[i]=s.nextInt();
s.close();
System.out.println(a[0]);
I wanted only one number to be read and used as an array. Only now he expects 10 inputs from me.
java arrays
java arrays
edited Nov 11 at 16:51
asked Nov 11 at 16:38
Max
164
164
Please read "How to create a Minimal, Complete, and Verifiable example". Then use the edit link to improve your question (do not add more information via comments). Otherwise we are not able to answer your question and help you. "isnt working" ... isnt a working problem description when asking us for help.
– GhostCat
Nov 11 at 16:41
java.util.Scanner
– K.Nicholas
Nov 11 at 16:43
ok thanks, now i used the java util scanner
– Max
Nov 11 at 16:49
Your question says: I want to read a number. And yet you have a loop iterating 10 times, and asking a number at each iteration. So you're asking 10 numbers, not one. So that can't be right. Take a step back, and think about what you're really being asked to do.
– JB Nizet
Nov 11 at 16:50
I wanted only one number to be read and used as an array. Only now he expects 10 inputs from me.
– Max
Nov 11 at 16:51
|
show 2 more comments
Please read "How to create a Minimal, Complete, and Verifiable example". Then use the edit link to improve your question (do not add more information via comments). Otherwise we are not able to answer your question and help you. "isnt working" ... isnt a working problem description when asking us for help.
– GhostCat
Nov 11 at 16:41
java.util.Scanner
– K.Nicholas
Nov 11 at 16:43
ok thanks, now i used the java util scanner
– Max
Nov 11 at 16:49
Your question says: I want to read a number. And yet you have a loop iterating 10 times, and asking a number at each iteration. So you're asking 10 numbers, not one. So that can't be right. Take a step back, and think about what you're really being asked to do.
– JB Nizet
Nov 11 at 16:50
I wanted only one number to be read and used as an array. Only now he expects 10 inputs from me.
– Max
Nov 11 at 16:51
Please read "How to create a Minimal, Complete, and Verifiable example". Then use the edit link to improve your question (do not add more information via comments). Otherwise we are not able to answer your question and help you. "isnt working" ... isnt a working problem description when asking us for help.
– GhostCat
Nov 11 at 16:41
Please read "How to create a Minimal, Complete, and Verifiable example". Then use the edit link to improve your question (do not add more information via comments). Otherwise we are not able to answer your question and help you. "isnt working" ... isnt a working problem description when asking us for help.
– GhostCat
Nov 11 at 16:41
java.util.Scanner
– K.Nicholas
Nov 11 at 16:43
java.util.Scanner
– K.Nicholas
Nov 11 at 16:43
ok thanks, now i used the java util scanner
– Max
Nov 11 at 16:49
ok thanks, now i used the java util scanner
– Max
Nov 11 at 16:49
Your question says: I want to read a number. And yet you have a loop iterating 10 times, and asking a number at each iteration. So you're asking 10 numbers, not one. So that can't be right. Take a step back, and think about what you're really being asked to do.
– JB Nizet
Nov 11 at 16:50
Your question says: I want to read a number. And yet you have a loop iterating 10 times, and asking a number at each iteration. So you're asking 10 numbers, not one. So that can't be right. Take a step back, and think about what you're really being asked to do.
– JB Nizet
Nov 11 at 16:50
I wanted only one number to be read and used as an array. Only now he expects 10 inputs from me.
– Max
Nov 11 at 16:51
I wanted only one number to be read and used as an array. Only now he expects 10 inputs from me.
– Max
Nov 11 at 16:51
|
show 2 more comments
2 Answers
2
active
oldest
votes
public static int lastDigitsSum(int total)
try (Scanner scan = new Scanner(System.in))
String str = scan.next();
int count = 0;
for (int i = str.length() - 1, j = 0; i >= 0 && j < total; i--, j++)
if (Character.isDigit(str.charAt(i)))
count += str.charAt(i) - '0';
else
throw new RuntimeException("Input is not a number: " + str);
return count;
Thank you very much. But i get this error: Exception in thread "main" java.util.InputMismatchException: For input string: "3457817948" at java.util.Scanner.nextInt(Unknown Source) at java.util.Scanner.nextInt(Unknown Source) at ersteAufgabe.main(ersteAufgabe.java:8)
– Max
Nov 11 at 17:03
@Max See updates
– oleg.cherednik
Nov 11 at 19:05
add a comment |
First you have to recognize if the entered value is a number and has at least 7 digits. Unless you have to output an error message. Convert the entered value to String and use the class Character.isDigit(); to check if the characters are numbers. Then you can use some methods from the String class like substring(..). At the end do a Unit-Test with erroneous/valid values to see if your code is robust. Close the BufferedReader and Resources when you are done by using finally br.close() . Push your code in methods and use an instance class erste-Aufgabe (first exercise).. When you are really really done use JFrame for a GUI-Application.
private static final int SUM_LAST_DIGITS = 7;
public void minimalSolution()
String enteredValue = "";
showInfoMessage("Please enter your number with at least " + SUM_LAST_DIGITS + " digits!");
try (Scanner scan = new Scanner(System.in))
enteredValue = scan.next();
if (enteredValue.matches("^[0-9]" + SUM_LAST_DIGITS + ",$"))
showInfoMessage(enteredValue, lastDigitsSum(enteredValue));
else
showErrorMessage(enteredValue);
catch(Exception e)
showErrorMessage(e.toString());
public int lastDigitsSum(String value)
int count = 0;
for (int i = value.length() - 1, j = 0; i >= 0 && j < SUM_LAST_DIGITS; i--, j++)
count += value.charAt(i) - '0';
return count;
public void showInfoMessage(String parMessage)
System.out.println(parMessage);
public void showInfoMessage(String parValue, int parSum)
System.out.println("Your entered value: [" + parValue + "]");
System.out.println("The summed value of the last 7 digits are: [" + parSum + "]");
public void showErrorMessage(String parValue)
System.err.println("Your entered value: [" + parValue + "] is not a valid number!");
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53250865%2fjava-sum-an-integer%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
public static int lastDigitsSum(int total)
try (Scanner scan = new Scanner(System.in))
String str = scan.next();
int count = 0;
for (int i = str.length() - 1, j = 0; i >= 0 && j < total; i--, j++)
if (Character.isDigit(str.charAt(i)))
count += str.charAt(i) - '0';
else
throw new RuntimeException("Input is not a number: " + str);
return count;
Thank you very much. But i get this error: Exception in thread "main" java.util.InputMismatchException: For input string: "3457817948" at java.util.Scanner.nextInt(Unknown Source) at java.util.Scanner.nextInt(Unknown Source) at ersteAufgabe.main(ersteAufgabe.java:8)
– Max
Nov 11 at 17:03
@Max See updates
– oleg.cherednik
Nov 11 at 19:05
add a comment |
public static int lastDigitsSum(int total)
try (Scanner scan = new Scanner(System.in))
String str = scan.next();
int count = 0;
for (int i = str.length() - 1, j = 0; i >= 0 && j < total; i--, j++)
if (Character.isDigit(str.charAt(i)))
count += str.charAt(i) - '0';
else
throw new RuntimeException("Input is not a number: " + str);
return count;
Thank you very much. But i get this error: Exception in thread "main" java.util.InputMismatchException: For input string: "3457817948" at java.util.Scanner.nextInt(Unknown Source) at java.util.Scanner.nextInt(Unknown Source) at ersteAufgabe.main(ersteAufgabe.java:8)
– Max
Nov 11 at 17:03
@Max See updates
– oleg.cherednik
Nov 11 at 19:05
add a comment |
public static int lastDigitsSum(int total)
try (Scanner scan = new Scanner(System.in))
String str = scan.next();
int count = 0;
for (int i = str.length() - 1, j = 0; i >= 0 && j < total; i--, j++)
if (Character.isDigit(str.charAt(i)))
count += str.charAt(i) - '0';
else
throw new RuntimeException("Input is not a number: " + str);
return count;
public static int lastDigitsSum(int total)
try (Scanner scan = new Scanner(System.in))
String str = scan.next();
int count = 0;
for (int i = str.length() - 1, j = 0; i >= 0 && j < total; i--, j++)
if (Character.isDigit(str.charAt(i)))
count += str.charAt(i) - '0';
else
throw new RuntimeException("Input is not a number: " + str);
return count;
edited Nov 11 at 19:05
answered Nov 11 at 16:55
oleg.cherednik
5,50521017
5,50521017
Thank you very much. But i get this error: Exception in thread "main" java.util.InputMismatchException: For input string: "3457817948" at java.util.Scanner.nextInt(Unknown Source) at java.util.Scanner.nextInt(Unknown Source) at ersteAufgabe.main(ersteAufgabe.java:8)
– Max
Nov 11 at 17:03
@Max See updates
– oleg.cherednik
Nov 11 at 19:05
add a comment |
Thank you very much. But i get this error: Exception in thread "main" java.util.InputMismatchException: For input string: "3457817948" at java.util.Scanner.nextInt(Unknown Source) at java.util.Scanner.nextInt(Unknown Source) at ersteAufgabe.main(ersteAufgabe.java:8)
– Max
Nov 11 at 17:03
@Max See updates
– oleg.cherednik
Nov 11 at 19:05
Thank you very much. But i get this error: Exception in thread "main" java.util.InputMismatchException: For input string: "3457817948" at java.util.Scanner.nextInt(Unknown Source) at java.util.Scanner.nextInt(Unknown Source) at ersteAufgabe.main(ersteAufgabe.java:8)
– Max
Nov 11 at 17:03
Thank you very much. But i get this error: Exception in thread "main" java.util.InputMismatchException: For input string: "3457817948" at java.util.Scanner.nextInt(Unknown Source) at java.util.Scanner.nextInt(Unknown Source) at ersteAufgabe.main(ersteAufgabe.java:8)
– Max
Nov 11 at 17:03
@Max See updates
– oleg.cherednik
Nov 11 at 19:05
@Max See updates
– oleg.cherednik
Nov 11 at 19:05
add a comment |
First you have to recognize if the entered value is a number and has at least 7 digits. Unless you have to output an error message. Convert the entered value to String and use the class Character.isDigit(); to check if the characters are numbers. Then you can use some methods from the String class like substring(..). At the end do a Unit-Test with erroneous/valid values to see if your code is robust. Close the BufferedReader and Resources when you are done by using finally br.close() . Push your code in methods and use an instance class erste-Aufgabe (first exercise).. When you are really really done use JFrame for a GUI-Application.
private static final int SUM_LAST_DIGITS = 7;
public void minimalSolution()
String enteredValue = "";
showInfoMessage("Please enter your number with at least " + SUM_LAST_DIGITS + " digits!");
try (Scanner scan = new Scanner(System.in))
enteredValue = scan.next();
if (enteredValue.matches("^[0-9]" + SUM_LAST_DIGITS + ",$"))
showInfoMessage(enteredValue, lastDigitsSum(enteredValue));
else
showErrorMessage(enteredValue);
catch(Exception e)
showErrorMessage(e.toString());
public int lastDigitsSum(String value)
int count = 0;
for (int i = value.length() - 1, j = 0; i >= 0 && j < SUM_LAST_DIGITS; i--, j++)
count += value.charAt(i) - '0';
return count;
public void showInfoMessage(String parMessage)
System.out.println(parMessage);
public void showInfoMessage(String parValue, int parSum)
System.out.println("Your entered value: [" + parValue + "]");
System.out.println("The summed value of the last 7 digits are: [" + parSum + "]");
public void showErrorMessage(String parValue)
System.err.println("Your entered value: [" + parValue + "] is not a valid number!");
add a comment |
First you have to recognize if the entered value is a number and has at least 7 digits. Unless you have to output an error message. Convert the entered value to String and use the class Character.isDigit(); to check if the characters are numbers. Then you can use some methods from the String class like substring(..). At the end do a Unit-Test with erroneous/valid values to see if your code is robust. Close the BufferedReader and Resources when you are done by using finally br.close() . Push your code in methods and use an instance class erste-Aufgabe (first exercise).. When you are really really done use JFrame for a GUI-Application.
private static final int SUM_LAST_DIGITS = 7;
public void minimalSolution()
String enteredValue = "";
showInfoMessage("Please enter your number with at least " + SUM_LAST_DIGITS + " digits!");
try (Scanner scan = new Scanner(System.in))
enteredValue = scan.next();
if (enteredValue.matches("^[0-9]" + SUM_LAST_DIGITS + ",$"))
showInfoMessage(enteredValue, lastDigitsSum(enteredValue));
else
showErrorMessage(enteredValue);
catch(Exception e)
showErrorMessage(e.toString());
public int lastDigitsSum(String value)
int count = 0;
for (int i = value.length() - 1, j = 0; i >= 0 && j < SUM_LAST_DIGITS; i--, j++)
count += value.charAt(i) - '0';
return count;
public void showInfoMessage(String parMessage)
System.out.println(parMessage);
public void showInfoMessage(String parValue, int parSum)
System.out.println("Your entered value: [" + parValue + "]");
System.out.println("The summed value of the last 7 digits are: [" + parSum + "]");
public void showErrorMessage(String parValue)
System.err.println("Your entered value: [" + parValue + "] is not a valid number!");
add a comment |
First you have to recognize if the entered value is a number and has at least 7 digits. Unless you have to output an error message. Convert the entered value to String and use the class Character.isDigit(); to check if the characters are numbers. Then you can use some methods from the String class like substring(..). At the end do a Unit-Test with erroneous/valid values to see if your code is robust. Close the BufferedReader and Resources when you are done by using finally br.close() . Push your code in methods and use an instance class erste-Aufgabe (first exercise).. When you are really really done use JFrame for a GUI-Application.
private static final int SUM_LAST_DIGITS = 7;
public void minimalSolution()
String enteredValue = "";
showInfoMessage("Please enter your number with at least " + SUM_LAST_DIGITS + " digits!");
try (Scanner scan = new Scanner(System.in))
enteredValue = scan.next();
if (enteredValue.matches("^[0-9]" + SUM_LAST_DIGITS + ",$"))
showInfoMessage(enteredValue, lastDigitsSum(enteredValue));
else
showErrorMessage(enteredValue);
catch(Exception e)
showErrorMessage(e.toString());
public int lastDigitsSum(String value)
int count = 0;
for (int i = value.length() - 1, j = 0; i >= 0 && j < SUM_LAST_DIGITS; i--, j++)
count += value.charAt(i) - '0';
return count;
public void showInfoMessage(String parMessage)
System.out.println(parMessage);
public void showInfoMessage(String parValue, int parSum)
System.out.println("Your entered value: [" + parValue + "]");
System.out.println("The summed value of the last 7 digits are: [" + parSum + "]");
public void showErrorMessage(String parValue)
System.err.println("Your entered value: [" + parValue + "] is not a valid number!");
First you have to recognize if the entered value is a number and has at least 7 digits. Unless you have to output an error message. Convert the entered value to String and use the class Character.isDigit(); to check if the characters are numbers. Then you can use some methods from the String class like substring(..). At the end do a Unit-Test with erroneous/valid values to see if your code is robust. Close the BufferedReader and Resources when you are done by using finally br.close() . Push your code in methods and use an instance class erste-Aufgabe (first exercise).. When you are really really done use JFrame for a GUI-Application.
private static final int SUM_LAST_DIGITS = 7;
public void minimalSolution()
String enteredValue = "";
showInfoMessage("Please enter your number with at least " + SUM_LAST_DIGITS + " digits!");
try (Scanner scan = new Scanner(System.in))
enteredValue = scan.next();
if (enteredValue.matches("^[0-9]" + SUM_LAST_DIGITS + ",$"))
showInfoMessage(enteredValue, lastDigitsSum(enteredValue));
else
showErrorMessage(enteredValue);
catch(Exception e)
showErrorMessage(e.toString());
public int lastDigitsSum(String value)
int count = 0;
for (int i = value.length() - 1, j = 0; i >= 0 && j < SUM_LAST_DIGITS; i--, j++)
count += value.charAt(i) - '0';
return count;
public void showInfoMessage(String parMessage)
System.out.println(parMessage);
public void showInfoMessage(String parValue, int parSum)
System.out.println("Your entered value: [" + parValue + "]");
System.out.println("The summed value of the last 7 digits are: [" + parSum + "]");
public void showErrorMessage(String parValue)
System.err.println("Your entered value: [" + parValue + "] is not a valid number!");
edited Nov 11 at 18:01
answered Nov 11 at 16:49
kaisel
1615
1615
add a comment |
add a comment |
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53250865%2fjava-sum-an-integer%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Please read "How to create a Minimal, Complete, and Verifiable example". Then use the edit link to improve your question (do not add more information via comments). Otherwise we are not able to answer your question and help you. "isnt working" ... isnt a working problem description when asking us for help.
– GhostCat
Nov 11 at 16:41
java.util.Scanner
– K.Nicholas
Nov 11 at 16:43
ok thanks, now i used the java util scanner
– Max
Nov 11 at 16:49
Your question says: I want to read a number. And yet you have a loop iterating 10 times, and asking a number at each iteration. So you're asking 10 numbers, not one. So that can't be right. Take a step back, and think about what you're really being asked to do.
– JB Nizet
Nov 11 at 16:50
I wanted only one number to be read and used as an array. Only now he expects 10 inputs from me.
– Max
Nov 11 at 16:51