Posts by Victor Stafusa • 63,338 points
1,321 posts
-
2
votes2
answers325
viewsA: How to verify which result will appear more often in a chronological sum of an established sequence?
I was able to replicate your exit and your step-by-step with this: class Somas7 { public static void main(String[] args) { int[] histograma = new int[100]; for (int a = 1; a < 7; a++) { for (int…
javaanswered Victor Stafusa 63,338 -
4
votes2
answers691
viewsA: Correct use of Override and constructor
First, the correct term is aboutscrever, and not subscrever. You have a method mostracredito and a mostradebito. These methods in the subclass not only show credit or debit, but they also change it,…
-
3
votes1
answer494
viewsA: Repeated Elements Linked List
There are several things to take into account in your code. I don’t know if your teacher is a good boy or an executioner or how much he forgives or punishes the student for silly details. So I’ll…
javaanswered Victor Stafusa 63,338 -
2
votes2
answers77
viewsA: Algorithm problem
import java.util.Scanner; public class Ex10 { public static void main(String[] args) { Scanner x = new Scanner(System.in); System.out.println("Digite 3 valores em sequencia:"); int a = x.nextInt();…
-
2
votes1
answer180
viewsA: How to get the maximum value of a Java circular list?
Set a variable to contain the highest value starting with Long.MIN_VALUE and go through the list from the sentinel element by element until you get back in the sentinel (or instead of…
-
1
votes3
answers1055
viewsA: Java error - Square root calculation
Try to do so: private void jtNumeroKeyPressed(java.awt.event.KeyEvent evt) { try { double i = Double.parseDouble(jtNumero.getText()); jlResultado.setText("A raiz quadrada de " + i + " é " +…
-
3
votes2
answers2577
viewsA: Find amount of times a character appears in a word using recursiveness
First, let’s start with this: if(letra == palavra[i]){ return 1 + ocorrencias(palavra[i++], letra); }else{ return ocorrencias(palavra[i++], letra); } You can delete this repetition of the recursive…
-
1
votes1
answer761
viewsA: Error saving related entities - Unsave Transient instance
Try to change that: @OneToOne @JoinColumn(name = "codigo_linha") public Linha getLinha() { return linha; } For that reason: @OneToOne(cascade = CascadeType.ALL, orphanRemoval = true)…
-
2
votes3
answers606
viewsA: Find the element with the highest number
You can use the method Collections.sort(List<T>, Comparator<? super T>) to order candidates by some criterion (in this case the number of votes in descending order), and then take the…
-
0
votes2
answers47
viewsA: Error to popular array with Double
Let’s see that: static List<Historico> listaComCincoUltimosMeses = new ArrayList<Historico> (); static double[] arrayinvertidoComUltimosCincoMeses = new…
-
1
votes1
answer1571
viewsA: Arraylist of 2 dimensions
The ArrayList (or more exactly, the interface List, there is a programming principle that says you should code for an interface, not an implementation) is often used as a replacement for arrays. Use…
javaanswered Victor Stafusa 63,338 -
3
votes1
answer455
viewsA: Calculation of the weighted moving average with exponential adjustment
Looking at the formula, I think the solution would be this: public static double smooth(double a, double... d) { double soma = 0.0; double potencia = 1.0; for (int i = d.length - 1; i >= 0; i--)…
-
2
votes2
answers313
viewsA: Button locked in while
You have to understand the thread concept. AWT and Swing run on a thread called EDT (see more in that reply and also in that question). The EDT is responsible for receiving operating system events…
-
1
votes5
answers2766
viewsA: What HTTP code should I use when I can’t authenticate to third-party services with login and password provided by the client earlier?
A user had posted an answer, but he deleted it. Personally, I don’t agree with her because I don’t think it’s right to return 200 in case of error, but anyway, I copy the deleted answer from him…
-
2
votes3
answers8033
viewsA: Do not repeat records in a Join
Try this: SELECT er.id_exm_rea FROM t_cmo_Exame_Realizado er WHERE er.id_exm_rea IN ( SELECT pl.id_xfc FROM t_cmo_planilha_leitura pl )
-
1
votes2
answers159
viewsA: Why is the value in bytes displayed as 4?
The program you want should be like this: #include <stdio.h> #include <stdlib.h> typedef struct matriz mat; struct matriz { int lin; int col; int *arr; }; mat* cria(int lin, int col) {…
-
3
votes2
answers402
viewsA: On the RSA cryptography
Encryption basically works like this: (a) What is written using the private key can only be read with the public key. (b) What is written using the public key can only be read with the private key.…
-
1
votes2
answers3541
viewsA: Algorithm to calculate lifetime in days
I wrote that answer based on in this my other answer (there I explain it in more detail). I just translated the pertinent part of the algorithm to C. #include <stdio.h> #include…
-
5
votes3
answers4536
viewsA: How to select a piece of the Java string?
Note: The question was edited and with that this answer ended up being invalidated. However, I will keep the answer here because it can still be useful. Utilize x.replaceAll("(?:[^0-9]+)", ""),…
-
5
votes1
answer1434
viewsA: How to compare two shades of color and give the percentage of similarity (Java)?
I will reference this other answer of mine: Color composition It is important to keep in mind that although white is the result of the sum of red, green and blue, it does not mean that each of these…
-
15
votes1
answer637
viewsQ: How to find where is the circular reference in GSON?
Imagine that I have these classes: public class A { private B b; } public class B { private A a; } And then I have this: A a = new A(); B b = new B(); a.b = b; b.a = a; Gson gson = new Gson();…
-
8
votes1
answer292
viewsA: Best possible algorithm
Calculating the behavior of algorithms The complexity analysis of an algorithm demonstrates what its performance would be with size inputs tending to infinity. However, for a reasonable number of…
answered Victor Stafusa 63,338 -
12
votes4
answers8945
viewsA: What’s the importance of indenting the code?
Serves to keep code easier to understand. For example, look at this code without any indentation: if (a) { if (b) { while (c) { d(); } } else if (e) { f(); } else if (g) { h(); } } else if (i) {…
indentationanswered Victor Stafusa 63,338 -
0
votes2
answers82
viewsA: Relationship problem Manytomany - Java
You add patients to doctors but not doctors to patients. The simplest solution would be to do this: m1.adicionar(p1); p1.adicionar(m1); m1.adicionar(p2); p2.adicionar(m1); m1.adicionar(p3);…
javaanswered Victor Stafusa 63,338 -
7
votes4
answers4228
viewsA: How to perform query with some null values?
The SQL below returns the values correctly only if the field data_emissao be as nonzero in my BD table [independent if the parameter cliente was passed or not], but I want to return the values where…
-
3
votes2
answers94
viewsA: Give replace to the chars of a string
Try this: public String mask(String template, String toMask) { int tamanho = toMask.length(); StringBuilder replacement = new StringBuilder(tamanho); for (int t = 0; t < tamanho; t++) {…
javaanswered Victor Stafusa 63,338 -
1
votes1
answer103
viewsA: How to apply "If" & "Else" functions to Biginteger?
You are just using the wrong method. In your original code with long, you wore the % which is the rest of the division. Already in your code with BigInteger, you are using divide, that makes…
javaanswered Victor Stafusa 63,338 -
3
votes2
answers1828
viewsA: Learning to use ENUM in Java
All enum has a method called ordinal() which gives the position in which it was declared within the class, the first element being that of ordinal() == 0. Why, that’s exactly what your field codigo…
-
7
votes1
answer319
viewsA: What are the options to initialize a final variable in a Java class?
The best way is to use a builder for this: class Triangle { public int base; public int height; private final double angle; public Triangle(double angle) { this.angle = angle; } } Other less…
javaanswered Victor Stafusa 63,338 -
1
votes1
answer1266
viewsA: How to convert sysdate to milliseconds in Oracle?
Based in that reply in Soen, first you must create this function: create or replace function date_to_unix_ts( PDate in date ) return number is l_unix_ts number; begin l_unix_ts := ( PDate - date…
-
1
votes1
answer641
viewsA: Tomcat 8, Ajax (jQuery), Jersey, REST API does not work, enable CORS
Try adding this class to your project: @WebFilter(urlPatterns = "/*") // Coloque só os padrões que você quer abrir o CORS. public class OpenCorsFilter implements Filter { public OpenCorsFilter() { }…
-
0
votes2
answers748
viewsA: When I create two tables in Android Sqlite, only one works
Looking at their classes AlunoDao and DisciplinaDao, They are quite equivalent. The very few differences of something that exists in one and not in the other should have no effect (commented lines,…
-
1
votes3
answers504
viewsA: Javac command with more than one packaged class
If you are in windows, create a file compila.bat with the following: cd src dir /s /B *.java > ../sources.txt cd .. javac -d bin @sources.txt And then, just run the compila.bat. If you’re on…
javaanswered Victor Stafusa 63,338 -
7
votes4
answers220
viewsA: How many Strings are created in the codes below?
It’s not a complete answer, but it gives an idea. I compiled the following class with javac 1.8.0_111: public class Teste { public String x() { String s1 = "s1"; String s2 = new String("s2"); String…
-
0
votes4
answers220
viewsA: How many Strings are created in the codes below?
First, the compiler will already create the Strings "s1", "s2", "s3", "s4", "s5", "-s6", "s7", "s8", "s9" and "-s10" and these will be present in the generated bytecode and in the Strings during…
-
2
votes2
answers727
viewsA: Square root and cube number in a table
I think what you want is this: <!doctype html> <html> <head> <title>Raiz quadradas e cubos</title> </head> <body> <table border="1"> <tr>…
-
1
votes1
answer924
viewsA: How to make one matrix run inside another using functions?
Extracting a 3x3 chunk from a 10x10 matrix and passing it to a function has some complications. The first is that in matrices, all elements are stored in memory sequentially. That is, in a 10x10…
-
2
votes1
answer567
viewsA: Unity3d: How can I move Gameobjects from the same script independently?
Sine waves A sine wave has three relevant properties: Frequency - The inverse of the distance between two peaks of a sine wave. Amplitude - Is the difference between the peak height of the wave…
-
1
votes2
answers106
viewsA: Returning an object. Any Smell code here?
Now that you’ve edited the question, it can be solved with a simple Optional, hiding the constructor and exposing static methods of Factory: public final class RespostaDeArme { private final…
-
0
votes2
answers106
viewsA: Returning an object. Any Smell code here?
It depends a lot on what is the context in which the method armar() will be used. However, the way it is, I believe so, that there is actually a Smell code, although it should not be a serious Smell…
-
1
votes1
answer82
viewsA: Save only if not null
I think what you want is the optional = true in the @OneToOne. In addition, the inverse relationship (because it is bidirectional) can be done with the mappedBy: public class ObservacaoPessoa { @Id…
-
7
votes3
answers302
viewsA: What is the cost of calling many functions?
Significant performance differences because of the size of the stack would only make sense in a few cases. The ones I can name who are realistic are those: Intensive use of recursion at deep levels.…
-
3
votes1
answer2870
viewsA: JDBC - Run a query and use its return as a parameter for another query
The first point to note is an extra semicolon in your while: while (res.next()); { String hash = res.getString(3); System.out.println(hash); } Look at that semicolon after the (res.next())? So he’s…
-
4
votes3
answers404
viewsA: Java JSON, Socket, or RMI integration
[...] The first thought was to use JSON, but listening and reading some things, I ended up seeing that a lot of the processing of a server ends up going because of parse and as an alternative I came…
-
11
votes3
answers3408
viewsA: How to find out if a hexadecimal color is dark or light?
I told you that in this other answer of mine: Color composition It is important to keep in mind that although white is the result of the sum of red, green and blue, it does not mean that each of…
-
1
votes2
answers959
viewsA: How do I take the content of a text that is on the link (URL) and upload it to Textview?
To download the text, try this: public static String download(String url) throws IOException { try { return download(new URL(url)); } catch (MalformedURLException e) { throw new…
-
6
votes1
answer552
viewsA: Update command does not work
Control coupling Before answering your question directly, allow me to talk a little bit about the control coupling. If you have a method that has a parameter that denotes what it has to do and…
-
1
votes3
answers1185
viewsA: How to insert a new value into an array without duplicating through the switch case?
First to use the switch so it’s not a good idea. In general, the switch is something that tends to be easily used improperly and most of the times there is some better resource, there are few cases…
-
0
votes1
answer78
viewsA: Nullpointerexception when trying to mount an Activity to show a video
The class link ViewPlayer that’s the one:…
-
5
votes4
answers4955
viewsA: Questions about Python functions
Your question is a little old, but come on. Program 1 The first program has the following error: def retângulo(largura, altura, caractere): # ... retângulo(caractere, altura, largura) Note the…
pythonanswered Victor Stafusa 63,338