Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: What's Wrong With My Code?

  1. #1
    Member
    Join Date
    Jan 2012
    Posts
    65
    My Mood
    Happy
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default What's Wrong With My Code?

    Whatever I input, it doesn't print out anything. All it keeps saying is to "Enter a variable name (q to quit): ". I'm pretty sure my coding is correct, I'm just having trouble putting it all together.
    import java.util.Scanner;
     
    public class Assign4 {
    	public static void main(String[] args) {
    		Scanner Input = new Scanner(System.in);
     
    		String variableName;
    		char ch;
    		boolean legal = true;
     
    		System.out.println("This program checks the properness of a proposed Java" +
    			" variable name.");
    		do {
    			System.out.println("Enter a variable name (q to quit): ");
    			variableName = Input.nextLine();
     
    			if (variableName.isEmpty()) {
    				System.out.println("Illegal.");
    			}
    			ch = variableName.charAt(0);
    			if (!(Character.isUpperCase(ch))); {
    				legal = false;
    				System.out.println("Legal, but uses poor style.");
    				}
    			if ((!(Character.isDigit(ch)))) {
    				legal = false;
    				System.out.println("Illegal.");
    			}
    			else {
    				System.out.println("Good.");
    			}
    			for (int i=1; i<variableName.length() && legal; i++) {
    				ch = variableName.charAt(i);
    				if(!(Character.isWhitespace(ch))) {
    					legal = false;
    				}
    			}
    			if (legal) {
    				System.out.println("Good.");
    			}
    				}while(!Input.equals("q")); {
    			System.out.println("End Program.");
    	}
    	}}


  2. #2
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: What's Wrong With My Code?

    First of all, I suggest using if-else-if statements instead of a bunch of if-statements like you are currently doing. This makes your program more easy to follow and lessens the work that a computer must do.

    As for your question, I don't know how to help; I need you to be more specific. What exactly are you typing and inputting into the Scanner when the program halts and waits on your input? Do you get errors? Does the program not ask for input at all?
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  3. #3
    Member
    Join Date
    Jan 2012
    Posts
    65
    My Mood
    Happy
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: What's Wrong With My Code?

    The program keeps asking for input. No errors. I will modify the program as you said and see if that helps. Thanks for responding snow.

  4. #4
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: What's Wrong With My Code?

    Input.equals("q")
    Here's your problem! You're checking to see if the Scanner equals "q" which will never be true. Instead of the Scanner's value, you should see if the String variableName has a value of "q". Maybe that will help.
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  5. #5
    Member
    Join Date
    Jan 2012
    Posts
    65
    My Mood
    Happy
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: What's Wrong With My Code?

    Still is not outputting what it's supposed to. Here is my modified code.

    import java.util.Scanner;
     
    public class Assign4 {
    	public static void main(String[] args) {
    		Scanner Input = new Scanner(System.in);
     
    		String variableName;
    		char ch;
    		boolean legal = true;
     
    		System.out.println("This program checks the properness of a proposed Java" +
    			" variable name.");
    		do {
    			System.out.println("Enter a variable name (q to quit): ");
    			variableName = Input.nextLine();
     
    			if (variableName.isEmpty()) {
    				System.out.println("Illegal.");
    			}
    			ch = variableName.charAt(0);
    			if else (!(Character.isUpperCase(ch))); {
    				legal = false;
    				System.out.println("Legal, but uses poor style.");
    				}
    			if else ((!(Character.isDigit(ch)))) {
    				legal = false;
    				System.out.println("Illegal.");
    			}
    			else {
    				System.out.println("Good.");
    			}
     
    			for (int i=1; i<variableName.length() && legal; i++) {
    				ch = variableName.charAt(i);
    				if(!(Character.isWhitespace(ch))) {
    					legal = false;
    				}
    			}
    			if (legal) {
    				System.out.println("Good.");
    			}
    				}while(!variableName.equals("q")); {
    			System.out.println("End Program.");
     
    	}
    	}
    	}
    If I input q, it prints Good. Anything else and it just keeps repeating the first statement over and over again.

  6. #6
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: What's Wrong With My Code?

    if else (!(Character.isUpperCase(ch))); {
    legal = false;
    System.out.println("Legal, but uses poor style.");
    }
    if else ((!(Character.isDigit(ch)))) {
    legal = false;
    System.out.println("Illegal.");
    }
    The syntax of else if statements is else if(condition), not if else(condition).
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  7. #7
    Member
    Join Date
    Jan 2012
    Posts
    65
    My Mood
    Happy
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: What's Wrong With My Code?

    Quote Originally Posted by snowguy13 View Post
    The syntax of else if statements is else if(condition), not if else(condition).
    Still doesn't output what I want it to. Do you think it has something to do with my Do loop? Modified code:

    import java.util.Scanner;
     
    public class Assign4 {
    	public static void main(String[] args) {
    		Scanner Input = new Scanner(System.in);
     
    		String variableName;
    		char ch;
    		boolean legal = true;
     
    		System.out.println("This program checks the properness of a proposed Java" +
    			" variable name.");
    		do {
    			System.out.println("Enter a variable name (q to quit): ");
    			variableName = Input.nextLine();
     
    			if (variableName.isEmpty()) {
    				System.out.println("Illegal.");
    			}
    			ch = variableName.charAt(0);
    			else if (!(Character.isUpperCase(ch))); {
    				legal = false;
    				System.out.println("Legal, but uses poor style.");
    				}
    			else if ((!(Character.isDigit(ch)))) {
    				legal = false;
    				System.out.println("Illegal.");
    			}
    			else {
    				System.out.println("Good.");
    			}
     
    			for (int i=1; i<variableName.length() && legal; i++) {
    				ch = variableName.charAt(i);
    				if(!(Character.isWhitespace(ch))) {
    					legal = false;
    				}
    			}
    			if (legal) {
    				System.out.println("Good.");
    			}
    				}while(!variableName.equals("q")); {
    			System.out.println("End Program.");
     
    	}
    	}
    	}

  8. #8
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: What's Wrong With My Code?

    Can you give me examples of your input, and then what output you get? I'm kinda in the dark as to what the exact problem is...

    Also, I suggest instead of using the for-loop you have to check for white space, simply use the <String>.contains() method. This will eliminate the for-loop.

    But again, I need some examples...

    Also note my school is starting now, so I can't be of much help until about 12:10 when I have some free time...
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  9. #9
    Member
    Join Date
    Jan 2012
    Posts
    65
    My Mood
    Happy
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: What's Wrong With My Code?

    Example: Input a variable name (q to quit):
    if input is Bakercake it should output "legal, but uses poor style." because of capital first letter.
    if input is baker cake2 it should output "illegal" because of space.
    if input is 2baker it should output "illegal" because of digit first letter.
    anything else for example, bakercake2, would be "good".
    if input is q it should output "End Program"

  10. #10
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: What's Wrong With My Code?

    Hi Nuggets!
    I think your problem is that you pass a value to "ch" after the first if in your do while loop. I think it should go before the if statements.
    And there is also a syntax error in the first else if statement:
    else if (!(Character.isUpperCase(ch))); {
    	legal = false;
    	System.out.println("Legal, but uses poor style.");
    }

  11. #11
    Member
    Join Date
    Jan 2012
    Posts
    65
    My Mood
    Happy
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: What's Wrong With My Code?

    Okay, so I rewrote my code again after my first failed attempt. What this program is supposed to do is to look for if a variable name that has been inputted is correct. If first character of variable name is a number, then it should print out "Illegal." If first character is capitalized, then it should print out "Legal." If there is a space, it should print out "Illegal." Also, if there is a '$' or '_' in the name, then it should print "Legal." Anything else that is inputted, for example "myName1", it should print out "Good." Entering "q" should print out "End Program." Here is my new code.

    import java.util.Scanner;
     
    public class Assign4 {
    	public static void main(String[] args) {
    		Scanner Input = new Scanner(System.in);
     
    		String variableName;
    		char ch;
    		boolean legal = true;
    		boolean proper = true;
     
    		System.out.println("This program checks the properness of a proposed Java" +
    			" variable name.");
    		do {
    			System.out.println("Enter a variable name (q to quit): ");
    			variableName = Input.nextLine();
    			ch = variableName.charAt(0);
    			if (variableName.equalsIgnoreCase("q")) {
    				System.out.println("End Program.");
     
    			}
    				else if (variableName.isEmpty()) {
    					legal = false;
    					proper = false;
     
    				}
     
    					if (Character.isUpperCase(ch)) {
    						legal = false;
    						proper = true;
    					}
    					else if (Character.isLetterOrDigit(ch) || ch == '$' || ch == '_') {
    						legal = false;
    						proper = false;
    					}
     
    			for (int i=1; i<variableName.length() && legal; i++) {
    				ch = variableName.charAt(i);
     
    				if (Character.isWhitespace(ch) || ch == '$' || ch == '_') {
    					legal = false;
    				}
    			}
    			if (legal) {
    				System.out.println("Good.");
    			}
    			if (proper) {
    				System.out.println("Legal, but uses poor style.");
    			}
    			else {
    				System.out.println("Illegal.");
    			}
    				}while (variableName.equalsIgnoreCase("q")); {
    			System.out.println("End Program.");
     
    	}
    	}
    	}
    What I'm having trouble on is when I input for example, "myName1", it prints "Illegal" when in fact it should print "Good." Also, if I input "q", then it will print out "End Program." and "Illegal." and the loop keeps going when it should end. All the other criteria's match and are fine. Any feedback would be nice.

  12. #12
    Junior Member
    Join Date
    Jan 2012
    Posts
    10
    My Mood
    Lurking
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: What's Wrong With My Code?

    Hey, This might help... your if statments are the problem. You seem to be checking for a false isUpperCase when what you want is a true.


    if (Character.isUpperCase(ch)) {
    legal = false;
    System.out.println("Legal, but uses poor style.");
    }

    if (Character.isDigit(ch)) {
    legal = false;
    System.out.println("Illegal, The first character can't be a number.");
    }

    for (int i = 1; i < variableName.length() && legal; i++) {
    ch = variableName.charAt(i);
    if (Character.isWhitespace(ch)) {
    legal = false;
    }
    }
    if (legal) {
    System.out.println("Good.");
    }

    } while (!variableName.equals("q"));
    System.out.println("End Program.");

Similar Threads

  1. need help .. what is wrong with my code.
    By baig-sh in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 10th, 2011, 07:28 PM
  2. what is wrong with my code?
    By bmb in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 7th, 2011, 09:03 AM
  3. What is the wrong with this code?
    By Subhasri in forum AWT / Java Swing
    Replies: 1
    Last Post: September 29th, 2011, 08:14 AM
  4. what's wrong with this code
    By gcsekhar in forum Java Networking
    Replies: 5
    Last Post: July 21st, 2011, 06:01 AM
  5. What is wrong with my code???
    By nine05 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 8th, 2011, 09:59 AM