I\'m working on writing a program that reads an int from a scanner and then pass
ID: 3623158 • Letter: I
Question
I'm working on writing a program that reads an int from a scanner and then passes that int to another method within the program. Unfortunately, I'm not exactly sure how to do it. The program itself is supposed to accept 5 different ints no bigger than six digits and run some calculations to create a separate int and post that to the front of the int read from the scanner.The code that I have so far is as follows:
import java.util.Scanner;
public class Homework5 {
/**
* @param args
*/
public static void main(String[] args) {
Scanner inputReader = new Scanner(System.in);
System.out.println("I am going to ask you for 5 ints");
int reader = inputReader.nextInt();
for (int i=0; i<5; i++){
System.out.print("Give me an int: ");
reader.addCheckDigit(); //how exactly do I call this?
//Send the int to your addCheckDigit method and print out its return.
}
}//end of main method
public static int addCheckDigit(int abc){
int intLength = String.valueOf(abc).trim().length();
if (intLength <= 6){
int digit1 = (abc/1)%10;
int digit2 = (abc/10)%10;
int digit3 = (abc/100)%10;
int digit4 = (abc/1000)%10;
int digit5 = (abc/10000)%10;
int digit6 = (abc/100000)%10;
int digit7 = (1 * digit1 + 2 * digit2 + 3 * digit3 + 4 * digit4 + 5 * digit5 + 6 * digit6) % 10;
int combinedDigits = digit7 + digit1 + digit2 + digit3 + digit4 + digit5 + digit6; //not sure how to combine them all without actually using addition
return combinedDigits;
}else{
IllegalArgumentException tooBig = new IllegalArgumentException("Too many integers, keep it within 6 digits please!");
throw tooBig;
}
}//end of method
}//end of program
Explanation / Answer
It should be System.out.println("Give me an int: "); reader = addCheckDigit(reader); and not reader.addCheckDigit(); //incorrect because reader is not an object but of type int. The function takes an integer value. So the input from user must be sent to addCheckDigit function.