Results 1 to 20 of 21
- 03-30-2012, 10:31 PM #1
Member
- Join Date
- Mar 2012
- Posts
- 55
- Rep Power
- 0
if else - declare if its an odd or even number - help !
I've started using eclipse and it's been very helpful in fixing my problem. I have a program that asks the user to put in a sentence, than counts how many words there are and tells whether its an odd or even number. If its odd, it should say True, if even, it should say false. I think I've almost got it right but i have one problem! Is there something else I could use instead of (num % 2 ==0) in my sentence:
if( num % 2 == 0) {return false;}
return true;
I keep getting an error on that. Here is my full program:
OddSentence:
public class OddSentence
{
public static boolean isOdd(String num )
{
if( num % 2 == 0) {return false;}
return true;
}
public static boolean isEven(String num )
{
if( num % 2 == 0) {return true;}
return false;
}
}
and here is OddSentenceTester (I dont get an error for that phrase in this program):
import javax.swing.JOptionPane;
import java.util.Scanner;
public class OddSentenceTester
{
public static void main ( String[] args )
{
Scanner input=new Scanner(System.in);
String num;
int number = 0;
num = JOptionPane.showInputDialog( null, " Enter a sentence" );
input.nextLine();
number = Integer.parseInt(num);
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!");
}
}
Thank so much, Ive almost got it except that one part! The error says "The operator % is undefined for the argument types String, it". Origonally I had it with mostly int, and eclipse had me change it to String. (dont really know how to explain it). Thakns again!
- 03-30-2012, 10:44 PM #2
Member
- Join Date
- Mar 2012
- Posts
- 55
- Rep Power
- 0
Re: if else - declare if its an odd or even number - help !
Actually, just made a modification- no errors after compiling but can someone tell me why my program will only ask me to input a sentence, and does nothing else? Thanks!
import javax.swing.JOptionPane;
import java.util.Scanner;
public class OddSentenceTester
{
public static void main ( String[] args )
{
Scanner input=new Scanner(System.in);
String num;
int number = 0;
num = JOptionPane.showInputDialog( null, " Enter a sentence" );
input.nextLine();
number = Integer.parseInt(num);
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!");
}
}
public class OddSentence
{
static int number = 0;
public static boolean isOdd(String num )
{
if( number % 2 == 0) {return false;}
return true;
}
public static boolean isEven(String num )
{
if( number % 2 == 0) {return true;}
return false;
}
}
- 03-30-2012, 11:17 PM #3
Re: if else - declare if its an odd or even number - help !
Can you post the console window from when you execute the program?
To copy the contents of the command prompt window:
Click on Icon in upper left corner
Select Edit
Select 'Select All' - The selection will show
Click in upper left again
Select Edit and click 'Copy'
Paste here.If you don't understand my response, don't ignore it, ask a question.
- 03-30-2012, 11:26 PM #4
Member
- Join Date
- Mar 2012
- Posts
- 55
- Rep Power
- 0
Re: if else - declare if its an odd or even number - help !
- 03-30-2012, 11:30 PM #5
Re: if else - declare if its an odd or even number - help !
Did you write the code you are testing?command prompt window just sits there
What is the next statement after the JOptionPane method call?
Think about what that statement will do to your program.If you don't understand my response, don't ignore it, ask a question.
- 03-30-2012, 11:34 PM #6
Member
- Join Date
- Mar 2012
- Posts
- 55
- Rep Power
- 0
Re: if else - declare if its an odd or even number - help !
I think it returns the rest of the current line. Is that right? And yes, I wrote this code myself. I looked at a few other codes and tried to apply pieces to mine, since i wouldnt have been able to find a program that does all of those things combined. I understand that line is supposed to, i think, return the sentence it asks the user to input? I do feel like im missing something, well obviously, i must be since its not working!
- 03-30-2012, 11:38 PM #7
Re: if else - declare if its an odd or even number - help !
That is exactly what the code would do after you answered the JOptionPane window.the empty command prompt window just sits there until i press enter
It will wait for you to enter something on the console.If you don't understand my response, don't ignore it, ask a question.
- 03-30-2012, 11:44 PM #8
Member
- Join Date
- Mar 2012
- Posts
- 55
- Rep Power
- 0
Re: if else - declare if its an odd or even number - help !
Oh, that makes sense then. What do you suggest i add in order to fix this?
- 03-30-2012, 11:49 PM #9
Re: if else - declare if its an odd or even number - help !
You need to decide what the program is supposed to do and in what order it should do it.
Mixing input modes between dialog windows and the console is not a good technique. Do all of the prompting and getting user input the same way.If you don't understand my response, don't ignore it, ask a question.
- 03-30-2012, 11:56 PM #10
Member
- Join Date
- Mar 2012
- Posts
- 55
- Rep Power
- 0
Re: if else - declare if its an odd or even number - help !
Ok I see what you're saying. This is my first semester of Java so this is confusing for me and probably one of the hardest programs ive done. I'm trying really hard to understand this. I do think that the program needs to first ask the user to is ask the user to input a sentence, than display how many words are in the sentence, than if the sentence is even or odd, and lastly, it should say "true" if the number is odd and "False" if the number is even. That's what I'm trying really hard to do.. unfortunately i cant figure out what to change and what my mistakes are. Thats why im thankful for the help of everyone who's trying to help - thanks!
- 03-30-2012, 11:59 PM #11
Re: if else - declare if its an odd or even number - help !
Which way do you want to get the user's input:
with JOptionPane class's method calls that use windows
or with the Scanner class's methods that read from the console?
You should use only one technique, not both.If you don't understand my response, don't ignore it, ask a question.
- 03-31-2012, 12:03 AM #12
Member
- Join Date
- Mar 2012
- Posts
- 55
- Rep Power
- 0
Re: if else - declare if its an odd or even number - help !
I'm not sure, whichever method will be most effective - i didnt realise i was using conflictive methods! I tried changing around the order as a start and now it displays the number of words as 0 and gives me an error:
import javax.swing.JOptionPane;
import java.util.Scanner;
public class OddSentenceTester
{
public static void main ( String[] args )
{
Scanner input=new Scanner(System.in);
String num;
num = JOptionPane.showInputDialog( null, " Enter a sentence" );
int number = 0;
System.out.println("Number of words are: "+number);
number = Integer.parseInt(num);
input.nextLine();
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!");
}
}
- 03-31-2012, 12:05 AM #13
Re: if else - declare if its an odd or even number - help !
Please copy and paste here the full text of the error message.gives me an error:
I still recommend you only use one technique to get user input.
Do you understand that the computer executes the statements in your program in the order that they are coded?
Look at the order of the statements in your code.Last edited by Norm; 03-31-2012 at 12:07 AM.
If you don't understand my response, don't ignore it, ask a question.
- 03-31-2012, 12:11 AM #14
Member
- Join Date
- Mar 2012
- Posts
- 55
- Rep Power
- 0
Re: if else - declare if its an odd or even number - help !
This is the error:
Number of words are: 0
Exception in thread "main" java.lang.NumberFormatException: For input string: "i love food"
at java.lang.NumberFormatException.forInputString(Num berFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:449)
at java.lang.Integer.parseInt(Integer.java:499)
at OddSentenceTester.main(OddSentenceTester.java:18)
And yes I do see that it exectues in the order you put it, which is why im trying to change it. Also, I can also understand now that the scanner method and JOptionpane cannot both be used. Believe it or not i'm really learning a lot with your help.
- 03-31-2012, 12:15 AM #15
Re: if else - declare if its an odd or even number - help !
You need to change the code to use the one technique. Fixing this mixed code will be a waste of time.
The parseInt() method called on line 18 wants numeric digits, not words. You should have entered a number.at java.lang.Integer.parseInt(Integer.java:499)
at OddSentenceTester.main(OddSentenceTester.java:18)If you don't understand my response, don't ignore it, ask a question.
- 03-31-2012, 12:26 AM #16
Member
- Join Date
- Mar 2012
- Posts
- 55
- Rep Power
- 0
Re: if else - declare if its an odd or even number - help !
Oh! That makes sense! I assumed that had to do with counting how many words are in the sentence... How would I change that to ask for words?
- 03-31-2012, 12:28 AM #17
Re: if else - declare if its an odd or even number - help !
Change what? Your code already asks: " Enter a sentence"
If you don't understand my response, don't ignore it, ask a question.
- 03-31-2012, 12:32 AM #18
Member
- Join Date
- Mar 2012
- Posts
- 55
- Rep Power
- 0
Re: if else - declare if its an odd or even number - help !
You;re right. If i take out the sentence. I dont get an error. but it says number of words are zero and it doesnt give me any of the other stuff i need to program to do. First thing i should probably do is change it to either scanner or JOptionPane right? When i try to do this, by replacing num = JOptionPane.showInputDialog( null, " Enter a sentence" ); with System.out.println("Enter a Sentence"); I get an error under my if stement saying that "num" hasnt been initialized.
- 03-31-2012, 12:42 AM #19
Re: if else - declare if its an odd or even number - help !
You can define a variable and assign it a value in one statement :
int vale = 123;
you do not need to use two statements:
int vale;
vale = 123;
Post your code that shows what you are doing to cause the error.If you don't understand my response, don't ignore it, ask a question.
- 03-31-2012, 12:52 AM #20
Member
- Join Date
- Mar 2012
- Posts
- 55
- Rep Power
- 0
Re: if else - declare if its an odd or even number - help !
What I just did was try and change it to scanner, and get rid of the JOptionpane.
This is what I have:
Scanner input=new Scanner(System.in);
String num;
System.out.println("Enter a Sentence.");
input.nextLine();
int number = 0;
String arr[]=st.split(" ");
System.out.println("Number of words are: "+number);
input.nextLine();
The problem I am having with is this line: String arr[]=st.split(" ");
I used in another program i wrote that asked to enter a sentence, it counted how many words were in the sentence. (which is what im trying to do for this first part). obviously arr isnt going to work. so i tried num or number. but i cant seem to figure out which one will work without error. if i do something like "n" it complains about the "st".... with what i have in there right now here is my error:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The method isOdd(String) in the type OddSentence is not applicable for the arguments (String[])
The method isEven(String) in the type OddSentence is not applicable for the arguments (String[])
The method isOdd(String) in the type OddSentence is not applicable for the arguments (String[])
at OddSentenceTester.main(OddSentenceTester.java:27)
If i take out that whole line though, the if statements give me this error:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The local variable num may not have been initialized
The local variable num may not have been initialized
The local variable num may not have been initialized
at OddSentenceTester.main(OddSentenceTester.java:27)
Thanks for answering all of my (millions) of questions.
Similar Threads
-
Boolean: different two type of declare
By hqt in forum New To JavaReplies: 5Last Post: 11-24-2011, 08:23 AM -
Declare ListArray
By tommyyyy in forum New To JavaReplies: 1Last Post: 03-20-2009, 10:08 AM -
Why do I need to declare a new Scanner object here?
By Chase in forum New To JavaReplies: 3Last Post: 09-24-2008, 11:59 PM -
Declare methods in a class
By Adiel224 in forum New To JavaReplies: 5Last Post: 09-19-2008, 10:38 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks