Posts by Victor Stafusa • 63,338 points
1,321 posts
-
9
votes2
answers172
viewsA: Does the garbage collector really exist? Why then is there a memory leak in runtimes that use it?
Why the garbage collector only collects references that are no longer accessible. However, it is still possible to have memory leaks through accessible references. For example: public class…
-
4
votes1
answer252
viewsA: Java - What to do when JSON can come in Double and Long?
You can use the instanceof to find out if it is Double or Long and then choose how to treat: Object temperaturaObj = dataNode.get("temperature"); Long temperatura = temperaturaObj == null ? null :…
-
1
votes1
answer204
viewsA: Recursion in Java Queue
Use a loop for this instead of recursion: public int getMaior() { int maior = Integer.MIN_VALUE; if (primeiro == null) return Integer.MIN_VALUE; for (Celula i = primeiro; i != null; i = i.prox) {…
javaanswered Victor Stafusa 63,338 -
0
votes1
answer178
viewsA: Take the result of an expression in c++
It took a lot of work to do that. #include <string> #include <iostream> #include <fstream> #include <sstream> #include <vector> #include <cmath> #include…
-
2
votes1
answer124
viewsA: reverseOrder() In java not reversing
You can do it: private void inverter(int[] vet) { Integer[] vet_x = new Integer[vet.length]; // Copia criada, ... for (int i = 0; i < vet.length; i++) { vet_x[i] = vet[i]; // ... preenchida, ...…
-
3
votes1
answer68
viewsA: Error in Lexing a Mathematical Equation
Your mistake is silly. In the parts where you have try_parse(s) and first_char(s), you used || when you should use &&. I also recommend storing the results of try_parse(s) and first_char(s)…
-
2
votes1
answer54
viewsA: Thread independent from the result
Do it: new MeuController().getMetodo().forEach(model -> { new Thread(() -> { //agora vai processar }).start(); }); Or else, you create a method like this: private void processar(Model model) {…
-
1
votes1
answer2674
viewsA: Matrix multiplication and storing in another matrix?
It is one thing to read user matrices, another is to multiply them and another is to display them. The function that multiplies the matrices should do no more than the multiplication itself. The…
-
6
votes1
answer746
viewsA: Java: Dual-chained list (deck) implementation with callback, Generics, Exception and lambda
Let’s start with the Element: public class Element<T> { private Element<T> next; private Element<T> prev; private T value; public Element(T v) { value = v; } public void…
-
7
votes2
answers267
viewsA: Behaviour of malloc(1) in C
Neither of the two. The malloc allocates the memory area, but does not wipe it. Therefore, any content that was abandoned there will continue there (this content is affectionately called rubbish).…
-
0
votes2
answers47
viewsA: Problem when replacing strings
This is a silly error. The second parameter of replace is the amount of characters you want to replace, which in all cases should be 1 instead of i. Doing this small correction in the 4 operations,…
-
1
votes2
answers392
viewsA: Array sum storage in vector
You walk through the matrix in two dimensions, row and column, so it only makes sense to have two fors. The vector where you store the result is traversed along with the column, containing the sum…
-
0
votes1
answer34
viewsA: I’m not getting these steps ! I wanted help
First, although it is not strictly necessary, things will get easier if you add a field float media in his struct. To fill this field, use a variable soma inside the for do i and within the loop of…
-
0
votes2
answers32
viewsA: After typing the P Value, you are not running the printf of the second for
Look at this: int p; // ... scanf("%f", &p); I think what you wanted was this: float p; // ... scanf("%f", &p); I mean, it was to use float instead of int. Also, never use it gets. In the…
-
8
votes1
answer2560
viewsA: What API to use for Java Desktop applications
The truth is that no desktop Java API has gained much momentum. Java on the desktop has always been behind competitors, always being restricted to a niche. There aren’t many Java Apis on the desktop…
-
3
votes1
answer52
viewsA: Struct ! Error on line 47 at the moment it compiles, Follow the program in c
When you do that: struct aluno; You are trying to declare a variable incorrectly. Remove this line as aluno was previously declared as a 5 position array with the type of the struct. Notice that…
-
0
votes1
answer547
viewsA: Check whether a string is contained in another string without the string library. h?
Just take a look at function code strstr and copy and paste it. Disregarding the header and documentation comments, here is the code: char * strstr(string, substring) register char *string; /*…
canswered Victor Stafusa 63,338 -
5
votes1
answer96
viewsA: Lexicographically compute the letter of a string
You scroll through the strings only once, so it doesn’t make sense to have two loops while. All this can be solved with a for. You go through the two strings together until you find a different…
-
1
votes1
answer945
viewsA: java.lang.Nullpointerexception error when sending data to a web service
Your client is written in C#. So there is no way java.lang.NullPointerException on your side. Because as the name of this exception already says, it is a Java exception. It may even be that you…
-
6
votes1
answer1360
viewsA: Turn lowercase into string
You almost got it right, you just made a silly little mistake. Instead: origem[i]-32; Use this: origem[i]+=32; That is, it was to use more instead of less and lacked the equal sign. That said, you…
-
3
votes1
answer641
viewsA: Invert characters from a String
First, that the variable palavra actually contains a phrase. So, to not leave things confused, let’s call it entrada. You can use entrada.split(" ") to divide the input into the constituent words…
-
1
votes1
answer1024
viewsA: Put Maven Local Project as Dependency on Gradle Project
Your Maven file has some problems. First, remove these things from it: <parent> <groupId>org.springframework.boot</groupId>…
-
2
votes1
answer1711
viewsA: Simple text editor in C
You were on the right track, but you missed a few things: char linha[50]; does not mean a 50-line text, but a single 50-character line. I think what you wanted was something like char…
-
0
votes1
answer825
viewsA: How to implement a generic Mergesort sorting algorithm?
I made a solution below. I used a function pointer to be the comparator (int (*comparador)(void *, void *)). This compared can return a negative number if the first parameter (the first void *) is…
-
2
votes2
answers193
viewsA: How do I replace a specific part of a string?
I created a function that uses the indexOf to find the string to be replaced within the entry and a for to iterate on it and look for the correct occurrence to be replaced. Once the part to be…
-
8
votes1
answer89
viewsA: Can the & (bitwise and) Java operator be used for interfaces?
This is a lambda: (c1, c2) -> keyExtractor.apply(c1).compareTo(keyExtractor.apply(c2)); This here is a cast: (Comparator<T> & Serializable) That one cast causes the lambda to be…
-
4
votes1
answer89
viewsA: Work using Thread
A thread can only be started once. Try calling the method start() in an already started thread gives you this exception that you have: java.lang.IllegalThreadStateException. Thread is created, but…
-
2
votes1
answer63
viewsA: Access data from a class
Use the parameters in the constructor: double longitude1 = ...; double latitude1 = ...; double longitude2 = ...; double latitude2 = ...; Coordenada c1 = new Coordenada(longitude1, latitude1);…
-
1
votes1
answer766
viewsA: Relational library model, huh?
That one model is pretty flawed. Obra and Exemplar are related 1-to-1, and with that, the result is that they are effectively a single logical entity or that Exemplar is specialization of Livro, and…
-
1
votes3
answers51
viewsA: If logic with bool type and Object type
I think what you want is this: for (let i = 0; i < mensagemDeErroDoInput.length; i++) { var erro = mensagemDeErroDoInput[i]; var status = statusInput[erro.nome]; if (typeof status === "boolean" ?…
-
1
votes2
answers203
viewsA: Find the most frequent character in a text
Never use gets. Use fgets. I’ll explain more about that here and here. Your approach is wrong. To find the most frequent letter, use a table (with an array) to compute these letters. Remember that…
canswered Victor Stafusa 63,338 -
8
votes2
answers73
viewsA: Creating an array from two
There are numerous ways to do this. One of the ways is this: XXX[] arrayC = new XXX[arrayA.length + arrayB.length]; for (int i = 0; i < arrayA.length; i++) { arrayC[i] = arrayA[i]; } for (int i =…
-
0
votes1
answer548
viewsA: Error while building and running the project, java web, with jpa and Hibernate
The problem you’re having is mostly in this part of pom.xml: <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId>…
-
8
votes2
answers562
viewsA: Why assign NULL on a pointer after a free?
This depends a lot on the context where it is used. Especially the pointer after the free is not usable. With the value NULL, it is usable. For example: while (p != NULL) { // Um monte de código…
-
3
votes1
answer1226
viewsA: Doubt about Recursiveness in a Power Method
First I’m gonna fix the code: public class CalculaPotencia { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.print("Digite um número inteiro para base: ");…
-
3
votes2
answers198
viewsA: strcpy is merging numeric format with other chars
What happens is that your struct occupies 164 bytes in memory: 80 bytes for the name, including the null terminator; 4 bytes for the year, you are not considering the null terminator here; 80 bytes…
-
1
votes1
answer127
viewsA: Invalid operators in a binary expression C
The purpose of this code is to know if a note is below or above the average note of a set of notes. However, there are some points to consider: There is no operator div in C. Use the operator /…
canswered Victor Stafusa 63,338 -
0
votes1
answer323
viewsA: How to take a char type die by Joptionpane?
You can do it: String resposta = JOptionPane.showInputDialog("cadastrar: A-aluno P-professor M-medico"); if (resposta == null) { // O usuário clicou no botão Cancelar ou no X do canto da tela. }…
-
2
votes2
answers97
viewsA: Create a program that is usable on Windows
Based on a very simple example: create a calculator. Once you have the code, how do I create an application that can be run on Windows, a so-called "program"? Assuming you are in Windows and your…
-
1
votes1
answer44
viewsA: Hashtable: Segmentation fault when inserting - C
Watch this excerpt: HashTable = resize_HashTable(HashTable); int hash_value = hash(nick); while(HashTable->baldes[hash_value] != 0 && hash_value < HashTable->tam){ hash_value++; }…
-
2
votes1
answer135
viewsA: Graphs - return a pointer to the adjacency matrix
Just cast a cast for the right type on malloc: static int ** MATRIZADJint(int linhas, const int colunas, int valor) { int **m = (int **) malloc(linhas * sizeof(int *)); // aloca linhas for (int i =…
-
4
votes2
answers883
viewsA: Read lines from a TXT to Arraylist
In your file, each line corresponds to a coordinated and each coordinated has a longitude (between -180 and +180) and a latitude (between -90 and +90). The contents of the archive are coordinate…
-
4
votes3
answers1428
viewsA: (SQL) In parentheses in the WHERE clause
Just as multiplication and division take precedence/priority over addition and subtraction, the AND takes precedence/priority over OR. I mean, this: nfe.id_empresa = 4 and nfe.ano = '2016' and…
-
3
votes1
answer53
viewsA: Does anyone know how this Javascript code works?
First, there’s a syntactic error here: window.top.location.href = 'http://sendtestewebsite.com; Missed the ' closing the string: window.top.location.href = 'http://sendtestewebsite.com'; Now, let’s…
javascriptanswered Victor Stafusa 63,338 -
1
votes1
answer261
viewsA: How to return the second lowest java value?
You can do it like this: import java.util.Scanner; public class SegundoMenor { public static void main(String[] args){ Scanner input = new Scanner(System.in); System.out.print(segundoMenor(input));…
javaanswered Victor Stafusa 63,338 -
0
votes1
answer40
viewsA: Check data received by the user
Very simple: int eh_inteiro(const char *c) { int i = c[0] == '-' || c[0] == '+'; if (!c[i]) return 0; while (c[i]) { if (c[i] < '0' || c[i] > '9') return 0; i++; } return 1; } Here’s a test…
canswered Victor Stafusa 63,338 -
2
votes1
answer81
viewsA: Error with constructor
This is a compile problem. There is no method getX who receives a int as a parameter in the class MyPoint. However, this makes sense, because getters have no parameters. The idea of this code seems…
-
0
votes2
answers4931
viewsA: validate Cpf with javascript
Try this here: function muitoCurto(campo, nome, tamanho) { if (campo.value.length >= tamanho) return false; alert("O conteúdo do campo '" + nome + "' deveria ter pelo menos " + tamanho + "…
-
0
votes2
answers4931
viewsA: validate Cpf with javascript
function verificarCPF(strCpf) { if (!/[0-9]{11}/.test(strCpf)) return false; if (strCpf === "00000000000") return false; var soma = 0; for (var i = 1; i <= 9; i++) { soma +=…
-
2
votes1
answer214
viewsA: How to delete a node in a chained list in Java?
I think this should work: public static Node delete(Node head, int position) { if (head == null) return null; if (position == 0) { Node prox = head.next; head.next = null; return prox; } int cont =…