How can I parse a jagged array to individual variables in Java? - java

I have a jagged array that has a minimum of three elements, and I need to parse out the first five elements, filling any nulls with a space.
// there will ALWAYS be three elements
String whiconcatC = scrubbedInputArray[0];
String whiconcatD = scrubbedInputArray[1];
String whiconcatE = scrubbedInputArray[2];
// there MAY be a fourth or fifth element
if (scrubbedInputTokens > 3) {
String whiconcatF = scrubbedInputArray[3];
} else {
String whiconcatF = " ";
}
//
if (scrubbedInputTokens > 4) {
String whiconcatG = scrubbedInputArray[4];
} else {
String whiconcatG = " ";
}
While the above code does not generate errors during compile, subsequent lines referencing whiconcatF or whiconcatG will error out during compile with cannot find symbol.
I've tried using forEach and StringTokenizer (after converting the array to a delimited string), but can't figure out how to work a default value in the instance that there's no value in spots 4 & 5.
I've not been able to figure out any other way to do this nor why my if logic is failing. Suggestions?

Thats because they have local scope and are defined inside brackets. Thus the die when you close the brackets and are not reachable. Define them outside and you should be fine.
String whiconcatC = scrubbedInputArray[0];
String whiconcatD = scrubbedInputArray[1];
String whiconcatE = scrubbedInputArray[2];
String whiconcatF = "";
String whiconcatG = "";
// there MAY be a fourth or fifth element
if (scrubbedInputTokens > 3) {
whiconcatF = scrubbedInputArray[3];
} else {
whiconcatF = " ";
}
//
if (scrubbedInputTokens > 4) {
whiconcatG = scrubbedInputArray[4];
} else {
whiconcatG = " ";
}

Declare the whiconcatF outside the if-else for them to visible beyond it. Currently both the String variables are within the scope of if and else only. Once its moved above the if, it gets the method level scope(I hope this whole snippet is not within any other block), and thus you can access them anywhere in the method.
String whiconcatF = " "; // Default value
if (scrubbedInputTokens > 3) {
whiconcatF = scrubbedInputArray[3];
}
String whiconcatG = " "; // Default value
if (scrubbedInputTokens > 4) {
whiconcatG = scrubbedInputArray[4];
}
Since you have default values now, you can remove the else part for both the if.

Related

Java/Angularjs - convert variable names to normal English conventions

My goal here is to retrieve the attribute names from a class, which I have already done using JAVA Reflections. But I want to be able to transform the variable naming convention, say firstName to First Name.
My current idea is to use .split() to transform position: 0 (usually a lower-case) to Uppercase, then loop until I find subsequent UpperCases, and push a blank space in between. Are there any better way to do this?
EDIT: This is my current method if any of you are interested:
public List<String> getProfileConstraintTemplateEnglish() {
//what I want to return
List<String> transformedList = new ArrayList<>();
//The reflection that I'm getting
List<ResultProfileConstraintTemplate> tmp = constraintService.getProfileCTml();
//loop each obj in reflection list
for (ResultProfileConstraintTemplate r : tmp) {
//get the letters first from the title in obj
String[] field = r.getTitle().split("");
//this is the transformed string in each tmp.
String transformed = "";
//converting the array to a list for simpler addition.
List<String> fieldString = Arrays.asList(field);
//adding a counter to know which is the "first" position.
int counter = 0;
for (String s : fieldString) {
//first letter
if (counter == 0) {
transformed += s.toUpperCase();
}
//everything else
if (counter != 0 && s.equals(s.toUpperCase())) {
transformed+= " ";
transformed+=s;
}
else if(counter != 0 && s.equals(s.toLowerCase())){
transformed+=s;
}
//increment counter
counter++;
}
//add the transformed word to list.
transformedList.add(transformed);
}
return transformedList;
}
Result:
I think your way is the only way. If you post your code, maybe we can shed more light on the matter.
You can use isUpperCase() method and if it returns true replace it with a space and the letter and always convert first letter i.e indexOf(0) to toUpperCase().

Using regex to add a new line to a string every time each line reaches a certain length

I'm trying to ensure text does not appear outside of the window as the window size cannot be changed.
The image above shows what happens when the string of the order numbers exceeds the length of the window. I'm trying to ensure that when the length of the string of order numbers reaches a certain length, I use regex to make a new line for the next orders.
private String listOfOrders( Map<String, List<Integer> > map, String key )
{
String res = "";
if ( map.containsKey( key ))
{
List<Integer> orders = map.get(key);
for ( Integer i : orders )
{
res += " " + i + ",";
}
} else {
res = "-No key-";
}
return res;
}
}
This is the code to display the text, it works by forming the string res and filling it with the order numbers from the array list.
I found, through researching, a cool little piece of code which replaces a string every set amount of characters with itself plus a new line.
if(res.length() >= W-10)
{
res = res.replaceAll("(.{20})", "$1\n");
}
else
{
res += " " + i + ",";
}
But this has no effect at all. And I also realised that this code can not tell how long each line is because I'm using length to determine the length of each line and not how long each line is between each "\n".
My question is, how do I go about using regex to ensure each line in the string is a certain number of characters long? As my attempt does not work. The above just provides context as to why I want lines in a string a certain legnth.
Thanks!

