[Solved] String parsing, replace new line character

Strings are immutable in Java – operations like replace don’t actually update the original string. You have to use the return value of the substring call, e.g. String updated = str.substring(j,str.indexOf(‘>’,j+1)).replaceAll(“\n”, “#10”); If you want to replace this in the overall string, you can simply concatenate this back into the string: int indexOf = str.indexOf(‘>’,j+1); … Read more

[Solved] If statement skipping || if first condition is false [closed]

The solution may be, that you need to put your statements in brackets. else if ((address.getLastName() != null ? address.getLastName().toLowerCase().contains(searchKey) : false) || (address.getFirstName() != null ? address.getFirstName().toLowerCase().contains(searchKey) : false) || (address.getTitle() != null ? address.getTitle().toLowerCase().contains(searchKey) : false) || (address.getStreet() != null ? address.getStreet().toLowerCase().contains(searchKey) : false) || Integer.toString(address.getPlz()).equals(searchKey)) { outputList.add(address); Otherwise the statements are not … Read more

[Solved] Trying to sentinel loop this program [closed]

while (true) { System.out.println(“What type of Employee? Enter ‘o’ for Office ” + “Clerical, ‘f’ for Factory, or ‘s’ for Saleperson. Enter ‘x’ to exit.” ); String response= inp.nextLine().toLowerCase(); // validate input, do you switch statement, return; on x } 1 solved Trying to sentinel loop this program [closed]

[Solved] What is Difference between Static Variable and Static method and Static Class? [duplicate]

A static variable is a variable: public static int whatever = 0; A static method is a method: public static void whatever() {} A static class is a class: public static class SomeInnerClass {} (A class can only have the static modifier when it is nested inside another class) Static variables and methods can be … Read more

[Solved] Get data type from String when the String have symbol [closed]

In this case (when the token defining it as a number is at the beginning): final String input = “Rp.1.000.000.000”; int value = 0; String token = “Rp”; if (input.contains(token)) value = Integer.parseInt(input.replaceAll(“[^012456789]”, “”)); Explanation .replaceAll(“[^012456789]”, “”) removes all non-numeric chars (R, P and . in this case). So the leftover is a String only … Read more

[Solved] Searching and Counting Greatest Values [closed]

First error: System.out.println(“the biggest value is: “+n+” and it is occurs “+H+” times”);}} The n is your TryCount. Should be: System.out.println(“the biggest value is: “+y+” and it is occurs “+H+” times”);}} Second error: You are increasing the count of the “highest” number: else if(y==f) H++; – but you are NOT taking into account, what should … Read more

[Solved] Java convert string to int

Assuming you have a string of digits (e.g. “123”), you can use toCharArray() instead: for (char c : pesel.toCharArray()) { temp += 3 * (c – ‘0’); } This avoids Integer.parseInt() altogether. If you want to ensure that each character is a digit, you can use Character.isDigit(). Your error is because str.split(“”) contains a leading … Read more

[Solved] What is this inefficient sorting algorithm with two loops that compares the element at each index with all other elements and swaps if needed?

It is named Exchange sort and is sometimes confused with bubble sort. While Bubble sort compares adjacent elements, Exchange sort compares the first element with all the following elements and swaps if needed. Then it does the same for the second element and so on. 0 solved What is this inefficient sorting algorithm with two … Read more

[Solved] Difference between two dates in month in java

ZoneId defaultZoneId = ZoneId.systemDefault(); String issueDate1=”01/01/2017″; Date issueDate2=new SimpleDateFormat(“dd/MM/yyyy”).parse(issueDate1); String dateTo1=”31/12/2018″; Date dateTo2=new SimpleDateFormat(“dd/MM/yyyy”).parse(dateTo1); here year month days can find easily.This giving ans of all question. Instant instant = issueDate2.toInstant(); LocalDate localDatestart = instant.atZone(defaultZoneId).toLocalDate(); Instant instant1 = dateTo2.toInstant(); LocalDate localDateend = instant1.atZone(defaultZoneId).toLocalDate().plusDays(1); Period diff = Period.between(localDatestart, localDateend); System.out.printf(“\nDifference is %d years, %d months and %d … Read more

[Solved] Math.random() *100 verses Math.random(100) [closed]

Math.random() exists. Math.random(int) does not exist. You may be getting that mixed up with the Random class constructor, which takes a long as a seed value, meaning your results will be pseudorandom and by consequence, repeatable. If you wanted a number between 0 and 99 I actually would recommend that you use Random. You can … Read more