Click to See Complete Forum and Search --> : array confusion
torrinment
10-09-2006, 03:37 PM
Question about two peices of code
1. {
int i=0;
int j=0;
int total=0;
while(Character.isWhitespace(lines[i].charAt(j)))
{
total++;
i++;
j++;
}
return total;
this should show the number of characters in an already loadaded file but it doesnt
2How do you take a file with blank lines in it and crunch it up for example
this is
a file
now
should turn into
this
is
a file
now
Uladzimir
10-09-2006, 04:13 PM
I)
int total=0;
for (int lineIndex=0; lineIndex < lines.length; lineIndex++) {
String str = lines[lineIndex];
if ( int j = 0; j < str.length; j++ ) {
if (!Character.isWhitespace(str.charAt(j))) { // if you do not want to calculate whitespaces
total++;
}
}
}
II)
BufferedReader br = new BufferedReader(XXX); // XXX - your input stream e.g. FileInputStream
String line;
while ((line = br.readLine()) != null) {
if (line.trim().size()str == 0) {
continue; // skip empty string
}
....
}
torrinment
10-09-2006, 04:14 PM
Can you show it without continue and also your the first one doesnt work
Uladzimir
10-09-2006, 04:22 PM
for (int lineIndex=0; lineIndex < lines.length; lineIndex++) {
String line = lines[lineIndex];
if (!line.trim().size() == 0) {
System.out.println(line);
...
}
}
torrinment
10-09-2006, 04:25 PM
the character count one doesnt work either
{
int i=0;
int j=0;
int total=0;
while(Character.isWhitespace(lines[i].charAt(j)))
{
total++;
i++;
j++;
}
return total;
Uladzimir
10-09-2006, 04:55 PM
The character count:
int total = 0;
String[] lines = new String[] {
"abcdef", "123", "qwer ty" // 15 chars + 1 space
};
for (int lineIndex=0; lineIndex < lines.length; lineIndex++) {
String str = lines[lineIndex];
for (int j = 0; j < str.length(); j++ ) {
if (!Character.isWhitespace(str.charAt(j))) { // if you do not want to calculate whitespaces
total++;
}
}
}
System.out.println(total);
torrinment
10-09-2006, 04:58 PM
do you know what a nullpointer exception is
Uladzimir
10-09-2006, 05:00 PM
Second example:
String[] lines = new String[] {
"this", " ", "is", "a" , " ", "test" };
for (int lineIndex=0; lineIndex < lines.length; lineIndex++) {
String line = lines[lineIndex];
if (!(line.trim().length() == 0)) {
System.out.println(line);
}
}
PS: I often post a programs which I don't check. I think small errors like misprints you can correct yourself.
Uladzimir
10-09-2006, 05:02 PM
do you know what a nullpointer exception is
Yes I know. It happens when you try to do with something that don't exsists.
For example:
String s = null;
s.trim(); // throws NullPointerException
torrinment
10-09-2006, 05:02 PM
that works but gives me a nullpointerexception how do i fix this like it does what it supposed to but then i get that exceptio n
Uladzimir
10-09-2006, 05:07 PM
that works but gives me a nullpointerexception how do i fix this like it does what it supposed to but then i get that exceptio n
It's difficult to answer because I don't know your code. Please attach a whole your application or am exception stack trace.
BTW: check that you nowhere set empty values to variables.
torrinment
10-09-2006, 05:10 PM
no im just talkin about both the character count you showed me and the eliminate vspace
public static int charCount(String[]lines)
{
int total = 0;
for (int i=0; i < lines.length; i++)
{
for (int j = 0; j < lines[i].length(); j++ )
{
if (!Character.isWhitespace(lines[i].charAt(j))) {
total++;
}
}
}
System.out.println(total);
return total;
}
and
public static void EliminateVSpace(String[]lines)
{
for (int lineIndex=0; lineIndex < lines.length; lineIndex++) {
String line = lines[lineIndex];
if (!(line.trim().length() == 0)) {
System.out.println(line);
}
}
}
Uladzimir
10-09-2006, 05:30 PM
I understand. The exception happens because you pass lines with empty values.
For example
if you pass such array:
String[] lines = new String[] {"this", " ", "is", null, "a", "test" };
it will produce an exception.
torrinment
10-09-2006, 05:31 PM
so i fix it by...
devx.com
Copyright Internet.com Inc. All Rights Reserved