I’m trying to generate an array of 5 non-repeating integers in Java, but there are still repeats when I run it. Here’s my code so far: Any help is very much appreciated! Answer So, your for-loop is checking for repeated characters, BUT each time you generate a new value (in the while-loop) you’re not checking to see if the value
Tag: arrays
In IntelliJ IDEA, while taking input in an array from console, character of enter key is accepted as an element
I was seeing an example of linear search in an array in Java, and I wrote this code: But while running this code, due to the System.out.println() at the 10th line, the carriage return (of the println() ) is taken as the element at index 0. Further, as I enter more elements, after each element I need to press Enter
read a File in hex byteArray and write part of that array to another File – java android
I have a 200kb file and I have to read it in bytes, and then write part of this byteArray (from index 90000 to 165000) to another file. How can I accomplish this. Answer Use a RandomAccessFile in order to seek to the offset to start copying from. For example:
Solving a matrix equation in Java
I have been trying to implement the given formula in JAVA but i was unsuccessful. Can someone help me find what I am doing wrong? Do i need to shift the summation index and if so how? My code: Answer Just trying to collect all my comments under the question into one coherent answer, since there are quite a few
How do I read strings from a file that already contain double quotes?
I have a list of names in a .txt file which are in the format: “Tim”, “Dave”, “Simon” The input will always be single value names in quotes, comma separated and on a single line. I want to read these into String[] names. I have the following code, but the output puts each of them in double quotes, meaning it
How to split an array of objects in java or Kotlin?
I have already asked one question about this here but, that answers about only strings. I could not edit that as there a couple of answers there already. Now, I get it how to split it with a space as given in that post. But, how can I split it with objects of custom class like this: Now, I want
AlgoExpert: Validate Subsequence, not passing all test cases
Question: Validate Subsequence on AlgoExpert. Given two non-empty arrays of integers write a function that determines whether the second array is a subsequence of the first one. My code is not passing all test cases, what am I doing wrong? Answer Thanks for everyone’s help! Updated Solution:
Remove duplicate letters from a string sentence while keeping the spaces from the sentence
I am trying to take a sentence that a user has entered into a String variable and remove the duplicate letters while keeping the spaces in the sentence. For example if a user enters “hello my name is danny” it will return “Helo my nam is dn”. To do this I have already tried this code: This code however doesn’t
How to add items to array of objects using loops
I’ve got a multiple student objects I want to write into with a CSV file containing their details. I’ve set each row of the CSV file to an array then was going to split each entry of the array into another array and use that to set the attributes of the object. However, each time I try, I get a
Calculating the difference between max of even numbers and min of odd numbers of an array
The question statement says – You are given an array of integers A of size N. Return the difference between the maximum among all even numbers of A and the minimum among all odd numbers in A. Examples – A = [5, 17, 100, 1] – Ans => 100-1 = 99 My Approach – The code is working fine for