Results 1 to 10 of 10
  1. #1
    pence is offline Member
    Join Date
    May 2013
    Posts
    16
    Rep Power
    0

    Default Doing it the hard way

    Hello all!
    So I have this assignment and my teacher has specific instructions. I have already completed the assignment, just not the way he wants it done.
    Image 1 is his sample screenshot of the final result.
    I can get that using this code I have written, but it need it done like ATTACHMENT 2 says. Please help me!
     import java.util.Scanner;

    publicclass Project5

    {

    publicstaticvoid main(String[] args)

    {

    Scanner keyboard = new Scanner(System.in);

    System.out.println("Please enter words ending with 999.");

    System.out.print("Word: ");

    String word = keyboard.next();

    while(!word.equals("999"))

    {

    if(word.charAt(0) == word.charAt(word.length() - 1))

    {

    System.out.println("The word "+word+" has first and last characters that are the same.");

    System.out.print("Word: ");

    }

    else

    {

    System.out.print("Word: ");

    }

    word = keyboard.next();

    }

    }


    } End... Any help at all would be appreciated. I know it should look something like this but I'm stuck.
    Attached Thumbnails Attached Thumbnails Doing it the hard way-image-1.png   Doing it the hard way-image-2.png  

  2. #2
    pence is offline Member
    Join Date
    May 2013
    Posts
    16
    Rep Power
    0

    Default Re: Doing it the hard way

    I know it should look something like this, but I think I'm just stuck...

    import java.util.Scanner;


    public class Project5
    {
    public static void main(String[] args)
    {
    Scanner keyboard = new Scanner(System.in);
    System.out.println("Please enter words ending with 999.");
    System.out.print("Word: ");
    String word = keyboard.next();
    while(!word.equals("999"))
    {
    if(word.charAt(0) == word.charAt(word.length() - 1))
    {
    System.out.println("The word "+word+" has first and last characters that are the same.");
    System.out.print("Word: ");
    }
    else
    {
    System.out.print("Word: ");
    }
    word = keyboard.next();
    }
    }
    public static boolean isWord999(boolean Word)
    {
    boolean word = (word=="999");
    if (isWord999(true))
    {
    System.out.println("Program ended.");
    }
    else
    {
    System.out.println();
    }

    }

    public static void isFirstLastSame()
    {

    }
    }

  3. #3
    Join Date
    May 2013
    Location
    New Hampshire, USA
    Posts
    5
    Rep Power
    0

    Default Re: Doing it the hard way

    Hey Pence!

    I'd be happy to help. First, a tip for the forums. Please use the code tag to wrap your code (preferably, indent as well (; ), so it looks like this:

    Java Code:
    import java.util.Scanner;
    
    
    public class Project5 
    {	
        public static void main(String[] args) 
        {
            Scanner keyboard = new Scanner(System.in); 
            System.out.println("Please enter words ending with 999."); 
            System.out.print("Word: ");
            String word = keyboard.next(); 
            while(!word.equals("999"))	
            { 
                if(word.charAt(0) == word.charAt(word.length() - 1)) 
                { 
                    System.out.println("The word "+word
                        +" has first and last characters that are the same."); 
                    System.out.print("Word: ");
                } 
                else 
                { 
                    System.out.print("Word: "); 
                } 
                word = keyboard.next(); 
            } 
        }
        public static boolean isWord999(boolean Word)
        {
            boolean word = (word=="999");
            if (isWord999(true)) 
            {
                System.out.println("Program ended.");
            } 
            else 
            {
                System.out.println();
            }
    
        }
    
        public static void isFirstLastSame()
        {
    
        }
    }
    Alright, first thing I noticed is the way your isWord999() function is laid out. The purpose of this function is to take in a String, and return true if the string is "999", and false if it's anything else. So why is the function taking in a boolean? Also, it's not actually called anywhere in the main() method.

  4. #4
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    340
    Rep Power
    5

    Default Re: Doing it the hard way

    Take it one step at a time:

    You need to create a method called isWord999(). It takes in a String as a parameter, and returns a boolean representing whether or not the word passed in is "999"
    Here is what you have:

    Java Code:
    public static boolean isWord999(boolean Word)
    {
    boolean word = (word=="999");
    if (isWord999(true))
    {
    System.out.println("Program ended.");
    }
    else
    {
    System.out.println();
    }
    
    }
    You have declared a method called isWord999(correct)
    The method returns a boolean (correct)
    The method takes in a boolean as a parameter(incorrect)

    First, you should fix your method to take in a String as a parameter, not a boolean.
    Once you do that, think about the logic for the method itself. It is fairly straightforward,
    the String class has an equals() method that you can use.

    Next, you need a method called isFirstLastSame(). It should take in a String as a parameter, and return a boolean value.
    Here is what you have:

    Java Code:
    public static void isFirstLastSame()
    {
    
    }
    You almost have the 'shell' for your method created. Remember that your method has to take in a String as a parameter(that String being the input from the user in order to test whether or not the 1st and last characters are the same.
    Try to update your method to take in a String as a parameter.
    Once you do that, you basically just need to call these methods from inside of your main method, instead of doing everything in main().

    Does that help?

  5. #5
    pence is offline Member
    Join Date
    May 2013
    Posts
    16
    Rep Power
    0

    Default Re: Doing it the hard way

    Oh thanks! That looks much better!

    So something like this?

    Java Code:
    import java.util.Scanner;
    
    
     public class Project5 
     { 
     public static void main(String[] args) 
     {
     Scanner keyboard = new Scanner(System.in); 
     System.out.println("Please enter words ending with 999."); 
     System.out.print("Word: ");
     String word = keyboard.next(); 
     while(!word.equals("999")) 
     { 
     word=isWord999() 
     { 
     System.out.print("Word: "); 
     } 
     word = keyboard.next(); 
     } 
     }
     public static String isWord999(String Word)
     {
     boolean word = (word=="999");
     if (isWord999(true)) 
     {
     System.out.println("Program ended.");
     } 
     else 
     {
     System.out.println();
     }
    
     }
    
     public static String isFirstLastSame(String word)
     {
     if(word.charAt(0) == word.charAt(word.length() - 1)) 
    {
    System.out.println("The word "+word+" has first and last characters that are the same."); 
    }
     }
     }

  6. #6
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    340
    Rep Power
    5

    Default Re: Doing it the hard way

    Which IDE are you using to write your code(NetBeans, Eclipse?). I'm guessing you didn't attempt to run what you posted, because this code would not even compile.

    Lets look at your first method:

    Java Code:
    public static String isWord999(String Word)
     {
     boolean word = (word=="999");
     if (isWord999(true))
     {
     System.out.println("Program ended.");
     }
     else
     {
     System.out.println();
     }
    
     }
    There are multiple problems. You are calling the isWord999() method from within the isWord999() method (sort of). That is known as recursion which can be a valid design approach, but not in this particular example, not to mention that you are passing in a boolean value as a parameter to isWord999(), when it requires a String as an argument.

    I would take a look at a tutorial on the basics of JAVA, like declaring methods, parameters, and return types.

    I'll help you out, here is what your isWord999() method should look like:

    Java Code:
    public static boolean isWord999(String inputWord){
       if(inputWord.equals("999")){
          return true;
       }
       return false;
    }
    When you want to call this method, from inside of your main method, you would do something like this:

    Java Code:
    boolean wordEnteredWas999 = isWord999(word);

  7. #7
    pence is offline Member
    Join Date
    May 2013
    Posts
    16
    Rep Power
    0

    Default Re: Doing it the hard way

    Okay I feel like this is better, but I keep getting an error.
    Last edited by pence; 06-11-2013 at 01:00 AM.

  8. #8
    pence is offline Member
    Join Date
    May 2013
    Posts
    16
    Rep Power
    0

    Default Re: Doing it the hard way

    using eclipse by the way.


    The program closes when any string is entered and stayed open when 999 is entered. backwards. :(
    Java Code:
    import java.util.Scanner;
    
    
    public class Project5 
    {	
    	
    	public static void main(String[] args) 
    	{
    	
    	Scanner keyboard = new Scanner(System.in); 
    	System.out.println("Please enter words ending with 999."); 
    	System.out.print("Word: ");
    	String word = keyboard.next(); 
    	while(word.equals("999"))		
    	{ 		 
    	{ 
    	System.out.print("Word: "); 
    	word = keyboard.next();
    	} 
    	} 
    	boolean wordEnteredWasSame = isFirstLastSame(word);
    	boolean wordEnteredWas999 = isWord999(word);
    	}
    	public static boolean isWord999(String inputWord)
    	{
    		if (inputWord.equals("999")) 
    		{				
    			System.out.print("Program ends.");
    		return true;
    		}
    		return false;
    			
    	}	
    	public static boolean isFirstLastSame(String input)
    	{
    		if(input.charAt(0) == input.charAt(input.length() - 1))
    		{ 			
    			System.out.println("The word "+input+" has first and last characters that are the same."); 
    				return true;	
    			} 
    		System.out.print("Word: ");
    		return false;
    			
    	}
    }
    Last edited by pence; 06-11-2013 at 01:01 AM.

  9. #9
    pence is offline Member
    Join Date
    May 2013
    Posts
    16
    Rep Power
    0

    Default Re: Doing it the hard way

    Don't worry guys, I GOT IT!

    Java Code:
    import java.util.Scanner;
    
    
    public class Project5 
    {	
    	
    	public static void main(String[] args) 
    	{	
    	System.out.println("Programmed by Spencer Preston."); 
    	System.out.println(""); 
    	Scanner keyboard = new Scanner(System.in); 
    	System.out.println("Please enter words ending with 999."); 
    	System.out.print("Word: ");
    	String word = keyboard.next();
    	while(!word.equals("999"))	
    	{
    		boolean wordEnteredWasSame = isFirstLastSame(word);
    		word = keyboard.next();	
    	} 
    		boolean wordEnteredWas999 = isWord999(word);	
    	}
    	public static boolean isWord999(String inputWord)
    	{
    		if (inputWord.equals("999")) 		
    		{	
    			System.out.print("Program ends."); 
    			return true;
    		}
    		return false;			
    	}	
    	public static boolean isFirstLastSame(String word)
    	{
    		if(word.charAt(0) == word.charAt(word.length() - 1))
    		{ 			
    			System.out.println("The word "+word+" has first and last characters that are the same."); 
    			System.out.print("Word: ");
    			return true;	
    		} 
    		else
    		{
    		System.out.print("Word: ");
    		return false;
    		}
    	}
    }

  10. #10
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: Doing it the hard way

    Quote Originally Posted by pence View Post
    Doing it the hard way
    Please go through the Forum Rules, particularly the third paragraph.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. This ap course is so hard :(((
    By Army in forum New To Java
    Replies: 22
    Last Post: 01-10-2013, 01:27 PM
  2. How hard is it.....
    By neilp123 in forum New To Java
    Replies: 4
    Last Post: 07-22-2010, 04:21 PM

Posting Permissions

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