How do i read a file and determine the # of array elements without having to look at the text file itself?
String temp = fileScan.toString();
String[] tokens = temp.split("[\n]+");
// numArrayElements = ?
Use the length property of the array:
int numArrayElements = tokens.length;
The proper expression is tokens.length. So, you can assign numArrayElements like this:
int numArrayElements = tokens.length;
This counts the number of elements in the tokens array. You can count the number of elements in any array in the same way.
Related
I am writing program to eliminate vowels in a String. What I want to do is check if the value of the character in the string is a Vowel. If it is false I want to store it in another array as follows:
if(isVowel(char_str[i]) == false) {
temp[index] = char_str[i];
index = index + 1;
}
I get an array out of Bounds exception for the 2nd line. I have initialised both the arrays as follows:
String str="Education";
char char_str[]=str.toCharArray();
char temp[] = {};
Can someone explain exactly what I am doing that is causing the error. I am a bit out of touch with the working of arrays in Java.
You cannot add an element to an array like this without specifying the array length. You can use ArrayList instead to add new elements without specifying the length. So either initialize the array length like this -
char temp[] = new char[100]; // Assuming 100 is the highest length
or declare an ArrayList like this -
List<Character> temp = new ArrayList<Character>();
You can add a new element to an ArrayList by using the add method.
temp.add(char_str[i]);
You are attempting to access an element of an empty array.
This line
char temp[] = {};
Creates an empty array - one with no elements.
So this
temp[index] = ...
Will explode because there are no positions to assign a value to.
Allocate some space, eg:
char temp[] = new char[str.length()];
Hi I am just starting to learn java and i am stuck on a problem.
The problem states that if we have a string S
S = "123:456:789"
We have to extract the numbers 123 ,456,789 separately and store them in different variables such as
int a=123
Int b=456
Int c=789
How can we do that?
You can split them by the : character and then save parse the Strings and save them in an array as follows:
String S = "123:456:789";
String[] arr = S.split(":");
int[] integers = new int[arr.length];
for(int i = 0; i < arr.length; i++)
integers[i] = Integer.parseInt(arr[i]);
You can split the string based on a delimiter into a string array. Once you have the string array you can access each array's element to get the specific values.
String S = "123:456:789"
String[] example = S.split(":");
Source: https://javarevisited.blogspot.com/2017/01/how-to-split-string-based-on-delimiter-in-java.html
Look at the method split() in String, and into Integer.parseInt().
You also need to look into Regular Expressions
I need your help. I have a string with value "1,2,3,4,5,6,7,8,9,10".
What I want to do is take once first value ("1") only (substring (0, 1) for example) and then do a loop with the rest of values except the first value that I already take.
Maybe I have to create another String variable and set the values without first value to the second String variable and then create a loop? How to do that?
The easiest way would probably be to use String#split(String):
String str = "1,2,3,4,5,6,7,8,9,10";
String[] parts = str.split(",");
// Save the first part
String firstPart = parts[0];
// Iterate over the others:
for (int i = 1; i < parts.length; ++i) {
System.out.println (parts[i]); // Or do something useful with it
}
You can use split function.
String numbers = "1,2,3,4,5,6,7,8,9,10"; //Here your String
String[] array = numbers.split(","); //Here you divide the String taking as reference the ,
String number = array[0] //You will get the number 1
If you want to take the rest of the elements:
for(i = 1; i < array.length; i++)
System.out.println(array[i]);
I expect it will be helpful for you!
I am trying to take the info from a string array that each string in the array is a csv, like so:
String[] jobs = {
"2,-8,4",
"10,-10,9,-3",
"9,-1"
}
I know how to take csv from a file, but i dont understand how to take these values from the string and get their int forms amd put them into an array. I was thinking i can all just put them in 1 array.
All you really need to do is split the strings and parse to int.
List<Integer> ints = new ArrayList<Integer>();
// For each element in jobs array
for (int i = 0; i < jobs.length; i++)
// For each csv in current element
for (String s : jobs[i].split(","))
ints.add(Integer.parseInt(s)); // parse and add to ints
for (int i : ints)
System.out.println(i);
hello every one i got a string from csv file like this
LECT-3A,instr01,Instructor 01,teacher,instr1#learnet.com,,,,male,phone,,
how to split this string with comma i want the array like this
s[0]=LECT-3A,s[1]=instr01,s[2]=Instructor 01,s[3]=teacher,s[4]=instr1#learnet.com,s[5]=,s[6]=,s[7]=,s[8]=male,s[9]=phone,s[10]=,s[11]=
can anyone please help me how to split the above string as my array
thank u inadvance
- Use the split() function with , as delimeter to do this.
Eg:
String s = "Hello,this,is,vivek";
String[] arr = s.split(",");
you can use the limit parameter to do this:
The limit parameter controls the number of times the pattern is applied and therefore affects the length of the resulting array. If the limit n is greater than zero then the pattern will be applied at most n - 1 times, the array's length will be no greater than n, and the array's last entry will contain all input beyond the last matched delimiter. If n is non-positive then the pattern will be applied as many times as possible and the array can have any length. If n is zero then the pattern will be applied as many times as possible, the array can have any length, and trailing empty strings will be discarded.
Example:
String[]
ls_test = "LECT-3A,instr01,Instructor 01,teacher,instr1#learnet.com,,,,male,phone,,".split(",",12);
int cont = 0;
for (String ls_pieces : ls_test)
System.out.println("s["+(cont++)+"]"+ls_pieces);
output:
s[0]LECT-3A
s[1]instr01
s[2]Instructor 01
s[3]teacher
s[4]instr1#learnet.com
s[5]
s[6]
s[7]
s[8]male
s[9]phone
s[10]
s[11]
You could try something like so:
String str = "LECT-3A,instr01,Instructor 01,teacher,instr1#learnet.com,,,,male,phone,,";
List<String> words = new ArrayList<String>();
int current = 0;
int previous = 0;
while((current = str.indexOf(",", previous)) != -1)
{
words.add(str.substring(previous, current));
previous = current + 1;
}
String[] w = words.toArray(new String[words.size()]);
for(String section : w)
{
System.out.println(section);
}
This yields:
LECT-3A
instr01
Instructor 01
teacher
instr1#learnet.com
male
phone