Get Portion of String

I need to get the values after "Swap:".
I've already developed a method to get the output from a shell command so I have a string that contains everything you see in the picture but now from the string I want to get ONLY the value after the Swap: How can i do this? These value are variable and can be even all three 0.
Let's say you have the text stored in a String called textContent. Assuming the Swap-line is the last part of your String, then you could do something like this:
int index = textContent.indexOf("Swap:");
index += "Swap:".length();
textContent.subString(index);
Try this:
String[] stringParts = text.substring(text.indexOf("Swap:") + 5).trim().split("( )+");
int[] parts = new int[stringParts.length];
for (int i = 0; i < stringParts.length; i++)
parts[i] = Integer.parseInt(stringParts[i]);
It will fill an integer array will the values after the "Swap" part.
Since you have already stored the output of the shell command, you simply need to do some string manipulation to search and extract the relevant information. The following particular string manipulation methods might be of interest to you: trim(), indexOf(), and substring().
Below is a simple example code on how to extract the value under the total's column using the above String methods:
public class ShellOutput {
public ShellOutput() {
final String extract = "Swap:"; // the keyword to search
String shellOutput = "Swap: 75692 29657 0"; // your shell output
int position = shellOutput.indexOf(extract); // get the position of the Swap: text
if (position != -1) {
String swapLine = shellOutput.substring(position + extract.length()); // remove everything except the swap line
String numbers = swapLine.trim(); // assuming they are spaces, otherwise do some operations to remove tabs if used
int firstSpace = numbers.indexOf(' '); // get the first space or change to a tab character if it is used
String totalNumber = numbers.substring(0, firstSpace); // remove up to the first found after the number
System.out.println("Total = " + totalNumber);
} else {
System.out.println("No '" + extract + "' segment found.");
}
}
public static void main(String[] args) {
new ShellOutput();
}
}
Output: Total = 75692

Java codingbat help - withoutString

