Home > Blockchain >  Making user to input a fixed amount of characters
Making user to input a fixed amount of characters

Time:11-18

So basically, the program is a "sales tracker" and we must enter an id, but the id must be only 8 characters long and if it isn't 8 characters long then the program will tell the user so. I've got most of the program done but I cannot seem to get this part of the program done.

The user can also change the id in the database and the id still must be 8 characters long.

This is the full program


import java.util.*;

public class CreateSalesperson
{
   public static void main(String[] args)
   {
      Salesperson[] salespeople = new Salesperson[20];
      
      int x;
      
      int id;
      
      int count = 0;
      
      String name;
      
      double sales;
      
      final int QUIT = 999;
      
      char option;
      
      String message = "";
      Scanner input = new Scanner(System.in);
      
      System.out.print("Do you want to (A)dd, or (C)hange a record or (Q)uit >> ");
      option = input.nextLine().charAt(0);
      
      while(option != 'Q')
      {
         if(option == 'A')
            count = addOption(salespeople, count);
         else
                if(option == 'C')
                   changeOption(salespeople, count);
                else
                   System.out.println("Invalid entry");
         System.out.print("Do you want to (A)dd, or (C)hange a record or (Q)uit >> ");
         option = input.nextLine().charAt(0);
      }
   }
   
   public static int addOption(Salesperson[] array, int count)
   {
      Scanner input = new Scanner(System.in);
   
      int id;
      
      double sales;
      
      String name;
      
      boolean alreadyEntered;
      
      if(count == array.length)
         System.out.println("Sorry - array is full -- cannot add a record");
      else
      {
          System.out.print("Enter name of salesperson >> ");
          name = input.nextLine();

         while(id.length() != 8)
         {
          System.out.print("Enter salesperson ID >> ");
          id = input.nextInt();
         }
          alreadyEntered = false;
          
          for(int x = 0; x < count;   x)
             if(array[x].getId() == id)
             {
                System.out.println("Sorry -- ID number already exists");
                alreadyEntered = true;
             }
          if(!alreadyEntered)
          {
             System.out.print("Enter sales amount >> ");
             sales = input.nextDouble();

             array[count] = new Salesperson(id, sales, name);
               count;
          
          }
      }
      display(array, count);
      input.nextLine();
      return count;
   }
   
   public static void changeOption(Salesperson[] array, int count)
   {
 
      Scanner keyboard = new Scanner(System.in);

      int position = 0;
 
      int id;
      
      double sales;
      
      if(count == 0)
         System.out.println("Database is empty -- cannot change record");
      else
      {
         System.out.print("Enter ID to change >> ");
         id = keyboard.nextInt();
         boolean exists = false;
         
         for(int x = 0; x < count;   x)
            if(array[x].getId() == id)
            {
               exists = true;
               position = x;
            }
         if(!exists)
            System.out.println("Sorry - ID number #"   id   " does not exists in the database");
         else
         {
            System.out.print("Enter sales >> ");
            sales = keyboard.nextDouble();
            array[position].setSales(sales);
         }
         keyboard.nextLine();
         display(array, count);
      }
   }
    public static void display(Salesperson[] array, int count)
    {
       int a,b;
       Salesperson temp;
       int highSubscript = count  - 1;

       for(a = 0; a < highSubscript;   a)
          for(b = 0; b < highSubscript;   b)
          {
             if(array[b].getId() != array[b   1].getId())
               {
                  temp = array[b];
                  array[b] = array[b   1];
                  array[b   1] = temp;
               }
          }
       
       System.out.println("\nCurrent database:");
 
       for(a = 0; a < count;   a)
         System.out.println("NAME: "  array[a].getName()   "   ID: #"   array[a].getId()   "  Sales Amount: $"   array[a].getSales());
       System.out.println();
    }

}

I've tried to do a while loop with id.length != 8, but I keep getting an int cannot be dereferenced error. How can I fix this? Can I fix this? If not, what can I do to make this correct.

while(id.length() != 8)
         {
          System.out.print("Enter salesperson ID >> ");
          id = input.nextInt();
         }
          alreadyEntered = false;

CodePudding user response:

If the ID is Alphanumeric where is can be a mix of alpha and numeric characters then you can just use Scanner#nextLine() method instead of the Scanner#nextInt() method, for example:

String id = "";
    while (id.isEmpty()) {
        System.out.print("Enter salesperson ID >> ");
        id = input.nextLine().trim();
        
        // Input Validation:
        if (id.length() < 8) {
            System.out.println("Invalid ID Supplied ("   id   ")! ID must be 8 characters long!");
            id = "";
        }
    }
   
    // If we made it to here then the entry was good.
    System.out.println("The Salesman ID is: -> "   id);

If however, the Salesman ID is suppose to be an integer value entry then you can use the same code but the validation section would need to change, for example:

String id = "";
while (id.isEmpty()) {
    System.out.print("Enter salesperson ID >> ");
    id = input.nextLine().trim();
        
    /* Input Validation: Is the ID 8 characters long and 
       are all characters numerical digits?           */
    if (id.length() < 8 || !id.matches("\\d ")) {
        System.out.println("Invalid ID Supplied ("   id   ")! ID must be 8 Digits in length!");
        id = "";
    }
}
   
// If we made it to here then the entry was good.
    
// Convert the id string to an integer value
int salesmanID = Integer.parseInt(id);
    
System.out.println("The Salesman ID is: -> "   salesmanID);

CodePudding user response:

As "id" is int and it must be 8 digits, try changing while(id.length() != 8) to while(!(id >= 10000000 and id <= 99999999)

10000000 is smallest 8 digit number 99999999 is largest 8 digit number

  • Related