Advertisement
Guest User

Javascript

a guest
Jun 28th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     /*Exercise 9 - 1: Average*/
  2.     public static void main(String[] args) {
  3.         int[] anArray = {13, 20, 7, 25, 2, 14, 9, 100};
  4.         double average, sum = 0;
  5.  
  6.         for(int i = 0; i < anArray.length; i++)
  7.         {
  8.             sum += anArray[i];
  9.         }
  10.         average = sum/anArray.length;
  11.  
  12.         System.out.println("Average value of the array elements is: " + average);
  13.     }
  14.  
  15.  
  16. /*-------------------------------------------------------------------------------------------------------------------------*/
  17.  /*Exercise 9 - 2: Odd number*/
  18.     public static void main(String[] args) {
  19.         int[] anArray = {184, 117, 81, 157, 209, 40, 58};
  20.         double average, sum = 0;
  21.  
  22.         for(int i = 0; i < anArray.length; i++)
  23.         {
  24.             if(anArray[i] % 2 != 0)
  25.             {
  26.                 System.out.println("odd number = " + anArray[i]);
  27.             }
  28.         }
  29.     }
  30.  
  31.  
  32. /*-------------------------------------------------------------------------------------------------------------------------*/
  33.     /*Exercise 9 - 3: Triangle*/
  34.     public static void main(String[] args) {
  35.         int length;
  36.         Scanner sc = new Scanner(System.in);
  37.  
  38.         System.out.print("Please input the length of the triangle: ");
  39.         length = sc.nextInt();
  40.  
  41.         int[][] triangle = new int[length][length];
  42.         for(int height = 0; height <= triangle.length; height++)
  43.         {
  44.             for(int width = 0; width < height; width++)
  45.             {
  46.                 System.out.print("*");
  47.         System.out.print(width + ",");
  48.             }
  49.             System.out.println("");
  50.         }
  51.     }
  52.  
  53.  
  54. /*-------------------------------------------------------------------------------------------------------------------------*/
  55.  /*Exercise 11 - 3: Generate random string*/
  56.     public static void GenerateRandomString(int stringLength, String aString)
  57.     {
  58.         Random generate = new Random();
  59.         StringBuilder rb = new StringBuilder();
  60.         char c;
  61.  
  62.         for(int i = 0; i < stringLength; i++)
  63.         {
  64.             if(aString.length() < stringLength)
  65.             {
  66.                 c = aString.charAt(generate.nextInt(aString.length()));
  67.             }
  68.             else
  69.             {
  70.                 c = aString.charAt(generate.nextInt(stringLength));
  71.             }
  72.             rb.append(c);
  73.         }
  74.         System.out.println("Your string is: " + rb);
  75.     }
  76.  
  77.     public static void main(String[] args) {
  78.         int length;
  79.         String randomString;
  80.         Scanner sc = new Scanner(System.in);
  81.  
  82.         System.out.print("Input your string length: ");
  83.         length = sc.nextInt();
  84.  
  85.         System.out.print("Chars to use for random string generation: ");
  86.         randomString = sc.next();
  87.  
  88.         GenerateRandomString(length, randomString);
  89.     }
  90.  
  91.  
  92. /*-------------------------------------------------------------------------------------------------------------------------*/
  93.  /*Exercise 11 - 3: Generate random string*/
  94.     public static void GenerateRandomString(int stringLength, String aString)
  95.     {
  96.         Random generate = new Random();
  97.         StringBuilder rb = new StringBuilder();
  98.         char c;
  99.  
  100.         for(int i = 0; i < stringLength; i++)
  101.         {
  102.             if(aString.length() < stringLength)
  103.             {
  104.                 c = aString.charAt(generate.nextInt(aString.length()));
  105.             }
  106.             else
  107.             {
  108.                 c = aString.charAt(generate.nextInt(stringLength));
  109.             }
  110.             rb.append(c);
  111.         }
  112.         System.out.println("Your string is: " + rb);
  113.     }
  114.  
  115.     public static void main(String[] args) {
  116.         int length;
  117.         String randomString;
  118.         Scanner sc = new Scanner(System.in);
  119.         //Scanner sc1 = new Scanner(System.in);
  120.  
  121.         System.out.print("Input your string length: ");
  122.         length = sc.nextInt();
  123.         sc.nextLine();
  124.         System.out.print("Chars to use for random string generation: ");
  125.         randomString = sc.nextLine();
  126.  
  127.         if(randomString.isEmpty())
  128.         {
  129.             System.out.println("User default charSet a-z,A-z,0-9 to random String");
  130.             randomString = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  131.         }
  132.         GenerateRandomString(length, randomString);
  133.     }
  134.  
  135.  
  136.  
  137. /*-------------------------------------------------------------------------------------------------------------------------*/
  138.  /*Exercise 11 - 4: Generate random string 2*/
  139.     public static void GenerateRandomString(int stringLength, String aString)
  140.     {
  141.         Random generate = new Random();
  142.         StringBuilder rb = new StringBuilder();
  143.         char c;
  144.  
  145.         for(int i = 0; i < stringLength; i++)
  146.         {
  147.             if(aString.length() < stringLength)
  148.             {
  149.                 c = aString.charAt(generate.nextInt(aString.length()));
  150.             }
  151.             else
  152.             {
  153.                 c = aString.charAt(generate.nextInt(stringLength));
  154.             }
  155.             rb.append(c);
  156.         }
  157.         System.out.println("Your string is: " + rb);
  158.     }
  159.  
  160.     public static void main(String[] args) {
  161.         int length;
  162.         String randomString;
  163.         Scanner sc = new Scanner(System.in);
  164.         //Scanner sc1 = new Scanner(System.in);
  165.  
  166.         System.out.print("Input your string length: ");
  167.         length = sc.nextInt();
  168.         sc.nextLine();
  169.         System.out.print("Chars to use for random string generation: ");
  170.         randomString = sc.nextLine();
  171.  
  172.         if(randomString.isEmpty())
  173.         {
  174.             System.out.println("User default charSet a-z,A-z,0-9 to random String");
  175.             randomString = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  176.         }
  177.         GenerateRandomString(length, randomString);
  178.     }
  179.  
  180.  
  181. /*-------------------------------------------------------------------------------------------------------------------------*/
  182.    /*Exercise 11 - 5: Choose random*/
  183.     public static String chooseRandom(String[] stringArray)
  184.     {
  185.         Random r = new Random();
  186.         int i = r.nextInt(stringArray.length);
  187.  
  188.         return stringArray[i];
  189.     }
  190.  
  191.     public static void main(String[] args) {
  192.         String[] aStringArray = {"Red", "Blue", "Orange", "Purple", "White", "Pink", "Yellow", "Colour"};
  193.  
  194.         System.out.println("Your item is: " + chooseRandom(aStringArray));
  195.     }
  196.  
  197.  
  198.  
  199. /*-------------------------------------------------------------------------------------------------------------------------*/
  200.    /*Exercise 13 - 1: Verify username*/
  201.     public static void CreateFile(String[] stringArray) throws IOException
  202.     {
  203.         String dir = System.getProperty("user.dir");
  204.  
  205.         File f = new File(dir + File.separator + "user.txt");
  206.  
  207.         f.createNewFile();
  208.  
  209.         FileWriter writer = new FileWriter(f);
  210.         BufferedWriter buffer = new BufferedWriter(writer);
  211.         PrintWriter printWriter = new PrintWriter(buffer);
  212.  
  213.         for(String s : stringArray) {
  214.             printWriter.println(s);
  215.         }
  216.  
  217.         printWriter.close();
  218.         writer.close();
  219.         buffer.close();
  220.     }
  221.  
  222.     public static boolean CheckUsername(String aDir, String aName) throws IOException
  223.     {
  224.         FileReader fr = new FileReader(aDir);
  225.         BufferedReader br = new BufferedReader(fr);
  226.         String s;
  227.  
  228.         System.out.println("Checking if username exists...");
  229.         while ((s = br.readLine()) != null) {
  230.             if (s.equals(aName)) {
  231.                 return true;
  232.             }
  233.         }
  234.         return false;
  235.     }
  236.  
  237.  
  238.     public static void main(String[] args) throws IOException {
  239.         //Create a user text file
  240.         String[] username = {"Ronda",
  241.                 "Stephan",
  242.                 "Illa",
  243.                 "Isabelle",
  244.                 "Nelson",
  245.                 "Juana",
  246.                 "Earlie",
  247.                 "Velda",
  248.                 "Linnie",
  249.                 "Mana",
  250.                 "Rosario",
  251.                 "Jolyn",
  252.                 "Merry",
  253.                 "Anika",
  254.                 "Carlotta",
  255.                 "Breann",
  256.                 "Jennine",
  257.                 "Larue",
  258.                 "Enola",
  259.                 "Pauline"};
  260.  
  261.         CreateFile(username);
  262.  
  263.         /*Exercise 13 - 1*/
  264.         Scanner sc = new Scanner(System.in);
  265.         String inputName;
  266.  
  267.         System.out.print("Enter your username: ");
  268.         inputName = sc.next();
  269.  
  270.         String dir = System.getProperty("user.dir") + File.separator + "user.txt";
  271.         if (CheckUsername(dir, inputName))
  272.         {
  273.             System.out.println("Username exists! Please try again.");
  274.         }
  275.         else
  276.         {
  277.             System.out.println("Username is accepted.");
  278.         }
  279.     }
  280. }
  281.  
  282.  
  283.  
  284. /*-------------------------------------------------------------------------------------------------------------------------*/
  285.   /*Exercise 13 - 2: Compare 2 files*/
  286.     public static void main(String[] args) throws IOException {
  287.         //Reader for compare1
  288.         String dir1 = System.getProperty("user.dir") + File.separator + "compare1.txt";
  289.         FileReader fr1 = new FileReader(dir1);
  290.         BufferedReader br1 = new BufferedReader(fr1);
  291.         String s1;
  292.  
  293.         //Reader for compare2
  294.         String dir2 = System.getProperty("user.dir") + File.separator + "compare2.txt";
  295.         FileReader fr2 = new FileReader(dir2);
  296.         BufferedReader br2 = new BufferedReader(fr2);
  297.         String s2;
  298.  
  299.         //Writer for compare
  300.         File f = new File(System.getProperty("user.dir") + File.separator + "compare.txt");
  301.         f.createNewFile();
  302.         FileWriter writer = new FileWriter(f);
  303.         BufferedWriter buffer = new BufferedWriter(writer);
  304.         PrintWriter printWriter = new PrintWriter(buffer);
  305.  
  306.         //Loop through compare1 and compare2
  307.         while ((s1 = br1.readLine()) != null)
  308.         {
  309.             while ((s2 = br2.readLine()) != null) {
  310.                 if(s1.equals(s2))
  311.                 {
  312.                     printWriter.println(s1);
  313.                 }
  314.             }
  315.             //Refresh buffer to the top of the file
  316.             fr2 = new FileReader(dir2);
  317.             br2 = new BufferedReader(fr2);
  318.         }
  319.         printWriter.close();
  320.         buffer.close();
  321.         writer.close();
  322.         br1.close();
  323.         br2.close();
  324.         fr1.close();
  325.         fr2.close();
  326.     }
  327.  
  328.  
  329. /*-------------------------------------------------------------------------------------------------------------------------*/
  330.   /*Exercise 13 - 3: Count number of lines in file*/
  331.     public static void CreateFile(String[] stringArray) throws IOException
  332.     {
  333.         String dir = System.getProperty("user.dir");
  334.  
  335.         File f = new File(dir + File.separator + "text.txt");
  336.  
  337.         f.createNewFile();
  338.  
  339.  
  340.         FileWriter writer = new FileWriter(f);
  341.         BufferedWriter buffer = new BufferedWriter(writer);
  342.         PrintWriter printWriter = new PrintWriter(buffer);
  343.  
  344.         for(String s : stringArray) {
  345.             printWriter.println(s);
  346.         }
  347.  
  348.         printWriter.close();
  349.         writer.close();
  350.         buffer.close();
  351.     }
  352.  
  353.     public static void main(String[] args) throws IOException {
  354.         //Create a text file
  355.         String[] textFile = {"Ronda",
  356.                 "Stephan",
  357.                 "",
  358.                 "Illa",
  359.                 "Isabelle",
  360.                 "",
  361.                 "Nelson",
  362.                 "Juana",
  363.                 "Earlie",
  364.                 "Velda",
  365.                 "",
  366.                 "Linnie",
  367.                 "Mana",
  368.                 "Rosario",
  369.                 "Jolyn",
  370.                 "Merry",
  371.                 "",
  372.                 "Anika",
  373.                 "Carlotta",
  374.                 "Breann",
  375.                 "Jennine",
  376.                 "Larue",
  377.                 "",
  378.                 "Enola",
  379.                 "Pauline"};
  380.  
  381.         CreateFile(textFile);
  382.  
  383.         /*Exercise 13 - 3*/
  384.         String dir = System.getProperty("user.dir") + File.separator + "text.txt";
  385.         FileReader fr = new FileReader(dir);
  386.         BufferedReader br = new BufferedReader(fr);
  387.         String s;
  388.         int numOfLine = 0;
  389.  
  390.         while ((s = br.readLine()) != null) {
  391.             if(!s.equals(""))
  392.             {
  393.                 numOfLine++;
  394.             }
  395.         }
  396.  
  397.         System.out.println("Number of lines is: " + numOfLine);
  398.     }
  399.  
  400.  
  401.  
  402. /*-------------------------------------------------------------------------------------------------------------------------*/
  403.     /*Exercise 13 - 4: Remove all blank line in file*/
  404.     public static void CreateFile(String[] stringArray) throws IOException
  405.     {
  406.         String dir = System.getProperty("user.dir");
  407.  
  408.         File f = new File(dir + File.separator + "text.txt");
  409.  
  410.         f.createNewFile();
  411.  
  412.  
  413.         FileWriter writer = new FileWriter(f);
  414.         BufferedWriter buffer = new BufferedWriter(writer);
  415.         PrintWriter printWriter = new PrintWriter(buffer);
  416.  
  417.         for(String s : stringArray) {
  418.             printWriter.println(s);
  419.         }
  420.  
  421.         printWriter.close();
  422.         writer.close();
  423.         buffer.close();
  424.     }
  425.  
  426.     public static void CreateFile(String aString) throws IOException
  427.     {
  428.         String dir = System.getProperty("user.dir");
  429.  
  430.         File f = new File(dir + File.separator + "text.txt");
  431.  
  432.         f.createNewFile();
  433.  
  434.  
  435.         FileWriter writer = new FileWriter(f);
  436.         BufferedWriter buffer = new BufferedWriter(writer);
  437.         PrintWriter printWriter = new PrintWriter(buffer);
  438.  
  439.         printWriter.print(aString);
  440.  
  441.         printWriter.close();
  442.         writer.close();
  443.         buffer.close();
  444.     }
  445.  
  446.     public static void main(String[] args) throws IOException {
  447.         //Create a text file with blanks
  448.         String[] textFile = {"Ronda",
  449.                 "Stephan",
  450.                 "",
  451.                 "Illa",
  452.                 "Isabelle",
  453.                 "",
  454.                 "Nelson",
  455.                 "Juana",
  456.                 "Earlie",
  457.                 "Velda",
  458.                 "",
  459.                 "Linnie",
  460.                 "Mana",
  461.                 "Rosario",
  462.                 "Jolyn",
  463.                 "Merry",
  464.                 "",
  465.                 "Anika",
  466.                 "Carlotta",
  467.                 "Breann",
  468.                 "Jennine",
  469.                 "Larue",
  470.                 "",
  471.                 "Enola",
  472.                 "Pauline"};
  473.  
  474.         CreateFile(textFile);
  475.  
  476.         /*Exercise 13 - 4*/
  477.         String dir = System.getProperty("user.dir") + File.separator + "text.txt";
  478.         FileReader fr = new FileReader(dir);
  479.         BufferedReader br = new BufferedReader(fr);
  480.         String s, separator = System.getProperty("line.separator");
  481.         int numOfLine = 0;
  482.  
  483.         StringBuilder sb = new StringBuilder();
  484.  
  485.  
  486.         while ((s = br.readLine()) != null) {
  487.             if(!s.equals(""))
  488.             {
  489.                 //This creates a line break at the end of string
  490.                 sb.append(s + separator);
  491.             }
  492.             else
  493.             {
  494.                 numOfLine++;
  495.             }
  496.         }
  497.  
  498.         System.out.println("Number of blank lines is: " + numOfLine);
  499.         String newString = sb.toString();
  500.         //remove the line break at the end of string
  501.         CreateFile(newString.substring(0, newString.length() - separator.length()));
  502.         br.close();
  503.         fr.close();
  504.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement