Program 1

Use the checklist to mark the code below. Ask your instructor if you need to use this sheet as an answer sheet to complete other questions in the lab assignment.

import java.util.*;

public class Input {
    public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);

    int sz = getArraySize(scan);
    String[] names = getNames(scan,sz);

    int which = getWhich(scan);
    String aName = getName(which,names);

    System.out.println("You choose name: "+aName);
  }

  public static int getArraySize(Scanner scan) {
    System.out.print("How many names? ");
    int n =  scan.nextInt();
    scan.nextLine();
    return n;
  }

  public static String[] getNames(Scanner scan, int sz) {
    String[] names = new String[sz];
    for (int i = 0; i < sz; i++ ){
      System.out.print("type name # "+(i+1)+": ");
      names[i] = scan.nextLine();
    }
    return names;
  }

  public static int getWhich(Scanner scan) {
    System.out.print("Which name: ");
    int x = scan.nextInt();
    return x;
  }

  public static String getName(int n,String[] vals) {
      return vals[n-1];
  }
}
 

Security Checklist

Vulnerability:Input Validation Course: CS2
Task – Check each line of code Completed
1. Mark each variable that receives external input with a V
For each statement that is marked with a V, verify that the variable is checked for each of these criteria. Note any that is not checked for:
1. Length
2 Range (reasonableness?)
3. Format
4. Type
Shaded areas indicate vulnerabilities!



 
Copyright © Towson University