How to Read a File Line by Line in Java

If you need to read a file line by line in Java, then you can use one of the following three (3) options. Option 1 You can use the FileReader and BufferedReader packages as follows: 1 2 3 4 5 6 7 8 9 10 11 File file = new File("./your/file.txt"); try (FileReader fr = new FileReader(file); BufferedReader br = new BufferedReader(fr);) { String line; while ((line = br.readLine()) != null) { System.

How can I clear or empty a StringBuilder

You can use Java’s StringBuilder to create and manipulate Strings as follows: 1 2 3 4 5 StringBuilder sb = new StringBuilder(); sb.append("Some").append(" ").append("String"); System.out.println(sb.toString()); // "Some String" The following three (3) options allow you to clear or empty a StringBuilder. Option 1 – using setLength 1 sb.setLength(0); Option 2 – reinstantiating 1 sb = new StringBuilder(); Option 3 – using delete 1 sb.delete(0, sb.length());

How to Approve a SageMaker model in AWS CLI

Sometimes you will need to manually approve a SageMaker model package from the AWS CLI. Step 1 – Get a list of the available packages 1 aws sagemaker list-model-packages --model-package-group-name "the-model-package-group" This will produce the following output: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 { "ModelPackageSummaryList": [ { "ModelPackageGroupName": "...", "ModelPackageVersion": "...", "ModelPackageArn": "the-arn-we-will-use-below", "ModelPackageDescription": "...", "CreationTime": "...", "ModelPackageStatus": "Completed", "ModelApprovalStatus": "PendingManualApproval" }, .

How to Assume Role across Accounts in AWS

If you need to assume role between AWS accounts, or allow an account to assume a role and use resources in another AWS account, then you need to create a role and attach the following policy. The following two (2) steps creates a Trust Relationship between the accounts. Step 1 – In the Source Account 1 2 3 4 5 6 7 8 9 10 11 12 { "Version": "2012-10-17", "Statement": [{ "Effect": "Allow", "Action": [ "sts:AssumeRole" ], "Resource": [ "arn:aws:iam::DESTINATION-ACCOUNT-ID:role/DESTINATION-ROLENAME" ] }] } Step 2 – In the Destination Account 1 2 3 4 5 6 7 8 9 10 { "Version": "2012-10-17", "Statement": [{ "Effect": "Allow", "Principal": { "AWS": "arn:aws:iam::SOURCE-ACCOUNT-ID:role/SOURCE-USERNAME" }, "Action": "sts:AssumeRole" }] }

How to Style an Element using Javascript

If you need to style an element using Javascript then you can use the style object to support all your CSS needs. 1 2 3 4 5 6 7 8 9 10 11 12 13 <html> <body> <p id="p1">Hello World!</p> <script> document.getElementById("p1").style.color = "red"; </script> <p>The paragraph above was changed by a script.</p> </body> </html> If you need to do this purely in a Javascript file itself, then: 1 2 3 4 5 6 // Get a reference to the element var myElement = document.

How to Wait 1 Second in Javascript

If you need your Javascript code to wait one (1) second (or more) while executing, then there are a couple of ways to achieve this. Option 1 – Creating a delay Promise 1 2 3 4 5 function delay(time) { return new Promise(resolve => setTimeout(resolve, time)); } delay(1000).then(() => console.log('ran after 1 second elapsed')); Option 2 – Using setTimeout 1 2 3 setTimeout(function(){ console.log("Ready") }, 1000); Option 3 – Using an async Promise 1 2 3 4 5 6 7 async function test() { console.

How to use forEach method in Javascript

Arrays come with a useful forEach function that allows you to loop through the array. 1 2 3 4 5 6 7 8 9 var colors = ['red', 'blue', 'green']; colors.forEach(function(color) { console.log(color); }); // red // blue // green You can also get the index in each loop as follows: 1 2 3 4 5 6 7 8 9 const colors = ['red', 'blue', 'green']; colors.forEach((item, index)=>{ console.log(index, item) }); // 0 'red' // 1 'blue' // 2 'green'

How to Remove an Element from an Array in Javascript

If you need to remove an element from an array in Javascript, then you can use one of the following five (5) options: Option 1 – Use splice to remove an element Example 1 using splice: 1 2 3 4 5 var colors = ["red","blue","car","green"]; var carIndex = colors.indexOf("car"); colors.splice(carIndex, 1); // colors = ["red","blue","green"] Example 2 using splice: 1 2 3 4 var myArray = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]; // Remove Sunday -- index 0 and Monday -- index 1 myArray.

How to get the Alphabet as Array in Javascript

If you need to get an array of alphabetical letters in Javascript then you can use one of the following: Option 1 – Explicitly define the array first 1 const alphabet = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]; Option 2 – Split a string of alphabetical characters 1 const alphabetArray = "abcdefghijklmnopqrstuvwxyz".split(""); Option 3 – Split a string and UpperCase characters 1 const alphabetArrayUp = toUpperCase("abcdefghijklmnopqrstuvwxyz").split("");

The Power of Two (2) Table

With the Power of Two, you can ask the following: How do you find the powers of 2? What does the power of 2 stand for? How much is the power of 2? The Power of Two (2) Table (Powers of Two) Power <td> <strong>Value</strong> </td> <td> 1 </td> 1 <td> 2 </td> 2 <td> 4 </td> 3 <td> 8 </td> 4 <td> 16 </td> 5 <td> 32 </td> 6 <td> 64 </td> 7 <td> 128 </td> 8 <td> 256 </td> 9 <td> 512 </td> 10 <td> 1,024 </td> 11 <td> 2,048 </td> 12 <td> 4,096 </td> 13 <td> 8,192 </td> 14 <td> 16,384 </td> 15 <td> 32,768 </td> 16 <td> 65,536 </td> 17 <td> 131,072 </td> 18 <td> 262,144 </td> 19 <td> 524,288 </td> 20 <td> 1,048,576 </td> 21 <td> 2,097,152 </td> 22 <td> 4,194,304 </td> 23 <td> 8,388,608 </td> 24 <td> 16,777,216 </td> 25 <td> 33,554,432 </td> 26 <td> 67,108,864 </td> 27 <td> 134,217,728 </td> 28 <td> 268,435,456 </td> 29 <td> 536,870,912 </td> 30 <td> 1,073,741,824 </td> 31 <td> 2,147,483,648 </td> 32 <td> 4,294,967,296 </td> 33 <td> 8,589,934,592 </td> 34 <td> 17,179,869,184 </td> 35 <td> 34,359,738,368 </td> 36 <td> 68,719,476,736 </td> 37 <td> 137,438,953,472 </td> 38 <td> 274,877,906,944 </td> 39 <td> 549,755,813,888 </td> 40 <td> 1,099,511,627,776 </td> 41 <td> 2,199,023,255,552 </td> 42 <td> 4,398,046,511,104 </td> 43 <td> 8,796,093,022,208 </td> 44 <td> 17,592,186,044,416 </td> 45 <td> 35,184,372,088,832 </td> 46 <td> 70,368,744,177,664 </td> 47 <td> 140,737,488,355,328 </td> 48 <td> 281,474,976,710,656 </td> 49 <td> 562,949,953,421,312 </td> 50 <td> 1,125,899,906,842,624 </td> 51 <td> 2,251,799,813,685,248 </td> 52 <td> 4,503,599,627,370,496 </td> 53 <td> 9,007,199,254,740,992 </td> 54 <td> 18,014,398,509,481,984 </td> 55 <td> 36,028,797,018,963,968 </td> 56 <td> 72,057,594,037,927,936 </td> 57 <td> 144,115,188,075,855,872 </td> 58 <td> 288,230,376,151,711,744 </td> 59 <td> 576,460,752,303,423,488 </td> 60 <td> 1,152,921,504,606,846,976 </td> 61 <td> 2,305,843,009,213,693,952 </td> 62 <td> 4,611,686,018,427,387,904 </td> 63 <td> 9,223,372,036,854,775,808 </td> 64 <td> 18,446,744,073,709,551,616 </td> 65 <td> 36,893,488,147,419,103,232 </td> 66 <td> 73,786,976,294,838,206,464 </td> 67 <td> 147,573,952,589,676,412,928 </td> 68 <td> 295,147,905,179,352,825,856 </td> 69 <td> 590,295,810,358,705,651,712 </td> 70 <td> 1,180,591,620,717,411,303,424 </td> 71 <td> 2,361,183,241,434,822,606,848 </td> 72 <td> 4,722,366,482,869,645,213,696 </td> 73 <td> 9,444,732,965,739,290,427,392 </td> 74 <td> 18,889,465,931,478,580,854,784 </td> 75 <td> 37,778,931,862,957,161,709,568 </td> 76 <td> 75,557,863,725,914,323,419,136 </td> 77 <td> 151,115,727,451,828,646,838,272 </td> 78 <td> 302,231,454,903,657,293,676,544 </td> 79 <td> 604,462,909,807,314,587,353,088 </td> 80 <td> 1,208,925,819,614,629,174,706,176 </td> 81 <td> 2,417,851,639,229,258,349,412,352 </td> 82 <td> 4,835,703,278,458,516,698,824,704 </td> 83 <td> 9,671,406,556,917,033,397,649,408 </td> 84 <td> 19,342,813,113,834,066,795,298,816 </td> 85 <td> 38,685,626,227,668,133,590,597,632 </td> 86 <td> 77,371,252,455,336,267,181,195,264 </td> 87 <td> 154,742,504,910,672,534,362,390,528 </td> 88 <td> 309,485,009,821,345,068,724,781,056 </td> 89 <td> 618,970,019,642,690,137,449,562,112 </td> 90 <td> 1,237,940,039,285,380,274,899,124,224 </td> 91 <td> 2,475,880,078,570,760,549,798,248,448 </td> 92 <td> 4,951,760,157,141,521,099,596,496,896 </td> 93 <td> 9,903,520,314,283,042,199,192,993,792 </td> 94 <td> 19,807,040,628,566,084,398,385,987,584 </td> 95 <td> 39,614,081,257,132,168,796,771,975,168 </td> 96 <td> 79,228,162,514,264,337,593,543,950,336 </td> 97 <td> 158,456,325,028,528,675,187,087,900,672 </td> 98 <td> 316,912,650,057,057,350,374,175,801,344 </td> 99 <td> 633,825,300,114,114,700,748,351,602,688 </td> 100 <td> 1,267,650,600,228,229,401,496,703,205,376 </td> The Power of Two (2) Tables from 1 to 100 Below you will find the Power of Two (2) tables from 1 to 100.

How to get the Screen Width in Javascript

Javascript gives a few options to determine the screen width. When we say screen, we mean the browser window’s width itself. Option 1 – Using Native Javascript Using native Javascript objects, we can find the innerWidth and innerHeight: 1 2 var w = window.innerWidth; var h = window.innerHeight; The window object also allows for: 1 2 3 window.screen.width // or simply screen.width We can write some code to tell us the width and height with fallbacks:

How to Read a File in Rust

If you need to read a file in Rust, then you can use the fs package from the standard library: 1 2 3 4 5 6 7 8 use std::fs; fn main() { let contents = fs::read_to_string(filename) .expect("Something went wrong reading the file"); println!("With text:\n{}", contents); }

How to Convert Array to String in Java

If you need to convert an array to a string in Java, then you could look at a solution like this: 1 2 3 4 5 6 7 String stringArray[] = {"Hello ", " world", " this", " is", " a", " test"}; StringBuffer sb = new StringBuffer(); for(int i = 0; i < stringArray.length; i++) { sb.append(stringArray[i]); } String str = sb.toString(); System.out.println(str); Or you could do something simple like this:

How to Join Two Strings Together in Golang

If you need to join two (2) strings together in Golang, you can do the following, using the + concatenation operator: 1 2 3 4 5 6 7 8 9 10 11 package main import ( "fmt") func main() { message1 := "Join Strings" message2 := "Together" result := message1 + " " + message2 fmt.Println(result) }

How to Recursively Delete all Files in an AWS S3 Bucket

If you need to recursively delete all files in an AWS S3 bucket, then you can do the following: 1 aws s3 rm --recursive s3://your-bucket-name/foo/bar/ If you only want to delete an specific object from AWS S3: 1 aws s3 rm s3://your-bucket-name/foo/bar/item.txt

How to Execute Linux Commands in Golang

If you want to execute linux commands in Golang, then you can use exec.Command: 1 2 3 cmd := exec.Command("echo", "hello world") res, _ := cmd.CombinedOutput() fmt.Println(string(res))

How to Check for Prime Numbers using Golang

If you need to check for Prime Numbers in Golang, then you can use the following method: 1 2 3 4 5 6 const n = 1212121 if big.NewInt(n).ProbablyPrime(0) { fmt.Println(n, "is prime") } else { fmt.Println(n, "is not prime") }

How to Raise a Number to a Power in Golang

If you need to raise a number to a power in Golang, then you can use the math.Pow function: 1 2 3 4 5 6 7 8 9 package main import ( "math" ) func main() { var exponent, base float64 output := math.Pow(base, exponent) }

How to get CPU Frequency in Linux

If you need to get the CPU Frequency in Linux, then you can run the following command: 1 watch -n.1 "grep \"^[c]pu MHz\" /proc/cpuinfo"

How to Disable Warning with Python Pandas

How can python pandas disable warnings? If you need to disable warnings with Pandas on Python, then you can use the warnings module and ignore the filter as follows: 1 2 import warnings warnings.filterwarnings('ignore')