[Solved] Renaming a variable in a string format

You need to isolate your variable. How about a reg-ex? return source.replaceAll(“(\\W)(” + var2Rename + “)(\\W)”, “$1” + newName + “$3”); Explanation. The \\W will check for non-letter characters, eg the boundary of a variable expression. We want a boundary on both sides of the variable, and then to replace we need to make sure … Read more

[Solved] My validation method is not working, where am i going wrong?

public static String checkUserInputSeriesName(Scanner sc, SeriesLibrary seriesLibrary){ boolean validInput = false; String seriesName = null; do{ validInput = false; seriesName=sc.nextLine(); for(int i = 0; i < seriesLibrary.getTvSeries().size(); i++){ if(seriesName.equals(seriesLibrary.getTvSeries().get(i))){ validInput = true; } } if(!validInput){ System.out.println(“That Series does not exist, please try again!”); sc.nextLine(); } }while(!validInput); return seriesName; } solved My validation method is not … Read more

[Solved] Take only the first hashmap value [closed]

Try this. public static void main(String[] args) { HashMap<String, Object> map = new HashMap<>(); map.put(“one”, “壱”); map.put(“two”, “弐”); map.put(“three”, “参”); map.put(“four”, “四”); System.out.println(“hash map = ” + map); Object firstValue = map.values().iterator().next(); System.out.println(“first value = ” + firstValue); } output: hash map = {four=四, one=壱, two=弐, three=参} first value = 四 solved Take only the … Read more

[Solved] Remove duplicate phrases [closed]

Try this. static String removeDuplicatePhrase(String s1, String s2) { s1 = s1.trim(); s2 = s2.trim(); List<String> list1 = List.of(s1.split(“\\s+”)); List<String> list2 = List.of(s2.split(“\\s+”)); int size1 = list1.size(), size2 = list2.size(); int i = Math.min(size1, size2); for (; i > 0; –i) if (list1.subList(size1 – i, size1).equals(list2.subList(0, i))) break; return String.join(” “, list1) + ” ” … Read more

[Solved] How do I set the variable to an incoming argument in java?

First, pass Address class as parameter. void setAddress(Address s) Since Address in a static nested class, but you have declared Address as String in your Problem3_1 class file. So you will need to concat the values manually like void setAddress(Address s) { //address = s; address = s.number + ” , ” + s.street; } … Read more

[Solved] Limit number guessing game to three attempts

Simply count the number of attempts and exit the loop once it reaches the threshold. Like that: int attemptsNum = 0; final int maxAttempts = 3; do { System.out.print(“Guess a number between 1 and 10: “); user = input.nextInt(); if (user > comp) System.out.println(“My number is less than ” + user + “.”); else if … Read more

[Solved] How to get a string before character java [closed]

When you know what String you are looking for, you can try the following. String s=”description: John Newman Logged on: 03.26.2018 9:26:29″; String log1 = null; if(s.toLowercase().contains(“logged on”)){ log1 = “Logged on”; // Your desired string }else if(s.toLowercase().contains(“logged off”)){ log1 = “Logged off”; // Your desired string } or Simply String log1 = (s.toLowercase().contains(“logged on”)) … Read more

[Solved] How can I make a list with alphabetical scrolling?

Open activity_main.xml file in res/layout and copy the following content. <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” xmlns:tools=”http://schemas.android.com/tools” android:layout_width=”match_parent” android:layout_height=”match_parent” android:orientation=”horizontal” android:paddingLeft=”5dp” tools:context=”.MainActivity” android:baselineAligned=”false” > <ListView android:id=”@+id/list_fruits” android:layout_width=”0dp” android:layout_height=”wrap_content” android:layout_weight=”1″ android:paddingRight=”5dp” > </ListView> <LinearLayout android:id=”@+id/side_index” android:layout_width=”50dp” android:layout_height=”fill_parent” android:background=”#c3c3c3″ android:gravity=”center_horizontal” android:orientation=”vertical” > </LinearLayout> Create a new side_index_item.xml file in res/layout and copy the following content. <?xml version=”1.0″ encoding=”utf-8″?> <TextView xmlns:android=”http://schemas.android.com/apk/res/android” android:id=”@+id/side_list_item” … Read more

[Solved] Is it possible to have a private class? [closed]

Since you start with Java – on the first degrees on your learning curve you will eventually not need any private classes. They make sense for inner classes (a class within a class) when this inner class is not usable/useful outside the parent class. But for now: just keep in mind, that private is not … Read more

[Solved] Remove objects from list based on equal property and comparison of time/date [closed]

Assuming you have the corresponding getters and setters, you may try something like below: List<Person> unique = persons.stream() .collect(Collectors.groupingBy(Person::getCustomerNumber)) //returns a Map<String,List<Person>> with customerNumber as key .values() .stream() // stream and sort each list .map(e-> e.stream().sorted( Comparator.comparing(Person::getBirthday) .thenComparing(Person::getBirthTime)) .findFirst().get()) // map to first Person obj .collect(Collectors.toList()); unique.forEach(System.out::println); 1 solved Remove objects from list based on … Read more

[Solved] How to Convert Date to Int? [closed]

String year1= String.valueOf (year); String month1= String.valueOf(month); String day1= String.valueOf(day); must be changed to String year1= String.valueOf (num1); String month1= String.valueOf(num2); String day1= String.valueOf(num3); because these 3 variables only hold the values entered by the user. Also, not sure why you need the hour, because you never get that as inout from the user. In … Read more