Results 1 to 6 of 6
  1. #1
    JMAsterson is offline Member
    Join Date
    Mar 2012
    Posts
    55
    Rep Power
    0

    Unhappy Help fix program- counts words in sentance, display odd or even, than true or false

    I've spent a lot of time working on my program and I just cant get it to work correctly! Here is the details:
    Create a class called OddSentence whose constructor takes a String as input – this String is the sentence that the user types. The class also implements a method isOdd() which takes no parameters and returns a bool value – it returns true if the sentence has an odd number of words, and false otherwise. Then create a class OddSentenceTester which prompts the user to enter a sentence, creates an OddSentence object with the sentence, and calls isOdd(). The program then displays “That’s odd!” if isOdd() returns true, and “That’s even!” if isOdd() returns false.

    So far I have OddSentence, which seems to be correct.

    import static java.lang.System.*;



    public class OddSentence

    {

    public static boolean isOdd( int num )

    {



    if( num %2 == 0) {return false;}

    return true;



    }

    public static boolean isEven( int num )

    {



    if( num %2 == 0) {return true;}

    return false;

    }

    }


    And here is the problematic one, OddSentenceTester:

    import static java.lang.System.*;
    import javax.swing.JOptionPane;
    import java.util.Scanner;
    import java.util.*;


    public class OddSentenceTester

    {
    public static void main ( String[] args )

    {
    Scanner input=new Scanner(System.in);
    String num;
    int number;
    num = JOptionPane.showInputDialog( null, " Enter a sentence" );
    String st=input.nextLine();
    int count=0;

    System.out.println("Number of words are: "+number);
    number = Integer.parseInt(num);



    if( number % 2 == 0)
    System.out.println(OddSentence.isOdd(num));

    else

    System.out.println(OddSentence.isEven(num));



    if(OddSentence.isOdd(num))


    System.out.println("That's Odd!");

    else

    System.out.println("That's Even!");

    }

    }


    Eventually i tried converting the int to String... now im a bit confused and dont know where to go from here. Yes, I'm a new programmer. I appreciate any help at all! - also, i'm not great with terms so please be as simple as possible and if you can show me the changes i should make. Thanks so much!

  2. #2
    subzero1012 is offline Member
    Join Date
    Feb 2012
    Location
    Phoenix, AZ
    Posts
    26
    Rep Power
    0

    Default Re: Help fix program- counts words in sentance, display odd or even, than true or fal

    You may need to figure out how to ignore the spaces in the sentence, but you take a look at mine.

    Java Code:
    public class test {
    	private String sentence;
    	public test(String sentence){
    		this.sentence = sentence;
    	}
    	
    	public boolean isOdd(){
    		if(sentence.length()%2 == 0)
    			return false;
    		else  
    			return true;
    	}
    
    }
    Java Code:
    public class testTest {
    	public static void main(String[] args){
    		String sentence = "Hello 1";
    		test oneSentence = new test(sentence);
    		if(oneSentence.isOdd())
    			System.out.println("Sentence is odd.");
    		else
    			System.out.println("Sentence is even.");
    	}
    
    }

  3. #3
    JMAsterson is offline Member
    Join Date
    Mar 2012
    Posts
    55
    Rep Power
    0

    Default Re: Help fix program- counts words in sentance, display odd or even, than true or fal

    Thanks for replying, your program is much simpler than mine. The thing is, the program is supposed to ask the user to input a sentence when the program is run. If the sentence has an odd number of words, "True" should display and if there is an even number of words it should display "False". Thanks again for helping!

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Help fix program- counts words in sentance, display odd or even, than true or fal

    Quote Originally Posted by JMAsterson View Post
    Thanks for replying, your program is much simpler than mine. The thing is, the program is supposed to ask the user to input a sentence when the program is run. If the sentence has an odd number of words, "True" should display and if there is an even number of words it should display "False". Thanks again for helping!
    So what is your current question?

  5. #5
    JMAsterson is offline Member
    Join Date
    Mar 2012
    Posts
    55
    Rep Power
    0

    Default Re: Help fix program- counts words in sentance, display odd or even, than true or fal

    Well it seems that your program already has a sentence in the program, so it doesnt ask the user to input when the program is run. I do see that in your test.java that it mentions the true and false. What I am not understanding is why my program, when run, will only ask for the sentence and display the number of words, but not display whether its true or false, or odd or even. I think I did everything right for those parts, but obviously not since its not working. Have you tried running my program? What i would like some advice on is why my program is running the rest of what I tried to have it do. thanks so much!

  6. #6
    JMAsterson is offline Member
    Join Date
    Mar 2012
    Posts
    55
    Rep Power
    0

    Default Re: Help fix program- counts words in sentance, display odd or even, than true or fal

    Thanks guys for helping - I"m trying to figure this out by sunday!

Similar Threads

  1. Replies: 3
    Last Post: 09-13-2011, 01:01 AM
  2. Replies: 2
    Last Post: 05-25-2011, 03:33 PM
  3. how to balance true and false instances per id ?
    By aneuryzma in forum New To Java
    Replies: 1
    Last Post: 03-27-2011, 03:35 PM
  4. Replies: 13
    Last Post: 10-31-2010, 09:42 PM
  5. Prime Number - true , false
    By pinkdreammsss in forum Java Applets
    Replies: 11
    Last Post: 05-04-2010, 03:49 PM

Tags for this Thread

Posting Permissions

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