Getting highest available string in java - string

I want to get the highest available string value in java how can i achieve this.
Example: hello jameswangfron
I want to get the highest string "jameswangfron"
String Text = request.getParameter("hello jameswangfron");
Please code example.

public class HelloWorld{
public static void main(String []args){
String text = "hello jameswangfron";
String[] textArray = text.split(" ");
String biggestString = "";
for(int i=0; i<textArray.length; i++){
if(i==0) {
textArray[i].length();
biggestString = textArray[i];
} else {
if(textArray[i].length()>textArray[i-1].length()){
biggestString = textArray[i];
}
}
}
System.out.println("Biggest String : "+biggestString);
}
}
And it shows the output as
Biggest String : jameswangfron

Maybe this will be easyer to understand
public class HelloWorld {
public static void main(String[] args) {
System.out.println(StringManipulator.getMaxLengthString("hello jameswangfron", " "));
}
}
class StringManipulator{
public static String getMaxLengthString(String data, String separator){
String[] stringArray = data.split(separator);
String toReturn = "";
int maxLengthSoFar = 0;
for (String string : stringArray) {
if(string.length()>maxLengthSoFar){
maxLengthSoFar = string.length();
toReturn = string;
}
}
return toReturn;
}
}
But there is a catch. If you pay attention to split method from class String, you will find out that the spliter is actually a regex. For your code, i see that you want to separate the words (which means blank space). if you want an entire text to search, you have to pass a regex.
Here's a tip. If you want your words to be separated by " ", ".", "," (you get the ideea) then you should replace the " " from getMaxLengthString method with the following
"[^a-zA-Z0-9]"
If you want digits to split up words, simply put
"[^a-zA-Z]"
This tells us that we use the separators as anything that is NOT a lower case letter or upper case letter. (the ^ character means you don't want the characters you listed in your brackets [])
Here is another way of doing this
"[^\\w]"
\w it actually means word characters. so if you negate this (with ^) you should be fine

Related

how to fix a double substring call?

in my homework I need to extract the server name from the url
at the same time, I need to take into account that there may not be a slash after the server name
I'm not allowed to use a loop
At the same time, I am once again trying to redo a remark from my teacher:
"Now substring can be done twice (if it goes into if). You need to make sure that only one substring is made for any variant of the function execution"
how can this be fixed? I've tried everything
public class Url {
public static String getServerName(String url) {
int index1 = url.indexOf("://") + 3;
String serverName = url.substring(index1);
int index2 = serverName.indexOf("/");
if (index2 >= 0) {
return url.substring(index1, index1 + index2);
}
return serverName;
}
public static void main(String[] args) {
String url = "https://SomeServerName";
System.out.println(getServerName(url));
}
}

Please I have A playfair cipher, it converts String inputs to Int, but I want Hex

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map;
This is the public class
public class Process {
private String keywordAsString = "";
private String keyword = "";
// ArrayList to hold the letters of the keyword with duplicates removed.
private ArrayList<Integer> keywordAsIntsNoDup = new ArrayList<Integer>(0);
// Map for removing all duplicate letters in the keyword.
private Map<Integer, Integer> keywordLetters = new LinkedHashMap<Integer, Integer>(0);
// ArrayList to hold all 256 ASCII characters (as integers).
private ArrayList<Integer> asciiArray = new ArrayList<Integer>(0);
// ArrayList for storing the message from the file.
ArrayList<Integer> fileMessageAsInteger = new ArrayList<Integer>(0);
// Constructor
public void process() {
}
public void processKeyword(String keyword) {
// Copy incoming keyword String
this.keywordAsString = keyword;
// Pass incoming keyword String to the removeDuplicate method.
// removeDuplicate will first convert the letters to Integers,
// then remove any duplicate letters.
// Store the result in the keywordAsIntsNoDup ArrayList
this.keywordAsIntsNoDup = removeDuplicates(this.keywordAsString);
// Create ArrayList and fill it with all 256 ASCII characters (as integers).
createAsciiArr();
// Remove the keyword letters from the asciiArray.
for (int i=0; i<this.keywordAsIntsNoDup.size(); i++) {
Integer letterToSearchFor = this.keywordAsIntsNoDup.get(i);
if (this.asciiArray.contains(letterToSearchFor))
{
this.asciiArray.remove(letterToSearchFor);
}
}
}// END processKeyword()
public ArrayList<Integer> removeDuplicates(String keyword) {
// Copy incoming keyword String
this.keyword = keyword;
I really would appreciate if someone would help me Java is really no piece of cake.
// Loop through the keywordAsIntArray ArrayList, putting each 'letter' of the keyword into the map.
// Duplicate letters will be overridden, so the map will contain the keyword without any duplicates.
for (int i=0; i
// Put the maps' key set (which holds the 'letters') into an ArrayList.
// This will make it easier to put the 'letters' into the Table later.
ArrayList<Integer> keyslist = new ArrayList<Integer>(this.keywordLetters.keySet());
System.out.println("\n" + "map.keySet() from keyslist ArrayList = " + keyslist.toString());
return keyslist;
}
public void createAsciiArr() {
// Use an enhanced for loop to fill the asciiArray ArrayList
// with all 256 ASCII characters as integers.
for (int i=0; i<256; i++) {
this.asciiArray.add(i);
}
}// END createAsciiArr()
}// END class
Please I want to input String as keyword, then get back hex values as the encrypted code and not integers. Also Please I have more of the codes I dont really understand,am really new to Java. Please can anyone help me.

If Else: String Equality (Java)

Lab Description : Compare two strings to see if each of the two strings contains the same letters in the
same order.
This is what I have so far far:
import static java.lang.System.*;
public class StringEquality
{
private String wordOne, wordTwo;
public StringEquality()
{
}
public StringEquality(String one, String two)
{
setWords (wordOne, wordTwo);
}
public void setWords(String one, String two)
{
wordOne = one;
wordTwo = two;
}
public boolean checkEquality()
{
if (wordOne == wordTwo)
return true;
else
return false;
}
public String toString()
{
String output = "";
if (checkEquality())
output += wordOne + " does not have the same letters as " + wordTwo;
else
output += wordOne + " does have the same letters as " + wordTwo;
return output;
}
}
My runner looks like this:
import static java.lang.System.*;
public class StringEqualityRunner
{
public static void main(String args[])
{
StringEquality test = new StringEquality();
test.setWords(hello, goodbye);
out.println(test);
}
}
Everything is compiling except for the runner. It keeps saying that hello and goodbye aren't variables. How can I fix this so that the program does not read hello and goodbye as variables, but as Strings?
You need to quote strings otherwise they are treated as variables.
"hello"
"goodbye"
so this would work better.
test.setWords("hello", "goodbye");
Problem with your code is with checkEquality(), you are comparing the string's position in memory when you use == use .equals() to check the string
public boolean checkEquality()
{
if (wordOne == wordTwo) //use wordOne.equals(wordTwo) here
return true;
else
return false;
}
Enclose them in double-quotes.

How to convert from ArrayList to String?

After compiling an ArrayList in java, how do I print it as a string?
Using ArrayList.toString() gives the values with brackets around them and commas between them.
I want to print them without brackets and only spaces between them.
(Assuming Java)
You can write your own method to do that:
public static <T> String listToString(List<T> list) {
StringBuilder sb = new StringBuilder();
boolean b = false;
for (T o : list) {
if (b)
sb.append(' ');
sb.append(o);
b = true;
}
return sb.toString();
}
Or, if you're using Guava, you can use Joiner:
Joiner.on(' ').join(list)
Similarly, if you just are interested in printing, you can avoid creating a new string all together:
public static <T> void printList(List<T> list) {
for (T o : list) {
System.out.print(o);
System.out.print(' ');
}
System.out.println();
}
If you're using Eclipse Collections, you can use the makeString() method.
ArrayList<String> list = new ArrayList<String>();
list.add("one");
list.add("two");
list.add("three");
Assert.assertEquals(
"one two three",
ArrayListAdapter.adapt(list).makeString(" "));
If you can convert your ArrayList to a FastList, you can get rid of the adapter.
Assert.assertEquals(
"one two three",
FastList.newListWith("one", "two", "three").makeString(" "));
Note: I am a committer for Eclipse collections.
for c#
string.Join(" ", _list);
Not sure what language you're using, but try either:
ArrayList.join()
or
ArrayList.toArray().join()
for(int i = 0; i < arraylist.size(); i++){
System.out.print(arraylist.get(i).toString + " ");
}
???

Check a string for containing a list of substrings

How can I check a specific string to see if it contains a series of substrings?
Specifically something like this:
public GetByValue(string testString) {
// if testString contains these substrings I want to throw back that string was invalid
// string cannot contain "the " or any part of the words "College" or "University"
...
}
If performance is a concern, you may want to consider using the RegexStringValidator class.
You can use string.Contains() method
http://msdn.microsoft.com/en-us/library/dy85x1sa.aspx
// This example demonstrates the String.Contains() method
using System;
class Sample
{
public static void Main()
{
string s1 = "The quick brown fox jumps over the lazy dog";
string s2 = "fox";
bool b;
b = s1.Contains(s2);
Console.WriteLine("Is the string, s2, in the string, s1?: {0}", b);
}
}
/*
This example produces the following results:
Is the string, s2, in the string, s1?: True
*/
This an interesting question. As #Jon mentioned, a regular expression might be a good start because it will allow you to evaluate multiple negative matches at once (potentially). A naive loop would be much less efficient in comparison.
You can check it as follow....
class Program
{
public static bool checkstr(string str1,string str2)
{
bool c1=str1.Contains(str2);
return c1;
}
public static void Main()
{
string st = "I am a boy";
string st1 = "boy";
bool c1=checkstr(st,st1);
//if st1 is in st then it print true otherwise false
System.Console.WriteLine(c1);
}
}
Decided against checking for strings to limit my data return, instead limited my return to a .Take(15) and if the return count is more than 65,536, just return null

Resources