PDA

View Full Version : Java - Classifying by character type




fBaran
Sep 24, 2006, 04:34 PM
I'm using xCode (FYI). This program accepts input via JOptionPane.showInputDialog() and tells the user how many upper/lower case letters there were using a JOptionPane.showMessageDialog(). It repeats this process until the user types the word "Stop".

Here's my code

//
//
import javax.swing.*;
public class Project0 {

public static void main (String args[])
{

String line = JOptionPane.showInputDialog("Enter a line of text:");

JOptionPane.showMessageDialog(null, "Your string contains "
+ lowercaseCount(line)
+ " lowercase letters.");

System.exit(0);
}

public static int lowercaseCount(String line)
{
int count = 0;
for ( int i = 0; i < line.length(); i++ )
{
char x = line.charAt(i);
if ( x >= '65' && x <= '90' )
count++;
}
return count;

}
}



I get 6errors here, 4 Unclosed character literal, 1 ')' expected, and 1 "Command /Developer/jam failed with exit code 1
if ( x >= '65' && x <= '90' )
count++;
Any ideas? Once this part is done, the rest should be a snap, just a matter ading an else() to the loop...right? Thanks!



mufflon
Sep 24, 2006, 04:54 PM
import javax.swing.*;
public class Project0 {

public static void main (String args[]) {

String line = JOptionPane.showInputDialog("Enter a line of text:");

JOptionPane.showMessageDialog(null, "Your string contains "
+ lowercaseCount(line)
+ " lowercase letters.");
//System.exit(0)
}

public static int lowercaseCount(String line) {
int count = 0;
for ( int i = 0; i < line.length(); i++ ) {
char x = line.charAt(i);
if ( x >= '65' && x <= '90' ) {
count++;
}


}
return count;
}

(use the "code statement next time please)

late over here, but I've rectified the code, this is what I consider had to be fixed:
if: no starting {
return: missplaced (inside for loop)


and one more thing, shouldn't cause an error, but isn't needed -
no need to use System.exit(0) at the end of the main class - ends anyhow



Before you're 100% comfortable with java, use the easier javac in the terminal - tells you the issues, where and how - without the gui.
But I suppose everyone must have a choice :)

gl

edit: forgot a } myself - from the class - so add one more } and tab the rest of the code accordingly

Eraserhead
Sep 24, 2006, 04:59 PM
the command/developer.jam thing isn't an error, XCode just provides that to tell you that their is an error in your Code.

I would add {} in the if ( x >= '65' && x <= '90' )
count++; bit but they aren't strictly needed.

For future use, you use [ CODE ] and [/ CODE ] tags (without spaces) to show code.

Other than that the code looks fine though I've never done a Java GUI so the problem may lie there. Can you give the lines of the errors please?

plinden
Sep 24, 2006, 05:11 PM
Remove the quotes:
if (x >= 65 && x <= 90)

And you're counting upper case letters, not lower case. See http://www.lookuptables.com/

Also, take a look at Sun's Java code style conventions - I always try to get new developers to use them since it makes other people's code easier to understand if it follows roughly the same style.

And I second the suggestion to use the command line until you're comfortable enough with it. Only then, use an IDE.

Edit: One more thing - since 1.4, Java has been much better at this, but even now, if a string is longer than about 300 characters, it is faster to convert the string to a character array than use charAt:
public static int lowercaseCount(String line) {
int count = 0;
char[] chars = line.toCharArray();
for (int i = 0; i < chars.length; i++) {
if ( char[i] >= 65 && char[i] <= 90 ) {
count++;
}
}
return count;
}

fBaran
Sep 24, 2006, 05:19 PM
Remove the quotes:
if (x >= 65 && x <= 90)

And you're counting upper case letters, not lower case. See http://www.lookuptables.com/

Also, take a look at Sun's Java code style conventions - I always try to get new developers to use them since it makes other people's code easier to understand if it follows roughly the same style.
D'oh! Thank you. I Googled "unclosed character literal" and I realized that I neglected to mention the reason for if (x >= 65 && x <= 90) was to compare each character to the ASCII value! In any case, I'm still getting this error
$ javac Project0.java
Project0.java:32: '}' expected
}
^
1 error

Here's my revised code:import javax.swing.*;
public class Project0 {

public static void main (String args[]) {

String line = JOptionPane.showInputDialog("Enter a line of text:");

JOptionPane.showMessageDialog(null, "Your string contains "
+ uppercaseCount(line)
+ " uppercase letters.");

}

public static int uppercaseCount(String line) {
int count = 0;
for ( int i = 0; i < line.length(); i++ ) {
char x = line.charAt(i);
if ( x >= 65 && x <= 90 ) {
count++;
return count;
}
}
} //Line32. This is the error in the CODE listed above.

plinden
Sep 24, 2006, 05:26 PM
If you follow the convention of indenting your class internals by 4 spaces, ie. all methods and member declarations should be indented, you will see what's missing.

dcv
Sep 24, 2006, 05:32 PM
I'm still getting this error
$ javac Project0.java
Project0.java:32: '}' expected
}
^
1 error

Yeah you're still missing a closing } at the end


Here's my revised code:import javax.swing.*;
public class Project0 {

public static void main (String args[]) {

String line = JOptionPane.showInputDialog("Enter a line of text:");

JOptionPane.showMessageDialog(null, "Your string contains "
+ uppercaseCount(line)
+ " uppercase letters.");

}

public static int uppercaseCount(String line) {
int count = 0;
for ( int i = 0; i < line.length(); i++ ) {
char x = line.charAt(i);
if ( x >= 65 && x <= 90 ) {
count++;
return count;
}
}
} //Line32. This is the error in the CODE listed above.

} //missing

fBaran
Sep 24, 2006, 05:35 PM
If you follow the convention of indenting your class internals by 4 spaces, ie. all methods and member declarations should be indented, you will see what's missing.
The needed '}' on line32 was the curly brace to close the Project0 class. That's done (:rolleyes: ), but I'm still missing a return statement on line32:confused: Project0.java:32: missing return statement
} //end uppercaseCount
^
1 error

plinden
Sep 24, 2006, 05:39 PM
The needed '}' on line32 was the curly brace to close the Project0 class. That's done (:rolleyes: ), but I'm still missing a return statement on line32:confused: Project0.java:32: missing return statement
} //end uppercaseCount
^
1 error
That's because you return count inside the if block ...

fBaran
Sep 24, 2006, 05:50 PM
That's because you return count inside the if block ...
Thank you so much! I'll be sure to bother you when I get my next assignment:D

plinden
Sep 24, 2006, 05:54 PM
You got this one for free. Next time I'll just be dropping hints.