I have executed the below java script in the informatica java transformation but I'm getting the error: [ERROR]java.lang.ArrayIndexOutOfBoundsException:1
String [] Name_parsed;
String Name_delimiter = "&";
String Name_li = Name;
int Name_length = Name_li.length();
for (int i=0; i < Name_length; i++)
{
Name_parsed = Name.split(Name_delimiter);
o_Name =Name_parsed[0];
generateRow();
o_Name =Name_parsed[1];
generateRow();
}
Chances are, your input string has fewer than 1 & character. Try printing the Name variable to see if this is the case. You can always wrap this in a try/catch block to handle these cases.
The error mean that you have array that length is lower then 1.
This mean that Name.split(Name_delimiter) return array with only one element.
and when you try to access to index 1 here o_Name =Name_parsed[1]; it can not found it.
Validate your input data.
If there can be variable number of "&"(s) in the Name field, you should do it like this:
String [] Name_parsed;
String Name_delimiter = "&";
String Name_li = Name;
int Name_length = Name_li.length();
for (int i=0; i < Name_length; i++)
{
Name_parsed = Name.split(Name_delimiter);
for (int j=0; j<Name_parsed.length; j++){
{
o_Name =Name_parsed[j];
generateRow();
}
}
Related
this is a lab for class I'm trying to do. Here's the instructions:
Write a program that takes in a line of text as input, and outputs that line of text in reverse. The program repeats, ending when the user enters "Done", "done", or "d" for the line of text.
Ex: If the input is:
"Hello there
Hey
done"
the output is:
"ereht olleH
yeH"
And here's what I have right now:
public class LabProgram {
public static void main(String[] args) {
/* Type your code here. */
Scanner scnr = new Scanner(System.in);
String[] inputs = new String[100];
String input;
int i = 0;
while (true) {
input = scnr.nextLine();
if(input.equals("Done") || input.equals("done") || input.equals("d"))
break;
inputs[i] = input;
i++;
}
for (int j = 0; j < i; j++) {
int length = inputs[j].length();
String reverse = "";
for (int k = length - i; k >= 0; k--) {
reverse = reverse + inputs[j].charAt(k);
}
System.out.print("\n" + reverse);
}
}
}
Current output
What am I doing wrong??
Iterate through the array, and reverse elements at every index.
This solution is time consuming but does your job
for (int j = 0; j < inputs.lenght; j++) {
int length = inputs[j].length();
char a;
String rev = "";
for(int i =0; i< length; i++){
a = inputs[j].charAt(i);
rev = a + rev;
}
System.out.println(rev);
}
*Try to use StringBuilder And use method reverse -- #Artur Todeschini
To add to what Artur said, an ArrayList of StringBuilders could do the trick quite well:
for(StringBuilder nextEntry : stringBuilderList)
{
nextEntry.reverse();
}
The enhanced for-loop will go through each entry in the ArrayList, and the StringBuilder's reverse will change the order of the letters.
EDIT TO SHOW FORMATTING
ArrayList<StringBuilder> stringBuilderList= new ArrayList<>();
*note. given that this is for a lab, its probably for learning purposes and using built-in classes that does all the work for you are usually not the intended solution. -- #experiment unit 1998X
Try to use StringBuilder
And use method reverse
This is another "ArrayList and StringBuilder-less" version.
Create two Strings, one filled and one empty:
String nextString = stringArray[i],
template = new String();
Loop through the length of the String, adding the next character in from the end each time through.
int length = nextString.length() - 1;
for(int j = 0; j < length; j++)
{
template += nextString.charAt(length - j);
}
Add the whole String to the String array's index
stringArray[i] = template;
NOTE
This is an inner loop for a String array and is NOT complete code
Please help with code. I have tried adding substring, etc, to remove the end comma. How can I do this?
Example: {1,2,3} to string is now "1,2,3,"
public static String arrayToString(int[] numbers)
{
String stringify = "";
for (int index = 0; index < numbers.length; index++)
{
stringify = numbers[index] + ", ";
}
return stringify;
}
Best way
Delete the entire method. Use Arrays.toString(int[]) instead. Why rewrite core functionality like this?
Stream way
return Arrays.stream(numbers)
.boxed()
.map(String::valueOf)
.collect(Collectors.joining(", "));
StringBuilder way
StringBuilder out = new StringBuilder();
for (int i = 0; i < numbers.length; i++) {
if (i != 0) out.append(", ");
out.append(numbers[i]);
}
return out.toString();
Adding to a string in a loop is bad form (it wastes a ton of memory), so don't do that. The above strategies are in rough order of preference, with the top option miles ahead of the rest.
You can modify your method to the following one:
public static String arrayToString(int[] numbers)
{
String stringify = "";
for (int index = 0; index < numbers.length; index++)
{
stringify = numbers[index];
if (index < numbers.length - 1) {
stringify += ", ";
}
}
return stringify;
}
I prefer to view this as having a separator before every number except for the first. Or, equally, that there's a separator before every number, but the first separator is empty.
public static String arrayToString(int[] numbers)
{
String separator = "";
String stringify = "";
for (int index = 0; index < numbers.length; index++)
{
stringify += separator + numbers[index];
separator = ", "
}
return stringify;
}
Note, I fixed the assignment to stringify to be += rather than =, since otherwise you're just getting the last number.
In practice, I'd probably use a StringBuilder, but I wanted to leave this as close to the original as possible. With a StringBuilder, the same separator technique can be used.
Given string is String[] s1={"Project1"} Using for loop I want to change s1 value from "Project1" into "Project2" on my next iteration. As the loop continuous the project value should get increment.Can someone help to solve this problem as am new to java and selenium.
You can use an int in conjunction with string concatenation:
for (int i = 1; i <= 10; i++) {
String projectName = "Project" + i;
// use projectName here...
}
String[] s1={"Project1"};
for(int i=2; i<n;i++) // n is how many you have to increase the project value
{
s1[0]= "Project"+String.valueOf(i);
System.out.println(s1[0]);
}
on 1st iteration i will print Project2
2nd iteration Project3
2nd iteration Project4..........
try this:
String str = "Project";
int HighestNumber; //set the bound for the number of projects you want
for(int i = 0;i<HighestNumber; i++)
{
System.out.println(str + toString(i));
}
in case you don't want to set a limit you can use
int i = 0; // or 1
while(true)
{
i++;
System.out.println(str + toString(i));
}
So basically this is how my code looked like
public static void printPrime(int[] arr)
{
int len = arr.length;
for(int i = 0; i < len; i++)
{
int c = countFactor(arr[i]);
if(c == 2)
{
System.out.print(arr[i] + ",");
}
}
}
So the output did have the 'comma' in the end. I tried looking around to remove the last comma some answers say to print last element separately but that can only happen when output depends on the for loop and not the if condition.
But as you can see I don't know how many elements I am going to get from the if condition. Only two things I can think of, to add another loop or use String then substr to output.
So I converted it to String
public static void printPrime(int[] arr)
{
int len = arr.length;
String str = "";
for(int i = 0; i < len; i++)
{
int c = countFactor(arr[i]);
if(c == 2)
{
str = str + arr[i] + ",";
}
}
str = str.substring(0, str.length()-1);
System.out.println(str);
}
My question is about knowing the optimum way (converting to string then substringing it?) for similar questions or could there be a better way as well? That I seem to be missing.
You don't have to construct a string. Consider the following slight tweaks:
public static void printPrime(int[] arr)
{
int len = arr.length;
String sep = ""; // HERE
for(int i = 0; i < len; i++)
{
int c = countFactor(arr[i]);
if(c == 2)
{
System.out.print(sep); // HERE
sep = ",";
System.out.print(arr[i]);
}
}
}
Print the delimiter first, and store its value in a variable: the first time it's printed, it will print the empty string. Thereafter, it prints the comma.
Whatever means you use should operate correctly for an empty array (length 0), a singleton array (length 1) and a long array (a large length).
Adding the comma then removing it requires special case handling for the empty array case. So you must have conditional code (an if statement) whatever you do.
I am working on a solution to the TSP problem. I have generated all the permutations of the String "123456", however, I need to convert this into an ArrayList of Integer like this [1,2,3,4,5,6]...[6,5,4,3,2,1]. I then store this into an ArrayList of ArrayLists. Once there I will be able to compare all of the cities that need to be traveled to.
When I run my code, I have a method to generate the permutation, then a method to change that permutation into an ArrayList of Integer. When I convert them, I get the exception java.lang.NumberFormatException: For input string: "". I don't know of any other way to get the String to Integer
Here is my code.
public static String permute(String begin, String string){
if(string.length() == 0){
stringToIntArray(begin+string);
return begin + string + " ";
}
else{
String result = "";
for(int i = 0; i < string.length(); ++i){
String newString = string.substring(0, i) + string.substring(i+1, string.length());;
result += permute(begin + string.charAt(i), newString);
}
stringToIntArray(result);
return result;
}
}
public static void stringToIntArray(String s){
ArrayList<Integer> perm = new ArrayList<Integer>();
String [] change = s.split("");
for(int i = 0; i < 7; ++i){
int integer = Integer.parseInt(change[i]);
System.out.println(integer);
}
}
public static void main(String[] args) {
permute("", "123456");
}
These lines
String [] change = s.split("");
for(int i = 0; i < 7; ++i){
int integer = Integer.parseInt(change[i]);
System.out.println(integer);
}
Given a String like "12345", when you split it on nothing, it will separate every character. Giving you an array with ["","1","2","3","4","5"]. Since the empty String "" is not a Number, you will get the NumberFormatException. You could change your index i to start at 1 so as to ignore that first empty String.
The split method, when splitting on "", produces an empty string as the first element of the array, so you need to start iterating from i = 1.
Also, it would be safer to stop the iteration at change.length to make sure you process all characters when there are more than 6, and don't go out of bounds if there are fewer.
String [] change = s.split("");
for(int i = 1; i < change.length; ++i){ // ignore first element
int integer = Integer.parseInt(change[i]);
System.out.println(integer);
}