I'm using codingbat.com to get some java practice in. One of the String problems, 'withoutString' is as follows:
Given two strings, base and remove, return a version of the base string where all instances of the remove string have been removed (not case sensitive).
You may assume that the remove string is length 1 or more. Remove only non-overlapping instances, so with "xxx" removing "xx" leaves "x".
This problem can be found at: http://codingbat.com/prob/p192570
As you can see from the the dropbox-linked screenshot below, all of the runs pass except for three and a final one called "other tests." The thing is, even though they are marked as incorrect, my output matches exactly the expected output for the correct answer.
Here's a screenshot of my output:
And here's the code I'm using:
public String withoutString(String base, String remove) {
String result = "";
int i = 0;
for(; i < base.length()-remove.length();){
if(!(base.substring(i,i+remove.length()).equalsIgnoreCase(remove))){
result = result + base.substring(i,i+1);
i++;
}
else{
i = i + remove.length();
}
if(result.startsWith(" ")) result = result.substring(1);
if(result.endsWith(" ") && base.substring(i,i+1).equals(" ")) result = result.substring(0,result.length()-1);
}
if(base.length()-i <= remove.length() && !(base.substring(i).equalsIgnoreCase(remove))){
result = result + base.substring(i);
}
return result;
}
Your solution IS failing AND there is a display bug in coding bat.
The correct output should be:
withoutString("This is a FISH", "IS") -> "Th a FH"
Yours is:
withoutString("This is a FISH", "IS") -> "Th a FH"
Yours fails because it is removing spaces, but also, coding bat does not display the correct expected and run output string due to HTML removing extra spaces.
This recursive solution passes all tests:
public String withoutString(String base, String remove) {
int remIdx = base.toLowerCase().indexOf(remove.toLowerCase());
if (remIdx == -1)
return base;
return base.substring(0, remIdx ) +
withoutString(base.substring(remIdx + remove.length()) , remove);
}
Here is an example of an optimal iterative solution. It has more code than the recursive solution but is faster since far fewer function calls are made.
public String withoutString(String base, String remove) {
int remIdx = 0;
int remLen = remove.length();
remove = remove.toLowerCase();
while (true) {
remIdx = base.toLowerCase().indexOf(remove);
if (remIdx == -1)
break;
base = base.substring(0, remIdx) + base.substring(remIdx + remLen);
}
return base;
}
I just ran your code in an IDE. It compiles correctly and matches all tests shown on codingbat. There must be some bug with codingbat's test cases.
If you are curious, this problem can be solved with a single line of code:
public String withoutString(String base, String remove) {
return base.replaceAll("(?i)" + remove, ""); //String#replaceAll(String, String) with case insensitive regex.
}
Regex explaination:
The first argument taken by String#replaceAll(String, String) is what is known as a Regular Expression or "regex" for short.
Regex is a powerful tool to perform pattern matching within Strings. In this case, the regular expression being used is (assuming that remove is equal to IS):
(?i)IS
This particular expression has two parts: (?i) and IS.
IS matches the string "IS" exactly, nothing more, nothing less.
(?i) is simply a flag to tell the regex engine to ignore case.
With (?i)IS, all of: IS, Is, iS and is will be matched.
As an addition, this is (almost) equivalent to the regular expressions: (IS|Is|iS|is), (I|i)(S|s) and [Ii][Ss].
EDIT
Turns out that your output is not correct and is failing as expected. See: dansalmo's answer.
public String withoutString(String base, String remove) {
String temp = base.replaceAll(remove, "");
String temp2 = temp.replaceAll(remove.toLowerCase(), "");
return temp2.replaceAll(remove.toUpperCase(), "");
}
Please find below my solution
public String withoutString(String base, String remove) {
final int rLen=remove.length();
final int bLen=base.length();
String op="";
for(int i = 0; i < bLen;)
{
if(!(i + rLen > bLen) && base.substring(i, i + rLen).equalsIgnoreCase(remove))
{
i +=rLen;
continue;
}
op += base.substring(i, i + 1);
i++;
}
return op;
}
Something things go really weird on codingBat this is just one of them.
I am adding to a previous solution, but using a StringBuilder for better practice. Most credit goes to Anirudh.
public String withoutString(String base, String remove) {
//create a constant integer the size of remove.length();
final int rLen=remove.length();
//create a constant integer the size of base.length();
final int bLen=base.length();
//Create an empty string;
StringBuilder op = new StringBuilder();
//Create the for loop.
for(int i = 0; i < bLen;)
{
//if the remove string lenght we are looking for is not less than the base length
// and the base substring equals the remove string.
if(!(i + rLen > bLen) && base.substring(i, i + rLen).equalsIgnoreCase(remove))
{
//Increment by the remove length, and skip adding it to the string.
i +=rLen;
continue;
}
//else, we add the character at i to the string builder.
op.append(base.charAt(i));
//and increment by one.
i++;
}
//We return the string.
return op.toString();
}
Taylor's solution is the most efficient one, however I have another solution that is a naive one and it works.
public String withoutString(String base, String remove) {
String returnString = base;
while(returnString.toLowerCase().indexOf(remove.toLowerCase())!=-1){
int start = returnString.toLowerCase().indexOf(remove.toLowerCase());
int end = remove.length();
returnString = returnString.substring(0, start) + returnString.substring(start+end);
}
return returnString;
}
#Daemon
your code works. Thanks for the regex explanation. Though dansalmo pointed out that codingbat is displaying the intended output incorrectly, I through in some extra lines to your code to unnecessarily account for the double spaces with the following:
public String withoutString(String base, String remove){
String result = base.replaceAll("(?i)" + remove, "");
for(int i = 0; i < result.length()-1;){
if(result.substring(i,i+2).equals(" ")){
result = result.replace(result.substring(i,i+2), " ");
}
else i++;
}
if(result.startsWith(" ")) result = result.substring(1);
return result;
}
public String withoutString(String base, String remove){
return base.replace(remove,"");
}

How to add two string array members into one string variable

I've been trying to figure out how to add an extra string array member to a string variable with no luck. Here is the code.
myDirString = myDirString.trim();
String[] myDirStringParts = myDirString.split(" +");
MySize = myDirStringParts[0];
MyNum = myDirStringParts[1];
Total = myDirStringParts[2];
MyName = myDirStringParts[3];
Basically I want myDirStringParts[2]; to also be included into MyName.
MyName = myDirStringParts[3] + myDirStringParts[2];
will work.
Simply
MyName = myDirStringParts[3] + myDirStringParts[2];
should do the trick.
However, i notice a few things about your code that i would like to point out:
Declare the variables MyName, Total, MyNum, MySize.
Make sure that myDirStringParts contains atleast 3 elements after the split(" +") call.
You can do this by the following code snippet:
if(myDirStringParts.length >= 4) {
MySize = myDirStringParts[0];
MyNum = myDirStringParts[1];
Total = myDirStringParts[2];
MyName = myDirStringParts[3] + myDirStringParts[2];
} else {
// print out an error message.
System.err.println("myDirString does not contain all the required data!");
}

Categories