4

I'm having trouble with this beginner java program that was assigned to me, I'm completely new to java and I'm having a lot of trouble with this particular program. These are the instructions:

Your program should prompt users to enter an n-digit number as a student ID, and then display the validity of the entered ID. You can assume n will be in the range of 6 and 10 inclusive. The checksum digit is part of student ID. Your algorithm is to match the rightmost digit in the entered ID with the computed checksum digit. If there is no match, then your program shall report the entered ID being invalid. For example, the entered ID 1234567 is invalid because the computed 7th digit is 1 (= (1 * (digit1) + 2 * (digit 2) + 3 * (digit 3) + 4 * (digit 4) + 5 * (digit 5) + 6 * (digit 6)) % 10) and is different from the actual 7th digit which is 7. However, if the entered ID is 1234561 your program shall display a message of acceptance.

The first step I'm trying to do is to read each number from the user's input that doesn't have spaces, as if it were to have spaces. Then I'm trying to get each of those numbers assigned to the variable that is digit 1, digit 2, ... etc. to compute.

import java.util.Scanner;
public class H4_Ruby{
public static void main(String[] args){
// this code scans the the user input 
  Scanner scan = new Scanner(System.in);
  System.out.println("Please enter a student ID between 6-10 digits");
  String nums = scan.nextLine(); 
  String[] studentIdArray = nums.split(" ");
  int[] studentID = new int [studentIdArray.length];



  for (int i = 0; i < studentIdArray.length; i++) 
  { 
     studentID[i]= Integer.parseInt(studentIdArray[i]);
     System.out.print(studentID[i] + ",");
  }
}

That is my code so far...

Raman Sahasi
  • 30,180
  • 9
  • 58
  • 71
Quin
  • 47
  • 1
  • 7
  • I'm incredibly confused by the whole spaces thing. Does the number they enter have spaces? The description given by your professor seems to indicate it is just a series of numbers without spaces. Is that accurate? – DJKnarnia Jul 09 '18 at 03:04
  • It would be an input from the user with no spaces – Quin Jul 09 '18 at 03:06
  • Okay, that makes a bit more sense. I think I have an answer. I just want to think through it in my head before I post it. – DJKnarnia Jul 09 '18 at 03:08
  • I may be completely wrong, but I feel the most efficent way to complete this program would be to split the "student ID" into seperate numbers, set each of those numbers into a variable, then plug those into the equation – Quin Jul 09 '18 at 03:09

2 Answers2

2

You can't split by nums.split(" ") to get a String array. You already got a String Id from user without spaces.

So iterate through the String and keep calculating the sum of products with multiplier like below. I've added comments in the code itself.

public static void main(String[] args) {
    // this code scans the the user input
    Scanner scan = new Scanner(System.in);
    System.out.println("Please enter a student ID between 6-10 digits");
    String nums = scan.nextLine();

    int multiplier = 1, totalProductSum = 0;
    for (int i = 0; i < nums.length() - 1; i++) {

        // if the character is not digit, its not a valid id
        if (!Character.isDigit(nums.charAt(i))) {
            System.out.println("Invaild Id");
            return;
        }

        // maintain the sum of products and increment the multiplier everytime
        totalProductSum += (multiplier * (nums.charAt(i) - '0'));
        multiplier++;
    }

    // the condition for validity i.e totalProduct sum mod 10 and last digit in input Id
    if ((totalProductSum % 10) == (nums.charAt(nums.length() - 1) - '0'))
        System.out.println("Valid Id");
    else
        System.out.println("Invaild Id");
}
Shanu Gupta
  • 3,699
  • 2
  • 19
  • 29
0

Here, you can use this one also:

final class H4_Ruby {
  public static void main(final String... args) {
    System.out.printf("%s student ID%n", (H4_Ruby.isValid(H4_Ruby.read())) ? "Valid" : "Invalid");
  }

  private static String read() {
    final Scanner scan = new Scanner(System.in);
    // [start] do-while here for input validation: length and all characters must be digits
    System.out.print("Please, enter a student ID between 6-10 digits: ");
    final String input = scan.nextLine();
    // [end] do-while here for input validation: length and all characters must be digits
    return input;
  }

  private static boolean isValid(final CharSequence studentId) {
    int partial = 0;
    for (int i = 0; i < (studentId.length() - 1); i++) {
      partial += ((i + 1) * Character.getNumericValue(studentId.charAt(i)));
    }
    return (partial % 10) == Character.getNumericValue(studentId.charAt(studentId.length() - 1));
  }
}
x80486
  • 6,627
  • 5
  • 52
  • 111