Logic error when finding prime numbers in an array

Advertisements I’m receiving a logic error when trying to find all the prime numbers in this array on Codecademy: import java.util.Arrays; import java.util.ArrayList; class PrimeDirective { // Add your methods here: public void findPrime(int[] nums) { ArrayList<Integer> primeNumbers = new ArrayList<Integer>(); for (int i = 0; i < nums.length; i++) { if ((nums[i] % 2… Read More Logic error when finding prime numbers in an array

How to add a new button to display when the array has more than 2 values?

Advertisements I’m learning JS and I’m facing a problem, I’ve written my code below. var AddStock = document.createElement(‘button’); //Button for adding the ticker to the list var Ticker = document.createElement(‘input’); //Input of the tickers that the user is going to use var TickerChosenContainer = document.createElement(‘div’); //Container for all the tickers that the user is going… Read More How to add a new button to display when the array has more than 2 values?

Need help sorting arraylist alphabetically

Advertisements I need help sorting an array list that contains all the names in the names.txt file. I tried Collections.sort() like suggested but the list is still unsorted. What am I doing wrong? private static void Excerise04(String fname) throws FileNotFoundException { Scanner filescanner = new Scanner(new File(fname)); while (filescanner.hasNext()) { String line = filescanner.next(); ArrayList<String>… Read More Need help sorting arraylist alphabetically

Add nested records of JSON body into one column while exporting to CSV in powershell

Advertisements I have JSON including multiple nested records. I want to add records with comma separated and store it in a CSV file. JSON Body { "projectVitals": { "productName": "Enterprise", "name": "WhiteSource Bolt", "token": "61a48eab05356f149828c0e", "creationDate": "2022-10-17 09:08:46", "lastUpdatedDate": "2023-01-25 06:37:32" }, "libraries": [ { "keyUuid": "a89b-40759d783dc3", "keyId": 145110423, "type": "NUGET_PACKAGE_MODULE", "languages": "Nuget", "references": {… Read More Add nested records of JSON body into one column while exporting to CSV in powershell

Trying to access an object from array of objects-Cannot invoke "Student.setName(String)" because "s[i]" is null

Advertisements I am trying to access an object from an array of objects in a simple program. For hours , I have been scratching my head. The moment I try to call the object from the array of objects using its INDEX VALUE, the output is error. Problem: I am trying to take details from… Read More Trying to access an object from array of objects-Cannot invoke "Student.setName(String)" because "s[i]" is null

Find overall max element to the left of each element in an arrray (not the first max)

Advertisements I’m trying to find the max element to the left of each element but i could write code for only the first max. public static void main(String[] args) { int a[]={3,0,0,2,0,4}; Stack<Integer> st=new Stack<Integer>(); ArrayList<Integer> al=new ArrayList<>(); for(int i=0;i<5;i++){ while(st.size()>0 && a[st.peek()]<a[i]){ st.pop(); } if(st.empty()) al.add(a[i]); else al.add(a[st.peek()]); st.push(i); } System.out.println(al); } >Solution :… Read More Find overall max element to the left of each element in an arrray (not the first max)

For loop to remove non-strings from a list

Advertisements I am trying to iterate over the list elemets and remove non-string items. However the loop is failing and removing strings as well. currency.unique() array([‘CADCAD’, ‘CADCHF’, ‘CADEUR’, ‘CADGBp’, ‘CADUSD’, ‘CADNOK’, nan, ‘CADMXN’], dtype=object) Forex_ticker=list(currency.unique()) for item in Forex_ticker: if type(item)!=’str’: print(item,type(item),Forex_ticker) Forex_ticker.remove(item) else: continue CADCAD <class ‘str’> [‘CADCAD’, ‘CADCHF’, ‘CADEUR’, ‘CADGBp’, ‘CADUSD’, ‘CADNOK’, nan,… Read More For loop to remove non-strings from a list

Trying to get index values of an ArrayList and add them to another ArrayList

Advertisements Maybe somebody could help me. I don’t understand what is wrong with my code. I want loop through the ArrayList, get the index value of each element (index number itself) and add these index values to another array. Don’t ask me why =)) Here’s my code: List<Integer> a = new ArrayList<>(); a.add(2); a.add(5); a.add(1);… Read More Trying to get index values of an ArrayList and add them to another ArrayList