Results 1 to 6 of 6
- 03-29-2012, 01:52 AM #1
Member
- Join Date
- Mar 2012
- Posts
- 55
- Rep Power
- 0
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!
- 03-29-2012, 02:37 AM #2
Member
- Join Date
- Feb 2012
- Location
- Phoenix, AZ
- Posts
- 26
- Rep Power
- 0
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."); } }
- 03-29-2012, 02:51 AM #3
Member
- Join Date
- Mar 2012
- Posts
- 55
- Rep Power
- 0
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!
-
Re: Help fix program- counts words in sentance, display odd or even, than true or fal
- 03-29-2012, 04:36 PM #5
Member
- Join Date
- Mar 2012
- Posts
- 55
- Rep Power
- 0
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!
- 03-30-2012, 08:53 PM #6
Member
- Join Date
- Mar 2012
- Posts
- 55
- Rep Power
- 0
Similar Threads
-
Boolean.True and Boolean.False, why do some people use these?
By Pojahn_M in forum New To JavaReplies: 3Last Post: 09-13-2011, 12:01 AM -
getting a set Visible(true) turned false with an actionhandler.
By Mokomi in forum New To JavaReplies: 2Last Post: 05-25-2011, 02:33 PM -
how to balance true and false instances per id ?
By aneuryzma in forum New To JavaReplies: 1Last Post: 03-27-2011, 02:35 PM -
a program that counts the number of each word in a sentence given by the user
By rambo126 in forum New To JavaReplies: 13Last Post: 10-31-2010, 08:42 PM -
Prime Number - true , false
By pinkdreammsss in forum Java AppletsReplies: 11Last Post: 05-04-2010, 02:49 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks