Posts by Victor Stafusa • 63,338 points
1,321 posts
-
-1
votes2
answers64
viewsA: Code continues running after game’s end
Try something like that: def ler_input(msg): a = 0 while a != 1 and a != 2: a = int(input(msg)) if a != 1 and a != 2: print("Você precisa digitar '1' ou '2'!!!") return a == 1 print("========TESTE…
pythonanswered Victor Stafusa 63,338 -
8
votes1
answer332
viewsA: How do you find out if the list is full?
Your question is a XY problem. You want to know how to determine if your list of books is full. But actually your real problem is that you don’t know how to define a list of books that is usable.…
-
1
votes2
answers347
viewsA: Java Language - Undo and Redo functions
You can use two lists as if they were stacks, one for the "undo" and one for the "redo". Also, when using lists instead of arrays, things will become much simpler to work with. import…
-
0
votes2
answers174
viewsA: Vector receiving a function that returns integers
We’ll rearrange your code: public class Teste { public static int[] vetorFuncao() { int[] numerosAleatorios = new int[10]; for (int i = 0; i < 10; i++) { numerosAleatorios[i] = ((int)…
-
3
votes2
answers319
viewsA: Python URI error - 2588 - Palindromes
You broke the line where you shouldn’t, inside the set(...).I think the way you wanted it was this: entrada = input() letras_unicas = set([i for i in set(list(entrada)) if entrada.count(i) %2 != 0])…
pythonanswered Victor Stafusa 63,338 -
17
votes3
answers2012
viewsA: Why does Scanner return error on something that is within the expected?
Let’s assume your entry is as follows: 5 Maniero 12 Note that the nextInt() will consume the 5 and return, without consuming the break-of-line that follows. When the nextLine() run, it will see the…
-
3
votes1
answer821
viewsA: Optimize factorial calculation using vector
This is easily solved by putting a for inside the other: #include <stdio.h> #include <stdlib.h> int main(int argc, char** argv) { int vet[5], fat[5], i; for (i = 0; i < 5; i++) {…
canswered Victor Stafusa 63,338 -
0
votes1
answer650
viewsA: Error reading string with scanf
Let’s start with this: scanf("%d %d", &numero, &alturam); This will then consume the two numbers at the beginning, but not the line-breaking that follows. The solution is to do this:…
canswered Victor Stafusa 63,338 -
0
votes1
answer73
viewsA: Repetition of Numbers
First, you don’t need these if (esc == 1), if (esc == 2), etc. The answer code of the previous question was made so that you had the same code no matter what the esc. I’ll repeat what I put in my…
-
1
votes1
answer98
viewsA: Java Web - Remove Method Caused by: org.hibernate.Mappingexception: Unknown Entity: java.lang.Integer
user is a variable of the type int. deletar is the name given to your EntityManager (bad name). When you do that: deletar.remove(user); Are you asking the EntityManager to remove an integer from the…
-
2
votes2
answers244
viewsA: Terminology - What is the difference between statement and statement?
Declaration is when you declare a variable, function, method, class or similar thing. Instruction is that where you instruct the program to perform a certain action. Statements are usually not…
-
2
votes1
answer568
viewsA: How to create binary tree with iterative algorithm?
Your first problem is that the root of the tree (dicio) is declared within the function itself load, statically allocated on the runstack (local variables are allocated on the runstack). This means,…
-
1
votes1
answer31
viewsA: Auto-increment in Android components
Try something like that: ImageView[] views = new ImageView[] { img1, img2, img3 /*, ..., imgN*/ }; for (int i = 0; i < bolaPreta; i++) { views[i].setImageResource(R.drawable.icon_circpreto); }…
-
7
votes1
answer667
viewsA: What is SSH and what are its advantages over HTTPS?
Do you know the windows command prompt or the linux shell? So, these are places where you type commands to be executed by the computer/device to which you are connected. When you need to connect to…
-
0
votes3
answers754
viewsA: Join objects within collection of different objects
First, you should better organize your code with identation and never forget the modifiers public or private, After all, in almost every case, package visibility is not what you want. And whenever…
-
1
votes2
answers1061
viewsA: What are real and practical examples in the use of Stored Procedures?
Let’s look at the possibilities: reduce network traffic improve database performance create scheduled tasks decrease risks create routines for processing In my conception, the main motivation would…
-
1
votes2
answers427
viewsA: How to create a generic class that receives data from two other classes
Your generic class would be like this: public class CrudRefeicao<T extends Refeicao> { public void cadastrar(T refeicao) { /* ... */ } public void remover(T refeicao) { /* ... */ } public void…
-
0
votes1
answer2170
viewsA: Calculation of sum of digits
Your program gets stuck inside the while and never comes out because the value of x never changes. Besides, if you have one while running through the digits, you should have soma = soma +…
pythonanswered Victor Stafusa 63,338 -
1
votes2
answers155
viewsA: Algorithm with flow chart/diagram
Your flowchart is quite confused and wrong. The only diamond I see where a decision should be made is at "Se EOF". However, below it there are no two lines "Sim" and "Não" leaving. But in the boxes…
-
0
votes3
answers1916
viewsA: How to replace vowels with @
C strings are arrays of characters. Use a for with an accountant i to traverse each position. In each iteration of for, you take the letter in position i and compares it to 'a', with 'e', with 'i',…
-
3
votes1
answer688
viewsA: Exercise with Hashmap
First, you should only wear one Scanner. Never use or create more than one and only one new Scanner(System.in). And there’s no point in shutting it down. Second, which class names must have…
-
4
votes2
answers592
viewsA: Best practices regarding the use of access modifiers
As you may already know, there are 4 types of java visibility: public private protected package-default (this is not a keyword, this is what happens when no modifier is used). In mature and…
-
31
votes2
answers2065
viewsA: What is "positive zero" and "negative zero" in float and double types?
Where float and double are specified The float and the double are implemented in accordance with the IEEE 754 standard, used by virtually all modern programming languages working with 32 or 64 bit…
-
8
votes1
answer535
viewsA: How does atan2 function?
Let’s see the Javadoc of method atan2(double, double), already translated into Portuguese by me: public static double atan2(double y, double x) Returns the angle tit conversion of rectangular…
-
4
votes1
answer284
viewsA: Classifications of design standards
The patterns in which there is divergence regarding the classification in your question are Bridge, Chain of Responsibility, Flyweight, Interpreter, Mediator, Memento and Visitor. Let’s start with…
pattern-designanswered Victor Stafusa 63,338 -
2
votes1
answer152
viewsA: Query in Mysql when there are two ids
Supposing: All records be duplicated. Duplicates always form a pair having a "Dial" and a "Hangup" in the column lastapp with the same uniqueid. All "Hangup" elements that are filled are equal to…
-
6
votes2
answers393
viewsA: Error storing data from a Select with Java
First, there is no reason to save letters in variable name, which is a bad programming practice. Second, use the Try-with-Resources. Third, avoid injection of SQL when using the PreparedStatement.…
-
1
votes1
answer129
viewsA: Doubt with reference when using add in Linkedlist, java
Change the type of pratos for List<List<Integer>>. Try to do this: for (String linha : texto) { String[] aux = linha.split(" "); if (aux.length > 2) { objeto = new CasosPossiveis();…
javaanswered Victor Stafusa 63,338 -
3
votes2
answers92
viewsA: JDBC code fails with exception
First of all, the principle of inversion of dependencies says it should code for abstractions, not implementations. A very direct application of this principle would be to use List instead of…
-
3
votes3
answers254
viewsA: Logic to compare four values and find the smallest
This even works, if the values are all different (if it has two or more equal values, it will go wrong). However, the number of checks within the Ses will grow unsustainably the more variables you…
-
1
votes1
answer55
viewsA: Are API Components in the aws package?
Whereas it was supposed to be AWT instead of AWS, let’s see: We have the class java.awt.Panel. We have the class java.awt.Label. We have the class java.awt.Window. Note that the correct spelling is…
-
2
votes1
answer95
viewsA: Jfxtextfield is not a Valid type. Error launching application in Javafx
Apparently, you’re using the Jfoenix. This is a build error. You forgot to put this: import com.jfoenix.controls.JFXTextField;…
-
0
votes1
answer518
viewsA: Error: java.lang.Nullpointerexception in project using JDBC
Let’s change a detail on your ConnectionFactory: public class ConnectionFactory { public static Connection getConnection() { try { return DriverManager.getConnection(…
-
0
votes1
answer53
viewsA: How to update Jtextarea automatically
Try this: int tp = 2; int colunas = 8; int pop[][] = new int[tp][colunas]; Random ran = new Random(); StringBuilder texto = new StringBuilder(tp * (colunas + 8)); for (int i = 0; i < tp; i++) {…
javaanswered Victor Stafusa 63,338 -
3
votes1
answer1092
viewsA: Recursive Fibonacci sequence
The recursive call to Fibonacci is quite simple: class Fibonacci { public static long fibonacci(int n) { return n < 2 ? n : fibonacci(n - 2) + fibonacci(n - 1); } public static void main(String[]…
-
3
votes1
answer2590
viewsA: Check the highest prime number - using while and if only
Its function ePrimo is not checking whether a number is prime or not, is trying to solve the problem of maior_primo. Instead, make her return True if a number is prime and False otherwise. Having it…
pythonanswered Victor Stafusa 63,338 -
8
votes1
answer214
viewsQ: How to compile a Java 9 project with Lombok in Gradle?
TL;DR How to pass multiple parameters -J--add-opens=<pacote>=ALL-UNNAMED so that Gradle uses them when calling javac? Details I have a code Helloworld.java using the Lombok in Java 9: import…
-
0
votes1
answer68
viewsA: Transforming Collections into Stream
That should do it from here: Map<String, List<Setor>> map = setores .stream() .collect(Collectors.groupingBy(Setor::getSiglaSetor)); List<Setor> listaSetores =…
-
3
votes1
answer96
viewsA: Random numbers repeating themselves
You can try to do that: private int numeroAleatorio() { return (int) (Math.random() * set * 10 + 1); } int esc = numeroDecisao(); int res1 = numerosAleatorio(); int res2 = numerosAleatorio(); int…
-
3
votes1
answer166
viewsA: int variable changes value when reading a char variable
Never, ever, ever dare use the function gets. This hideous function is hated by C programmers for a good reason: It is impossible to use it correctly and any and all ways to use it gets are wrong.…
canswered Victor Stafusa 63,338 -
0
votes2
answers28
viewsA: Jlabel does not receive new text
private void btnstartActionPerformed(java.awt.event.ActionEvent evt) { btnstart.setVisible(false); Random random = new Random(); int array[] = new int[1]; for (int i=1; i<array.length; i++) {…
javaanswered Victor Stafusa 63,338 -
2
votes2
answers135
viewsA: Doubt about access modifiers and polymorphism
Overlapping methods allow subclass to replace some of the methods that she sees in the superclass. If the superclass method is public or protected, then the subclass sees it, and when declaring a…
-
4
votes1
answer48
viewsA: My code only takes the last element of the database
How often do you urge your class Livro? Answer: One. Soon, you have only one book! The place where you live is here: livro = new Livro(); That’s the single place where you give a new Livro(). As…
-
4
votes1
answer2436
viewsA: What is the (real) usefulness of the javax.persistence.Transient annotation?
As already mentioned, it serves to delete a certain value from the JPA mapping and is useful in the case of calculated attributes. To give an example of where this is useful, imagine that this value…
-
2
votes1
answer24
viewsA: Article name does not let enter
The problem is here: scanf("s",&artigo); What you wanted was this: scanf("%[^\n]", artigo);
canswered Victor Stafusa 63,338 -
4
votes1
answer34
viewsA: Segfault no fwrite. Why?
Look at this: FILE *arq; if((fopen("alunos.txt", "w")) == NULL){ The result of fopen is not being assigned to arq. Thus, the variable of arq will get garbage. What you wanted was this: FILE *arq =…
canswered Victor Stafusa 63,338 -
26
votes3
answers814
viewsA: How and when to build an object in valid state?
TL;DR There are several possible alternatives to reducing the complexity of creating an object. First of all, it is necessary to verify that the class is well designed, respecting the principle of…
-
2
votes2
answers76
viewsA: Simplification of boolean function
Let’s set up the truth table of f: w x y z f 0 0 0 0 1 0 0 0 1 0 0 0 1 0 1 0 0 1 1 1 0 1 0 0 1 0 1 0 1 0 0 1 1 0 1 0 1 1 1 1 1 0 0 0 0 1 0 0 1 0 1 0 1 0 1 1 0 1 1 0 1 1 0 0 0 1 1 0 1 0 1 1 1 0 1 1 1…
-
3
votes2
answers439
viewsA: Java financial math equation
This one performs the calculation using the bisection method, looking for the j in the range of 105 up to 1. He works with BigDecimals rounding divisions to 21 decimal places and working with a…
javaanswered Victor Stafusa 63,338 -
2
votes1
answer95
viewsA: Mergesort, problem of logic
There are two errors. The first one is here: int[] aux = new int[10]; Was to be new int[x.length]! But your big mistake is here: for(i = inicio_vetor1;i<=meio;i++){…