Posts by Victor Stafusa • 63,338 points
1,321 posts
-
0
votes1
answer1903
viewsA: java.lang.Noclassdeffounderror: Could not initialize class com.ailson.mavenproj.factory.Jpautil
Remove that line from your persistence.xml: <jta-data-source>java:jboss/datasources/MySQL-DS</jta-data-source> You are managing the creation of EntityManager by creating yourself the…
javaanswered Victor Stafusa 63,338 -
1
votes2
answers71
viewsA: String search in file . log in Java
You can get a Stream<String> that matches the file lines using File.lines(Path) (Java 8 or higher). Your code should look like this: File.lines(Paths.get("seu_arquivo.log")) .filter(x ->…
javaanswered Victor Stafusa 63,338 -
0
votes3
answers253
viewsA: Refactor code using Factory Pattern without using the if-elseif condition
You can use the library Reflections and then do this: public class InvoiceParserFactory { private static final Set<Class<? extends InvoiceParser>> SUB_TYPES; static { Reflections…
-
1
votes3
answers253
viewsA: Refactor code using Factory Pattern without using the if-elseif condition
You can use a ServiceLoader. This is the mechanism that was introduced in Java 6 to solve problems exactly of this type that you have. In your case, the InvoiceParserFactory corresponds to a…
-
2
votes3
answers446
viewsA: Sum of component numbers
Read the user number: Scanner entrada = new Scanner(System.in); int numero = Integer.parseInt(entrada.nextLine()); Make sure the number is ok: if (numero < 100 || numero > 999) {…
javaanswered Victor Stafusa 63,338 -
2
votes2
answers465
viewsA: How to prevent the object to exceed the limits of the screen?
I managed to do so: import java.awt.Color; import java.awt.EventQueue; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.event.MouseAdapter; import…
-
1
votes1
answer48
viewsA: Comparison Datetime in scheduling in the database
I will assume that :data_inicio and :data_fim be the timestamps of the trip you want to mark, with the id driver :idm and vehicle :idv. I will also assume that your tables are viagem, motorista and…
databaseanswered Victor Stafusa 63,338 -
1
votes1
answer61
viewsA: Code error repeating values in threads?
Your code has some problems: You’re using in some places “aspas inglesas” instead of "aspas duplas comuns". This then does not compile. However, this is also very easy to arrange. Its function…
-
2
votes1
answer1397
viewsA: Program that reads real number in Java
You can use nextLine() and then Double.parseDouble. The reason to do this is what is explaining in that other question. Your code goes like this: import java.io.IOException; import…
-
2
votes2
answers79
viewsA: Colour tone of the record, for quantity
First, let’s create a function that given a color that corresponds to the value 0, a color that corresponds to the value 1 and a value between 0 and 1, provide the corresponding color: function…
phpanswered Victor Stafusa 63,338 -
2
votes1
answer257
viewsA: Place object array in combobox
It’s not just because there’s a method toString() that means it would be called automagically. You should call it: public void PopulaCategoria() throws SQLException{ for(Categoria categoria :…
-
1
votes1
answer166
viewsA: How to convert Scanner to double in java?
I see no reason for you to try using an array here. I think what you want is just this: double a = vlrus.nextDouble(); Then store the result in the array: double[] b = new double[] {a};…
javaanswered Victor Stafusa 63,338 -
2
votes1
answer174
viewsA: Substitution in c
The best decoding attempt I could get was this. Explanations in the code: #include <stdlib.h> #include <stdio.h> #include <string.h> #define MAX_STR 1000 #define NUM_LETRAS 26 int…
canswered Victor Stafusa 63,338 -
2
votes1
answer144
viewsA: Infinite loop with an if inside a do in Java
Notice that he will repeat the loop while op is different from 3. Inside the loop, nowhere it changes the variable op. So, supposing that op is 3, then after at least one of the values saldoCC or…
-
2
votes1
answer656
viewsA: Convert recursive to iterative function
Stacked, the calls in the code are. When pop-up, the list nodes reconnected. Back-to-front, in their original code, merged the list. In the call stack, the nodes to be connected, stored. No stack…
-
4
votes2
answers749
viewsA: How to put icon on Joptionpane buttons?
Digging into the inner details of the classes javax.swing.JOptionPane and javax.swing.plaf.basic.BasicOptionPaneUI, I managed to do with it: import java.awt.EventQueue; import…
-
1
votes2
answers165
viewsA: Calling the method of a specific superclass
If who made the class Pai (maybe yourself) decided to replace the class method Avo by one another, it is because he was inadequate, incomplete or something. If on the other hand, the class Filho or…
-
1
votes2
answers98
viewsA: Reflect java caching, how to do?
Maybe what you want is just this: T3 newInstance = T3.class.getConstructor().newInstance(); In that code, T3.class is an object of the type Class<T3>. From it, you can create instances. This…
-
1
votes1
answer55
viewsA: Displayed Value and Nan
The problem is this: Altura : tdAltura.textContent It was supposed to be lowercase: altura : tdAltura.textContent I just changed that in the code and nothing else, now it feels like it’s working:…
javascriptanswered Victor Stafusa 63,338 -
13
votes1
answer1027
viewsA: Galton’s Board - Central Limit Theorem
I did with it: <?php function cai_bolinha($linhas) { $sorteio = mt_rand() % (1 << $linhas); return substr_count(decbin($sorteio), '1'); } $linhas = 15; $bolinhas = 100000; $casas = []; for…
-
0
votes1
answer269
viewsA: Eliminate cycles in graph by removing fewer vertices
First, you try to make a topological ordering graph. In a topological ordering, you define an order that tells which vertices come before or after which other vertices. In a graph with cycles, there…
-
0
votes2
answers98
viewsA: Reflect java caching, how to do?
If I understand your question correctly, just do it: Class<?> clazz = Class.forName("br.com.macadu.aprendizado.testando.T3"); log.info(clazz.toString()); for (Field field : clazz.getFields())…
-
1
votes4
answers81
viewsA: Succinct conditional structure with multiple comparisons
Our friend Miguel formulated the following answer, but for some reason he did not want to post: <?php $title = 0; $squad = 0; $level = 0; var_dump([$title, $squad, $level] == [0,0,0]); $level =…
-
3
votes4
answers81
viewsA: Succinct conditional structure with multiple comparisons
You have put the tags PHP and Java, so I will give solutions in both languages. In PHP: $t = array_unique(array($title, $squad, $level)); if (count($t) == 1 && in_array(0, $t)) { return…
-
2
votes3
answers110
viewsA: How to call the same method with different arguments but inherited from the same mother class?
Complementing the answer from Dudaskank, the reason why Set<? extends Coluna> solves why a Set<Coluna> is an object in which these two properties are worth: We can obtain items of the…
-
1
votes1
answer545
viewsA: Help in C algorithmic activity
Let’s see, under what conditions the first date is higher? If the year of the first date is greater than that of the second date. Or if the years are equal, if the month of the first date is greater…
canswered Victor Stafusa 63,338 -
3
votes1
answer72
viewsA: Regular expressions do not match the desired text snippet
There are three possible behaviors in regular expressions: Greedy, reluctant and possessive. What you want is reluctant behavior. You can use the .*?, where .* means to take something and the ?…
-
4
votes1
answer91
viewsA: ISA x Performance
ISA means "Instruction Set Architecture" - "Architecture of Instruction Set". There are two types of architectures to consider: RISC and CISC. The CISC means "Complex Instruction Set Computer" -…
-
6
votes2
answers229
viewsA: Arraylist statement with type or without
The two codes are equivalent. The second form with the diamond (<>) was introduced in Java 7 with the aim of leaving the syntax of generic types leaner, avoiding the redundancy of having to…
-
2
votes2
answers248
viewsA: Call method by class or by instance?
The correct form depends on the method you call: If the method has the modifier static, use the class name. If the method nay has the modifier static, use the variable with the reference to the…
-
4
votes1
answer1409
viewsA: How to create a tree with n children per node?
I’m trying to do this with a ArrayList, but I don’t understand how to insert a ArrayList within a ArrayList recursively or interactively even because the tree has no defined limit. That’s not what…
javaanswered Victor Stafusa 63,338 -
0
votes1
answer1592
viewsA: Convert letter by number to visualG
Try to use asc to convert the letter to a number according to the ascii table, manipulate the number and then use carac to convert back to text. Escreval("Escreva uma letra de A a D para conversão:…
visualganswered Victor Stafusa 63,338 -
0
votes1
answer199
viewsA: Java parce csv to my bean - Opencsv
Add this to your classpath configuration (which I assume is Maven): <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 --> <dependency>…
javaanswered Victor Stafusa 63,338 -
2
votes1
answer45
viewsA: How to get the edge of the line drawn with Graphics in Java straight?
Use the class RenderingHints: private static final RenderingHints HINTS = new RenderingHints(null); static { HINTS.put(KEY_ALPHA_INTERPOLATION, VALUE_ALPHA_INTERPOLATION_QUALITY);…
-
0
votes2
answers318
viewsA: Enum getting next code in Java
You can try using one Converter/AttributeConverter: package br.com.academia.domain; import java.io.Serializable; import java.util.Date; import javax.persistence.CascadeType; import…
-
2
votes1
answer78
viewsA: How to assign a treated value in the function to my vector?
I’ll give you an upgrade on your code: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #define TAM_MIN 1 #define TAM_MAX 500 /* Síntese…
-
2
votes1
answer62
viewsA: How to turn a number reading directly into a stream?
With the method Stream.generate(Supplier<? extends T>), you can read the numbers of Scanner without needing the ArrayList. I know the tag is java-8. But if you accept to use Java 9 or higher,…
-
2
votes1
answer233
viewsA: Invoking a method by reflection
Your Reflection fails because the metodoB is an instance method and not a static method. Therefore, to invoke it (either normally or by Reflection), you need the instance. You even try to solve it…
-
3
votes2
answers825
viewsA: Lambda in Java 8 launching Exception
Lambdas and checked exceptions do not form a good mixture. But before that, throws Exception and throw new Exception are bad programming practices. You should release and treat specific exceptions…
-
2
votes1
answer183
viewsA: How to do a while without locking the application in Java
First, I see here a XY problem: I’m building an application in college that I need to run a code for 2 minutes and minute to minute to do an evaluation of the system and the variables that are in…
-
2
votes1
answer147
viewsA: Evaluate tic-tac-toe code
(index>=1||index<=9)&&!add_board(index) should be index < 1 || index > 9 || !add_board(index). player->cod+=pow(2,index); can be rewritten as player->cod |= 1 <<…
canswered Victor Stafusa 63,338 -
4
votes1
answer729
viewsA: Bubble Sort improved
First, watch out for the identation of your program, which is horrible. It helps others (and also yourself) to understand it. If you had done that, you’d see that one was missing { after the first…
canswered Victor Stafusa 63,338 -
17
votes1
answer583
viewsA: What is the complexity class NPI?
P problems are those that admit deterministic solution in polynomial time. NP problems are those that admit solution nay-deterministic in polynomial time (i.e., if you see a possible answer in a…
-
8
votes3
answers654
viewsA: How to separate a number into two
double real = Math.floor(valorEntrada); double frac = valorEntrada % 1;
javaanswered Victor Stafusa 63,338 -
3
votes3
answers214
viewsA: Java Mergesort sorting algorithm error
Your program has some problems: If you are working with lists, it makes no sense to call the list of array, since it is not an array but a list. It’s easier to work with fim being the last item on…
-
4
votes2
answers53
viewsA: I must stop the application when I run a test
Your question doesn’t even make sense. Running Junit tests has nothing to do with running in production. Junit tests are sort of an extension of the build process. You compile your project and run…
javaanswered Victor Stafusa 63,338 -
4
votes4
answers103
viewsA: How to avoid value repeats in this code?
Based in this other answer of mine here: function eliminarAcento(palavra) { var latinoMaiusculo = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; var latinoMinusculo = "abcdefghijklmnopqrstuvwxyz"; var…
-
3
votes2
answers233
viewsA: Binary search tree (Binary search Tree) and binary search (Binary search)
Almost that. The concept of binary search is the idea of dividing the search space successively into halves until you only have the element you were looking for or conclude that it is not in the set…
algorithmanswered Victor Stafusa 63,338 -
3
votes3
answers1148
viewsA: How to concatenate strings without using a function?
You create a new string of sufficient size and copy the characters of the two strings into the new one. The size is the sum of the sizes of the other two strings. The copy of each string can be done…
-
0
votes1
answer72
viewsA: Reading of files in Java
You can rewrite your class Stock thus: import java.io.File; import java.io.Scanner; import java.io.IOException; import java.util.ArrayList; import java.util.List; public class Stock { private final…