-
array confusion
Question about two peices of code
1.
Code:
{
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
-
I)
Code:
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)
Code:
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
}
....
}
-
Can you show it without continue and also your the first one doesnt work
Last edited by torrinment; 10-09-2006 at 04:21 PM.
-
Code:
for (int lineIndex=0; lineIndex < lines.length; lineIndex++) {
String line = lines[lineIndex];
if (!line.trim().size() == 0) {
System.out.println(line);
...
}
}
-
the character count one doesnt work either
Code:
{
int i=0;
int j=0;
int total=0;
while(Character.isWhitespace(lines[i].charAt(j)))
{
total++;
i++;
j++;
}
return total;
Last edited by torrinment; 10-09-2006 at 04:34 PM.
-
The character count:
Code:
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);
-
do you know what a nullpointer exception is
-
Second example:
Code:
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.
-
 Originally Posted by torrinment
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
-
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
-
 Originally Posted by torrinment
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.
-
no im just talkin about both the character count you showed me and the eliminate vspace
Code:
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
Code:
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);
}
}
}
-
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.
-
Similar Threads
-
By Java_Noob in forum Java
Replies: 7
Last Post: 03-07-2006, 04:34 PM
-
By angelito in forum VB Classic
Replies: 1
Last Post: 11-21-2005, 06:16 AM
-
Replies: 6
Last Post: 11-01-2005, 09:05 AM
-
By kanakatam in forum Java
Replies: 2
Last Post: 04-15-2005, 09:06 PM
-
By Jason Salas in forum ASP.NET
Replies: 2
Last Post: 04-20-2003, 11:39 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks