StefanTobler

Lesson 14 Activity 3

Oct 1st, 2016
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.29 KB | None | 0 0
  1. /*
  2.  * Lesson 14 Coding Activity 3
  3.  * The Internet runs on web addresses.The addresses we type represent the IP address
  4.  * for each site and how the computer finds an individual web page.
  5.  *
  6.  * IP addresses are made up of four numbers, each between 0 and 255 separated by a period.
  7.  * For example, 128.253.21.58 is an IP address.
  8.  *
  9.  * Write a program to enter four numbers and test if they make up a valid IP address.
  10.  * In other words, test to see if the numbers entered are between 0 and 255 inclusive.
  11.  *
  12.  *     Sample Run 1
  13.  *         Please enter the first octet:
  14.  *         898
  15.  *         Please enter the second octet:
  16.  *         34
  17.  *         Please enter the third octet:
  18.  *         712
  19.  *         Please enter the fourth octet:
  20.  *         45
  21.  *         Octet 1 is incorrect
  22.  *         Octet 3 is incorrect
  23.  *
  24.  *        
  25.  *      Sample Run 2
  26.  *         Please enter the first octet:
  27.  *         112
  28.  *         Please enter the second octet:
  29.  *         200
  30.  *         Please enter the third octet:
  31.  *         0
  32.  *         Please enter the fourth octet:
  33.  *         254
  34.  *         IP Address: 112.200.0.254
  35.  *
  36.  */
  37.  
  38.  
  39. import java.util.Scanner;
  40.  
  41. class Lesson_14_Activity_Three {
  42.     public static void main(String[] args)
  43.      {
  44.       Scanner scan = new Scanner(System.in);
  45.       System.out.println("Please enter the first octet:");
  46.       int a = scan.nextInt();
  47.       System.out.println("Please enter the second octet:");
  48.       int b = scan.nextInt();
  49.       System.out.println("Please enter the third octet:");
  50.       int c = scan.nextInt();
  51.       System.out.println("Please enter the fourth octet:");
  52.       int d = scan.nextInt();
  53.       boolean ip = true;
  54.       if (!(a >= 0 && a <= 255))
  55.       {
  56.         System.out.println("Octet 1 is incorrect");
  57.         ip = false;
  58.       }
  59.        if (!(b >= 0 && b <= 255))
  60.       {
  61.         System.out.println("Octet 2 is incorrect");
  62.         ip = false;
  63.       }
  64.         if (!(c >= 0 && c <= 255))
  65.       {
  66.         System.out.println("Octet 3 is incorrect");
  67.         ip = false;
  68.       }
  69.          if (!(d >= 0 && d <= 255))
  70.       {
  71.         System.out.println("Octet 4 is incorrect");
  72.         ip = false;
  73.       }
  74.          if (ip == true)
  75.            System.out.println("IP Address: " + a + "." + b + "." + c + "." + d);
  76.     }
  77. }
Add Comment
Please, Sign In to add comment