Java - Classifying by character type

Discussion in 'Mac Programming' started by fBaran, Sep 24, 2006.

  1. macrumors regular

    Joined:
    Oct 22, 2003
    #1
    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
    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
    Code:
             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!
     
  2. macrumors 6502

    Joined:
    Sep 15, 2006
    #2
    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.");
          [B]//System.exit(0)[/B]
    }
    
    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' ) [B]{[/B]
    			count++;
    		}
    		
    
    	}
    	[B]return count;[/B]
    }
    (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
     
  3. macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #3
    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?
     
  4. macrumors 68040

    plinden

    Joined:
    Apr 8, 2004
    #4
    Remove the quotes:
    Code:
    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:
    Code:
    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;
    }
     
  5. thread starter macrumors regular

    Joined:
    Oct 22, 2003
    #5
    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
    Code:
    $ javac Project0.java
    Project0.java:32: '}' expected
    }
     ^
    1 error
    Here's my revised code:
    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.
    
    
     
  6. macrumors 68040

    plinden

    Joined:
    Apr 8, 2004
    #6
    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.
     
  7. dcv
    macrumors G3

    Joined:
    May 24, 2005
    #7
    Yeah you're still missing a closing } at the end

     
  8. thread starter macrumors regular

    Joined:
    Oct 22, 2003
    #8
    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:
    Code:
    Project0.java:32: missing return statement
            } //end uppercaseCount
            ^
    1 error
     
  9. macrumors 68040

    plinden

    Joined:
    Apr 8, 2004
    #9
    That's because you return count inside the if block ...
     
  10. thread starter macrumors regular

    Joined:
    Oct 22, 2003
    #10
    Thank you so much! I'll be sure to bother you when I get my next assignment:D
     
  11. macrumors 68040

    plinden

    Joined:
    Apr 8, 2004
    #11
    You got this one for free. Next time I'll just be dropping hints.
     

Share This Page