Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Enter any number of integer values in a single row separated by space and the calculate and print the sum to next line.
EX: input: 1 2 3 4
output: 10
This should work - console.hasNext uses whitespace as its delimiter.
Scanner console = new Scanner(System.in);
int sum = 0;
while (console.hasNext()) {
sum += console.nextInt();
}
System.out.print(sum);
You can read the whole line as a String and than split it into a String array and than you can add it one by one for this purpose as following:
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s[] = br.readLine().split(" ");
int sum=0;
for(int i=0;i<s.length;i++){
sum+=Integer.parseInt(s[i]);
}
System.out.println(sum);
for this you have to import :
import java.io.BufferedReader;
import java.io.InputStreamReader;
This is faster than Scanner, see this question for more details about Scanner and BufferedReader.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Here is the code I have.
//input
System.out.println("Using numbers, in what month were you born? ");
String **userMonth** = input.nextLine();
// also tried int **userMonth** = input.nextInt();
It does not work either way.
userMonth will not "activate?" (sorry noob and don't know all terms)
When tried to call later in program error:
if (userMonth < 3) {
System.out.println("Your vacation home is a van down by a river!");
}
//userMonth cannot be resolved to variable
Very odd that your variables are showing up as **varName** instead of varName.
First use:
int userMonth = 0; // initialize variable.
userMonth = input.nextInt();
But it does not consume the newline ('ENTER').
Therefore, immediately after, consume the newline character via:
input.nextLine();
Also make sure Scanner was declared as
Scanner input = new Scanner(System.in);
Don't forget to close the scanner at the end of program
input.close();
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Example we input 20182391, how to just calculate the last digit of number? like "add the last digit with 2"
Thank you
Scanner sc = new Scanner(System.in);
int input = sc.nextInt();
int lastDigit = input % 10;
//Do whatever you want to.
public static void main(String[] args){
Integer input = 20182391;
System.out.println(input % 10 + 2);
}
This should work
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
How to write a java program to get a string as a key board input. Then count the number of letters, digits and spaces available in that string and display the count of letters, digits and spaces.
This is honestly... really, really easy and something that really should be taught to you in an introductory course or something.
Scanner scan = new Scanner(System.in);
String userInput = scan.nextLine();
You will need to put, right after package, import java.util.Scanner. To do the other thing, i.e. counting the number of characters in the String, simply invoke length().
Pass a string as an argument in this method you get count of letter, space, and digit
public void count(String str) {
int space = 0;
int digit = 0;
int letter =0;
for (int f4 = 0; f4 < str.length(); f4++) {
if (Character.isSpace(str.charAt(f4)))
space++;
if(Character.isAlphabetic(str.charAt(f4)))
letter++;
if(Character.isDigit(str.charAt(f4)))
digit++;
}
System.out.println("space = "+space+", digit = "+digit+", letter = "+letter);
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am trying to validate input values to pass only integers that are divisible by 10. The code below is failing.
public static void main(String args[]) {
Scanner scan =new Scanner(System.in);
ArrayList<Integer> liste = new ArrayList<Integer>(); // I have filled my array with integers
int x=scan.nextInt();
int y=x%10;
do{
if(y==0){
liste.add(x);}
else if(y!=0){
System.out.println("It is not valid"); continue;
}
else
{System.out.println("Enter only integer"); continue;
}
}while(scan.hasNextInt()); }
System.out.println(liste);
System.out.println("Your largest value of your arraylist is: "+max(liste));
You're calling scan.nextInt() twice. Each time you call it, it will read another int from the input. Thus, if your input was something like
10
5
13
then the 10 would pass the scan.nextInt()%10==0 check, and then 5 would be added to the list. Store the result of scan.nextInt() in a variable first, so the value won't change.
Instead of
if(scan.nextInt()%10==0){
liste.add(scan.nextInt());}
do
int num = scan.nextInt();
if(num%10 == 0){
liste.add(num);
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
i have been working on a project that would scan a textfile line per line and at each line, each word in the line will be stored in an array
here is my code as of now. when it comes to the storing from the Answer.txt file, an error occurs. can someone please help me?
try
{
String s = sc.nextLine();
//System.out.println(s);
String[] Question = s.split(" ");
for(int i=0;i<=Question.length;i++)
{
System.out.println(Question[i]);
}//debug
s = sc2.nextLine();
//System.out.println(s2);
String[] Answer = s.split(" ");
for(int c=0;c<=Answer.length;c++)
{
System.out.println(Answer[c]);
}//debug
}
catch (ArrayIndexOutOfBoundsException e)
{
System.out.println("...");
}
You're probably getting ArrayIndexOutOfBounds exception.
for(int i=0;i<=Question.length;i++)
Should be:
for(int i=0;i<Question.length;i++)
^
(The same for the other loop).
Why?
Remember that arrays are zero-based in Java. So if you have an array of size N, the indexes will be from 0 to N - 1 (total sum of N).
You can avoid the index counting all together with an "foreach" loop.
for (String s: Question){
System.out.println(s);
}