Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

fBaran

macrumors regular
Original poster
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!
 
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
 
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?
 
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;
}
 
plinden said:
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.
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.
 
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.
 
fBaran said:
I'm still getting this error
Code:
$ javac Project0.java
Project0.java:32: '}' expected
}
 ^
1 error
Yeah you're still missing a closing } at the end

fBaran said:
Here's my revised code:
Code:
import javax.swing.*;
public class Project0 [color=red]{[/color]

    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.

[color=red]}[/color] //missing
 
plinden said:
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😕
Code:
Project0.java:32: missing return statement
        } //end uppercaseCount
        ^
1 error
 
fBaran said:
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😕
Code:
Project0.java:32: missing return statement
        } //end uppercaseCount
        ^
1 error
That's because you return count inside the if block ...
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.