Results 1 to 8 of 8
  1. #1
    ydrgsz is offline Member
    Join Date
    Nov 2015
    Posts
    4
    Rep Power
    0

    Default Help with a java assignment

    I'm currently trying to complete an assignment for my compsci class. Can anyone tell me what exactly I've done wrong here?
    Attached Files Attached Files

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,993
    Rep Power
    9

    Default Re: Help with a java assignment

    In method isOdd(), variable "nameTwo" suddenly appears. Where did that come from?

    Next time, please put your code in between [code][/code] tags, so we can just read it here.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  3. #3
    ydrgsz is offline Member
    Join Date
    Nov 2015
    Posts
    4
    Rep Power
    0

    Default Re: Help with a java assignment

    I replaced nameTwo with userInput (nametwo was from an earlier attempt. I'm having a hard time understanding methods. Can you provide any insight as to what I've done wrong?
    Java Code:
    import java.util.Scanner;
    class Methods{
    	public static void main(String args[]){
    		String userInput = getInput();
    		printMessage(String);
    	}
    	public static String getInput(){
    		Scanner UserIn = new Scanner(System.in);
    		String input = UserIn.next();
    		return input;
    	}
    	public static void printMessage(){
    		boolean isItOdd = isOdd();
    		String userInput = userInput;
    		if (isItOdd == true){
    			String oddNess = "odd";
    			System.out.println("Hello," + userInput + "your name has an " + oddNess + "number of characters!");
    		} else{
    			oddNess = "even";
    			System.out.println("Hello," + userInput + "your name has an " + oddNess + "number of characters!");
    		}
    	}
    	public static boolean isOdd(){
    		int nameLength = userInput.length;
    		boolean isEven = false;
    		if ( nameLength % 2 == 0){
    			isEven = true;
    		} else{
    			isEven = false;
    		}
    }
    		return isEven;
    	}
    }
    Last edited by ydrgsz; 11-24-2015 at 12:47 PM. Reason: didn't copy all code

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Help with a java assignment

    What does your code do?
    What is it supposed to do?

    That'll help us narrow down the possible issues.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    ydrgsz is offline Member
    Join Date
    Nov 2015
    Posts
    4
    Rep Power
    0

    Default Re: Help with a java assignment

    It's supposed to repeat your name, and tell you if it has an even or odd number of letters.

  6. #6
    ydrgsz is offline Member
    Join Date
    Nov 2015
    Posts
    4
    Rep Power
    0

    Default Re: Help with a java assignment

    I've managed to get it down to two errors. I just need to know how to call the string userInput from method main into the printMessage method

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Help with a java assignment

    how to call the string userInput from method main into the printMessage method
    See the tutorial on methods: Passing Information to a Method or a Constructor (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,993
    Rep Power
    9

    Default Re: Help with a java assignment

    You should learn about "scope". If you declare a variable inside a method, the scope of that variable is the method. You cannot access it outside that method. But that is what you are trying to do with "userInput". You declare it in several methods, but try to reference it from others. If you want to access it from more places, you should widen the scope by declaring it in the class. Remember you are using static methods, so make sure to make the variable static as well. (I'm not promoting the use of static variables, but for now, you should be able to make it work)
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

Similar Threads

  1. Java Assignment
    By lance215 in forum New To Java
    Replies: 1
    Last Post: 06-15-2012, 01:11 AM
  2. Java Assignment
    By MichaelF144 in forum New To Java
    Replies: 34
    Last Post: 04-24-2012, 06:28 PM
  3. Java Assignment Help
    By nve5009 in forum New To Java
    Replies: 4
    Last Post: 04-27-2011, 07:36 AM
  4. java assignment
    By omgonoes in forum New To Java
    Replies: 13
    Last Post: 04-23-2011, 03:29 AM
  5. need help with a java assignment.
    By Z-slasher in forum New To Java
    Replies: 7
    Last Post: 04-18-2011, 12:05 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •