Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Exercise 9 - 1: Average*/
- public static void main(String[] args) {
- int[] anArray = {13, 20, 7, 25, 2, 14, 9, 100};
- double average, sum = 0;
- for(int i = 0; i < anArray.length; i++)
- {
- sum += anArray[i];
- }
- average = sum/anArray.length;
- System.out.println("Average value of the array elements is: " + average);
- }
- /*-------------------------------------------------------------------------------------------------------------------------*/
- /*Exercise 9 - 2: Odd number*/
- public static void main(String[] args) {
- int[] anArray = {184, 117, 81, 157, 209, 40, 58};
- double average, sum = 0;
- for(int i = 0; i < anArray.length; i++)
- {
- if(anArray[i] % 2 != 0)
- {
- System.out.println("odd number = " + anArray[i]);
- }
- }
- }
- /*-------------------------------------------------------------------------------------------------------------------------*/
- /*Exercise 9 - 3: Triangle*/
- public static void main(String[] args) {
- int length;
- Scanner sc = new Scanner(System.in);
- System.out.print("Please input the length of the triangle: ");
- length = sc.nextInt();
- int[][] triangle = new int[length][length];
- for(int height = 0; height <= triangle.length; height++)
- {
- for(int width = 0; width < height; width++)
- {
- System.out.print("*");
- System.out.print(width + ",");
- }
- System.out.println("");
- }
- }
- /*-------------------------------------------------------------------------------------------------------------------------*/
- /*Exercise 11 - 3: Generate random string*/
- public static void GenerateRandomString(int stringLength, String aString)
- {
- Random generate = new Random();
- StringBuilder rb = new StringBuilder();
- char c;
- for(int i = 0; i < stringLength; i++)
- {
- if(aString.length() < stringLength)
- {
- c = aString.charAt(generate.nextInt(aString.length()));
- }
- else
- {
- c = aString.charAt(generate.nextInt(stringLength));
- }
- rb.append(c);
- }
- System.out.println("Your string is: " + rb);
- }
- public static void main(String[] args) {
- int length;
- String randomString;
- Scanner sc = new Scanner(System.in);
- System.out.print("Input your string length: ");
- length = sc.nextInt();
- System.out.print("Chars to use for random string generation: ");
- randomString = sc.next();
- GenerateRandomString(length, randomString);
- }
- /*-------------------------------------------------------------------------------------------------------------------------*/
- /*Exercise 11 - 3: Generate random string*/
- public static void GenerateRandomString(int stringLength, String aString)
- {
- Random generate = new Random();
- StringBuilder rb = new StringBuilder();
- char c;
- for(int i = 0; i < stringLength; i++)
- {
- if(aString.length() < stringLength)
- {
- c = aString.charAt(generate.nextInt(aString.length()));
- }
- else
- {
- c = aString.charAt(generate.nextInt(stringLength));
- }
- rb.append(c);
- }
- System.out.println("Your string is: " + rb);
- }
- public static void main(String[] args) {
- int length;
- String randomString;
- Scanner sc = new Scanner(System.in);
- //Scanner sc1 = new Scanner(System.in);
- System.out.print("Input your string length: ");
- length = sc.nextInt();
- sc.nextLine();
- System.out.print("Chars to use for random string generation: ");
- randomString = sc.nextLine();
- if(randomString.isEmpty())
- {
- System.out.println("User default charSet a-z,A-z,0-9 to random String");
- randomString = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
- }
- GenerateRandomString(length, randomString);
- }
- /*-------------------------------------------------------------------------------------------------------------------------*/
- /*Exercise 11 - 4: Generate random string 2*/
- public static void GenerateRandomString(int stringLength, String aString)
- {
- Random generate = new Random();
- StringBuilder rb = new StringBuilder();
- char c;
- for(int i = 0; i < stringLength; i++)
- {
- if(aString.length() < stringLength)
- {
- c = aString.charAt(generate.nextInt(aString.length()));
- }
- else
- {
- c = aString.charAt(generate.nextInt(stringLength));
- }
- rb.append(c);
- }
- System.out.println("Your string is: " + rb);
- }
- public static void main(String[] args) {
- int length;
- String randomString;
- Scanner sc = new Scanner(System.in);
- //Scanner sc1 = new Scanner(System.in);
- System.out.print("Input your string length: ");
- length = sc.nextInt();
- sc.nextLine();
- System.out.print("Chars to use for random string generation: ");
- randomString = sc.nextLine();
- if(randomString.isEmpty())
- {
- System.out.println("User default charSet a-z,A-z,0-9 to random String");
- randomString = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
- }
- GenerateRandomString(length, randomString);
- }
- /*-------------------------------------------------------------------------------------------------------------------------*/
- /*Exercise 11 - 5: Choose random*/
- public static String chooseRandom(String[] stringArray)
- {
- Random r = new Random();
- int i = r.nextInt(stringArray.length);
- return stringArray[i];
- }
- public static void main(String[] args) {
- String[] aStringArray = {"Red", "Blue", "Orange", "Purple", "White", "Pink", "Yellow", "Colour"};
- System.out.println("Your item is: " + chooseRandom(aStringArray));
- }
- /*-------------------------------------------------------------------------------------------------------------------------*/
- /*Exercise 13 - 1: Verify username*/
- public static void CreateFile(String[] stringArray) throws IOException
- {
- String dir = System.getProperty("user.dir");
- File f = new File(dir + File.separator + "user.txt");
- f.createNewFile();
- FileWriter writer = new FileWriter(f);
- BufferedWriter buffer = new BufferedWriter(writer);
- PrintWriter printWriter = new PrintWriter(buffer);
- for(String s : stringArray) {
- printWriter.println(s);
- }
- printWriter.close();
- writer.close();
- buffer.close();
- }
- public static boolean CheckUsername(String aDir, String aName) throws IOException
- {
- FileReader fr = new FileReader(aDir);
- BufferedReader br = new BufferedReader(fr);
- String s;
- System.out.println("Checking if username exists...");
- while ((s = br.readLine()) != null) {
- if (s.equals(aName)) {
- return true;
- }
- }
- return false;
- }
- public static void main(String[] args) throws IOException {
- //Create a user text file
- String[] username = {"Ronda",
- "Stephan",
- "Illa",
- "Isabelle",
- "Nelson",
- "Juana",
- "Earlie",
- "Velda",
- "Linnie",
- "Mana",
- "Rosario",
- "Jolyn",
- "Merry",
- "Anika",
- "Carlotta",
- "Breann",
- "Jennine",
- "Larue",
- "Enola",
- "Pauline"};
- CreateFile(username);
- /*Exercise 13 - 1*/
- Scanner sc = new Scanner(System.in);
- String inputName;
- System.out.print("Enter your username: ");
- inputName = sc.next();
- String dir = System.getProperty("user.dir") + File.separator + "user.txt";
- if (CheckUsername(dir, inputName))
- {
- System.out.println("Username exists! Please try again.");
- }
- else
- {
- System.out.println("Username is accepted.");
- }
- }
- }
- /*-------------------------------------------------------------------------------------------------------------------------*/
- /*Exercise 13 - 2: Compare 2 files*/
- public static void main(String[] args) throws IOException {
- //Reader for compare1
- String dir1 = System.getProperty("user.dir") + File.separator + "compare1.txt";
- FileReader fr1 = new FileReader(dir1);
- BufferedReader br1 = new BufferedReader(fr1);
- String s1;
- //Reader for compare2
- String dir2 = System.getProperty("user.dir") + File.separator + "compare2.txt";
- FileReader fr2 = new FileReader(dir2);
- BufferedReader br2 = new BufferedReader(fr2);
- String s2;
- //Writer for compare
- File f = new File(System.getProperty("user.dir") + File.separator + "compare.txt");
- f.createNewFile();
- FileWriter writer = new FileWriter(f);
- BufferedWriter buffer = new BufferedWriter(writer);
- PrintWriter printWriter = new PrintWriter(buffer);
- //Loop through compare1 and compare2
- while ((s1 = br1.readLine()) != null)
- {
- while ((s2 = br2.readLine()) != null) {
- if(s1.equals(s2))
- {
- printWriter.println(s1);
- }
- }
- //Refresh buffer to the top of the file
- fr2 = new FileReader(dir2);
- br2 = new BufferedReader(fr2);
- }
- printWriter.close();
- buffer.close();
- writer.close();
- br1.close();
- br2.close();
- fr1.close();
- fr2.close();
- }
- /*-------------------------------------------------------------------------------------------------------------------------*/
- /*Exercise 13 - 3: Count number of lines in file*/
- public static void CreateFile(String[] stringArray) throws IOException
- {
- String dir = System.getProperty("user.dir");
- File f = new File(dir + File.separator + "text.txt");
- f.createNewFile();
- FileWriter writer = new FileWriter(f);
- BufferedWriter buffer = new BufferedWriter(writer);
- PrintWriter printWriter = new PrintWriter(buffer);
- for(String s : stringArray) {
- printWriter.println(s);
- }
- printWriter.close();
- writer.close();
- buffer.close();
- }
- public static void main(String[] args) throws IOException {
- //Create a text file
- String[] textFile = {"Ronda",
- "Stephan",
- "",
- "Illa",
- "Isabelle",
- "",
- "Nelson",
- "Juana",
- "Earlie",
- "Velda",
- "",
- "Linnie",
- "Mana",
- "Rosario",
- "Jolyn",
- "Merry",
- "",
- "Anika",
- "Carlotta",
- "Breann",
- "Jennine",
- "Larue",
- "",
- "Enola",
- "Pauline"};
- CreateFile(textFile);
- /*Exercise 13 - 3*/
- String dir = System.getProperty("user.dir") + File.separator + "text.txt";
- FileReader fr = new FileReader(dir);
- BufferedReader br = new BufferedReader(fr);
- String s;
- int numOfLine = 0;
- while ((s = br.readLine()) != null) {
- if(!s.equals(""))
- {
- numOfLine++;
- }
- }
- System.out.println("Number of lines is: " + numOfLine);
- }
- /*-------------------------------------------------------------------------------------------------------------------------*/
- /*Exercise 13 - 4: Remove all blank line in file*/
- public static void CreateFile(String[] stringArray) throws IOException
- {
- String dir = System.getProperty("user.dir");
- File f = new File(dir + File.separator + "text.txt");
- f.createNewFile();
- FileWriter writer = new FileWriter(f);
- BufferedWriter buffer = new BufferedWriter(writer);
- PrintWriter printWriter = new PrintWriter(buffer);
- for(String s : stringArray) {
- printWriter.println(s);
- }
- printWriter.close();
- writer.close();
- buffer.close();
- }
- public static void CreateFile(String aString) throws IOException
- {
- String dir = System.getProperty("user.dir");
- File f = new File(dir + File.separator + "text.txt");
- f.createNewFile();
- FileWriter writer = new FileWriter(f);
- BufferedWriter buffer = new BufferedWriter(writer);
- PrintWriter printWriter = new PrintWriter(buffer);
- printWriter.print(aString);
- printWriter.close();
- writer.close();
- buffer.close();
- }
- public static void main(String[] args) throws IOException {
- //Create a text file with blanks
- String[] textFile = {"Ronda",
- "Stephan",
- "",
- "Illa",
- "Isabelle",
- "",
- "Nelson",
- "Juana",
- "Earlie",
- "Velda",
- "",
- "Linnie",
- "Mana",
- "Rosario",
- "Jolyn",
- "Merry",
- "",
- "Anika",
- "Carlotta",
- "Breann",
- "Jennine",
- "Larue",
- "",
- "Enola",
- "Pauline"};
- CreateFile(textFile);
- /*Exercise 13 - 4*/
- String dir = System.getProperty("user.dir") + File.separator + "text.txt";
- FileReader fr = new FileReader(dir);
- BufferedReader br = new BufferedReader(fr);
- String s, separator = System.getProperty("line.separator");
- int numOfLine = 0;
- StringBuilder sb = new StringBuilder();
- while ((s = br.readLine()) != null) {
- if(!s.equals(""))
- {
- //This creates a line break at the end of string
- sb.append(s + separator);
- }
- else
- {
- numOfLine++;
- }
- }
- System.out.println("Number of blank lines is: " + numOfLine);
- String newString = sb.toString();
- //remove the line break at the end of string
- CreateFile(newString.substring(0, newString.length() - separator.length()));
- br.close();
- fr.close();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement