Page 1 of 2 12 LastLast
Results 1 to 20 of 21
  1. #1
    JMAsterson is offline Member
    Join Date
    Mar 2012
    Posts
    55
    Rep Power
    0

    Default 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!

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

    Default 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;

    }

    }

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,563
    Rep Power
    23

    Default 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.

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

    Default Re: if else - declare if its an odd or even number - help !

    if else - declare if its an odd or even number - help !-capture.png

    Well, I hope this is what you want to see because it doesnt come up in the command prompt , that box opens. Once i type into it the box disappears and the empty command prompt window just sits there until i press enter or x out. (Thanks for the help).

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,563
    Rep Power
    23

    Default Re: if else - declare if its an odd or even number - help !

    command prompt window just sits there
    Did you write the code you are testing?
    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.

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

    Default 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!

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,563
    Rep Power
    23

    Default Re: if else - declare if its an odd or even number - help !

    the empty command prompt window just sits there until i press enter
    That is exactly what the code would do after you answered the JOptionPane window.
    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.

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

    Default 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?

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,563
    Rep Power
    23

    Default 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.

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

    Default 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!

  11. #11
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,563
    Rep Power
    23

    Default 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.

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

    Default 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!");

    }

    }

  13. #13
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,563
    Rep Power
    23

    Default Re: if else - declare if its an odd or even number - help !

    gives me an error:
    Please copy and paste here the full text of the error message.

    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.

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

    Default 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.

  15. #15
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,563
    Rep Power
    23

    Default 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.

    at java.lang.Integer.parseInt(Integer.java:499)
    at OddSentenceTester.main(OddSentenceTester.java:18)
    The parseInt() method called on line 18 wants numeric digits, not words. You should have entered a number.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default 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?

  17. #17
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,563
    Rep Power
    23

    Default 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.

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

    Default 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.

  19. #19
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,563
    Rep Power
    23

    Default 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.

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

    Default 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.

Page 1 of 2 12 LastLast

Similar Threads

  1. Boolean: different two type of declare
    By hqt in forum New To Java
    Replies: 5
    Last Post: 11-24-2011, 08:23 AM
  2. Declare ListArray
    By tommyyyy in forum New To Java
    Replies: 1
    Last Post: 03-20-2009, 10:08 AM
  3. Replies: 3
    Last Post: 09-24-2008, 11:59 PM
  4. Declare methods in a class
    By Adiel224 in forum New To Java
    Replies: 5
    Last Post: 09-19-2008, 10:38 AM

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
  •