Posts by Victor Stafusa • 63,338 points
1,321 posts
-
2
votes1
answer98
viewsA: Printing a 3-in-3 formatted sequence of numbers separated by dash
Regex does magic: class Solution { public static void main(String[] args) { String s = "00-44 48 5555 8361"; System.out.println(solution(s)); String s2 = "00-44 48 5555 8361 xxx 1";…
-
2
votes2
answers87
viewsA: Multidimensional array list
Try this: aaff = [ "calças", "XXS", "16", "fita", "M", "13", "calças", "XXS", "1" ]; var ret = []; for (var i = 0; i < aaff.length; i += 3) { ret.push([aaff[i], aaff[i + 1], aaff[i + 2]]); }…
-
4
votes1
answer248
viewsA: Keyword 'final' for java variables
There is no performance relationship. The main reason is just to link some more checks made by the compiler in search of errors. When you put the modifier final In a variable, you’re telling the…
javaanswered Victor Stafusa 63,338 -
1
votes2
answers1716
viewsA: Change Javascript input color
I think what you want is to manipulate the property document.form.cor3.className: function cor() { var cor1 = parseInt(document.form.cor1.value) || 0; var cor2 = parseInt(document.form.cor2.value)…
javascriptanswered Victor Stafusa 63,338 -
0
votes1
answer238
viewsA: Take CPU usage in percentage
Your variable os contains a com.sun.management.OperatingSystemMXBean. This class offers a method getSystemCpuLoad() that returns a double between 0 and 1 indicating CPU usage or then -1 if it fails…
javaanswered Victor Stafusa 63,338 -
5
votes3
answers69
viewsA: Shouldn’t this function return a bool instead of a pizza?
The operator && in Javascript evaluates the first expression and being it something that can be true, returns the value of the second expression. For example: console.log('a' && 'b'…
javascriptanswered Victor Stafusa 63,338 -
7
votes3
answers3029
viewsA: How to calculate perfect numbers quickly?
Your program is just taking a really long time to calculate. Notice that you have one for inside each other. The outside loop will rotate more than 33 million times to find the 5th number. The…
-
1
votes3
answers491
viewsA: Converting C to Java
First, I think you missed one * before the peso++, which, by the way, I think should be ++peso in your case. The variables soma and peso should be initialized. Their initial value is zero. I suggest…
-
2
votes2
answers108
viewsA: Problems with Casino Roulette
Understanding your problem Let’s see that stretch: for(int i=0; i < jogadores.length;i++){ if(jogadores[i]== null){ System.out.println("Deseja adicionar jogador? sim ou não");…
javaanswered Victor Stafusa 63,338 -
1
votes1
answer658
viewsA: How to mock a private function in Junit
You can’t mock a private method. The idea of the mock is for you to provide false implementations of objects and not isolated methods (even more so when private). Moreover, a private method is by…
-
2
votes1
answer63
viewsA: Division and conquest algorithms design
The algorithm would be something like this: função z(int[n] A, int[n] B, int x) { // Linha 1 C = copy(B) // Linha 2 sort(C) // Linha 3 int i, k = -1 // Linha 4 for (i = 0; i < n && k ==…
-
0
votes1
answer80
viewsA: Can anyone help me fix problem?
First you should read two values A and B, but you’re just reading the B. Within the while, you should check the digit of the valor % 10 is the value of A and only then increment the contaDigitos.…
canswered Victor Stafusa 63,338 -
1
votes1
answer381
viewsA: Calculation of SQL hours
I think a better approach is to record only entries and exits for various reasons: Some don’t go out for lunch. Some may end up having to leave more than once in the middle of the day for some…
-
3
votes1
answer476
viewsA: Read JSON and turn it into Java list
When using dados.getJsonArray("_dados"); you’re trying to read the _dados from within the sucesso, which makes no sense. In fact, the node is not called sucesso and yes _status and he’s a String…
-
3
votes1
answer183
viewsA: Problem to add at the end of a Simple Chained List
The problem is you missed one return at the end of if of addFimRecursivo: private void addFimRecursivo(Node anterior, Node atual, int chave) { if(atual.prox == null) { Node novo = new Node(chave);…
-
4
votes1
answer125
viewsA: Do I need to download JVM to run a java program on my PC since I’m not a developer?
I need to download JVM to run a java program on my PC since I’m not a developer, that is, I will only need JVM, just to run this program? Yes. Better, because programs made in java run on people’s…
-
10
votes2
answers1126
viewsA: What kind of data (double, float) should I use when representing a temperature?
Let me copy some of the stuff I posted in this answer: The float and the double are implemented in accordance with the IEEE 754 standard, used by virtually all modern programming languages working…
-
1
votes1
answer186
viewsA: Problems with graphs in Java
EOF means the end of the file. Since this is used in a context where you read from the standard entry, it means the end of the System.in. First, let’s come up with a class that represents an edge:…
-
3
votes2
answers2915
viewsA: Compare dates using Localdate
To add days to LocalDate, use the method plus(int, TemporalUnit). To check if it is a working day, recommend that other answer of mine. That way, to add up to two working days, you can do this:…
-
2
votes2
answers1277
viewsA: How to manipulate a CSV file column?
First, you create a class to represent the player: public class Jogador { private final int id; private final String nome; private final int idade; private final String cargo; private final int…
-
2
votes2
answers183
viewsA: How to implement this while not from Python in C?
Direct translation to C would be as follows: while (!(f1 == 0 && fn == 1)) { ... } However, this can be simplified by using the de Morgan’s law for that reason: while (f1 != 0 || fn != 1) {…
-
3
votes1
answer788
viewsA: How to compare all values of an array with a variable
First, you can’t convert directly from String for Date. So we’ll need a conversion step: SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); sdf.setLenient(false); Date[] datas =…
-
2
votes2
answers53
viewsA: Why am I having trouble with C-scanf?
Because the %s reads a word. This means that the white space between the hello and the world is one of the things that makes him stop reading. Use %[^']s instead. This [^'] means something more or…
-
4
votes1
answer523
viewsA: Searching in depth using a stack
The procedure should be more or less like this: Inserts the first node into the stack and marks as visited. While there are nodes in the stack, pop the knot and stack all your neighbors that have…
-
1
votes2
answers3596
viewsA: Log in with network user (LDAP)
Hi. I know this is an old question, but I was directed here because of a problem my friends were having. I created a little project to authenticate and put it in the format of a library on Github, I…
javaanswered Victor Stafusa 63,338 -
1
votes1
answer2163
viewsA: Ascending Order in a chained list in c
First I’ll start with the simplest problems. Missed a spot after the %d in function imprime. Avoid using l or O as a variable name, because it is very easy to confuse this with the numbers 1 and 0.…
canswered Victor Stafusa 63,338 -
2
votes1
answer31
viewsA: Doubt regex use information after "_"
The regular expression is as follows: _(.{3}) Here’s a Javascript test that shows you how it works: var str = 'NeName = MGLUE_EPCVMH_UGW01'; var regex = /_(.{3})/m; var match = regex.exec(str);…
regexanswered Victor Stafusa 63,338 -
2
votes1
answer70
viewsA: Why is this C code not working (binary tree)?
Apparently, the error is in these two lines: maior = &t->dado; printf("%p %p", maior, &t->dado); I think what you wanted was this: *maior = t->dado; Making this simple change, the…
-
4
votes1
answer38
viewsA: Code before or after super
In your specific case, you should first warn the functionality of the event’s parent class start, and therefore the super.onStart(); would be at the beginning. However each case is a case. There are…
-
1
votes1
answer1474
viewsA: Java Spring Boot - Read a file from the Resources folder inside a . jar
First, I suggest you follow the language conventions and instead of naming his class as run_transform, name her as RunTransform. You can get a InputStream for the resource within the JAR when using…
-
1
votes1
answer2507
viewsA: Find and show the way - maze in C
I didn’t study your code in depth, but it seemed to me to be much more complicated than necessary. Running it in this little maze you gave it, it bursts the time limit. A simpler approach is to use…
canswered Victor Stafusa 63,338 -
0
votes1
answer928
viewsA: In descending order, remove a value from the inserted number to 0
Your program works. See it working on ideone. With input 12, it produces this output: DIGITE UM NUMERO Numero: 12 Numero: 11 Numero: 10 Numero: 9 Numero: 8 Numero: 7 Numero: 6 Numero: 5 Numero: 4…
-
2
votes1
answer134
viewsA: Recursion Doubt with String in Java
str[ultimo caracter] str.charAt(str.length() - 1) str[do primeiro ao penúltimo caractere] str.substring(0, str.length() - 1) But there is still one condition to work, as you have to ensure that…
-
2
votes1
answer57
viewsA: How to improve performance in this code
To analyze this code, let’s apply some transformations: Correct the indentation. Put variable statements in the smallest possible scope. The variable tam can be stated where she receives numero. And…
canswered Victor Stafusa 63,338 -
3
votes1
answer38
viewsA: Different results with the same SQL statement
PreparedStatement ps = conexao.preparedStatement("SQL_ACIMA"); ResultSet rs = ps.executeQuery(); List<Categoria> categorias = new ArrayList<>(); while(rs.next()) {…
-
3
votes2
answers4332
viewsA: How to identify a line break character in C?
Your problem is almost the same as the one outlined in this other question. The only difference is that yours is in C, not Java. The solution approach is the same. You are reading lines containing…
-
1
votes2
answers121
viewsA: Using float to check overflow in unsigned long int
The float has a maximum accuracy of 23 bits, as demonstrated in this answer. Like the unsigned long int must have 32 bits, the float will not be able to store 4294967295 accurately enough. See this:…
-
1
votes2
answers1433
viewsA: Invert "string"
For me, InverterString("abcde"); produced as a response edcba. See here in ideone. However, due to the numbers 5 and 4 in the code, this only works for strings of size 5. Any strings larger or…
-
7
votes1
answer402
viewsA: How to convert a list of Strings to an integer list
First thing: Don’t use FileWriter, because this class uses the standard machine encoding rather than having a user-defined encoding, leading to encoding incompatibility problems. There are even…
javaanswered Victor Stafusa 63,338 -
1
votes2
answers128
viewsA: If Else Statement problems chained in C language
Let’s reorganize and simplify this code using the wonderful if else: float Peso, Altura, Imc; printf("----------- #17 -----------\n\n"); printf("Para fazer o calculo do IMC, forneca seu peso: \n");…
-
1
votes1
answer349
viewsA: Check if an expression is well formed
The first thing that caught my attention was this: for (int i = 0; i < strlen(v); i++) if (v[i] != '{' || v[i] != '}' || v[i] != '[' || v[i] != ']' || v[i] != '(' || v[i] != ')') v[i] = 0; This…
-
0
votes4
answers940
viewsA: Java: How do I store a vector in another vector and using Math library
Using Java 8+, you can generate the array with the squares in a single row. Assuming your array is the variable numeros, then you can do it: int[] quadrados = IntStream.of(numeros).map(x -> x *…
-
3
votes2
answers138
viewsA: CSS - How to create a button or design like this reversed
Use the CSS property transform with perspective and rotateY. For example: #botao { background: yellow; width: 200px; padding: 10px; text-align: center; font-size: 20pt; font-weight: bold; transform:…
cssanswered Victor Stafusa 63,338 -
2
votes1
answer109
viewsA: Trajectory of an object using canvas in javascript
This is the code that controls the path of the square: if(x<=700 && y==150) x = x + 10; if (x>=700 && y>=0) y=y-10; if (x>=350 && y<=0) x=x-10; This code is…
-
7
votes2
answers1420
viewsA: How to split a String that contains white spaces at the beginning?
Let’s look at this simpler case: class TesteRegex { public static void main(String[] args) { String s = " A B C "; String[] d = s.split(" ", 5); System.out.println(d.length); for (int i = 0; i <…
-
7
votes2
answers673
viewsA: How to identify if the user is at the top of the page?
Check whether $(document).scrollTop() returns 0. If return 0, it is at the top of the page. If you do not want to use jQuery, use document.documentElement.scrollTop. If the page is at the top, hide…
-
3
votes2
answers140
viewsA: Substring program
If you already have the C code working, just translate it directly to Java. You don’t need to redo anything completely other than scratch. See the translation: puts → System.out.println. string →…
-
1
votes1
answer208
viewsA: Pointers and data type hidden in C
typedef struct Pilha { char *v; int topo; int tamanho; } Pilha; Pilha *criaPilha(int tamanho) { Pilha *p = (Pilha *) malloc(sizeof(Pilha)); p->v = (char *) malloc(tamanho); p->topo = 0;…
canswered Victor Stafusa 63,338 -
2
votes2
answers101
viewsA: Form with PHP sends POST variables to same page with jQuery without reloading
There are some errors in your code. Your <input type="submit" > is the button that sends and reloads the page. You did not place the event click on that button, and yes you put it in the form.…
-
1
votes2
answers112
viewsA: Problem when trying to add an array of objects within another java array
First, we see that the expression listaNotas.get(i).getProdutos() is repeated. Let’s put this into a variable to avoid repeating and also simplify your code a little more by dividing long…