Just a little Java Help :)

Discussion in 'Mac Programming' started by zechmann, Oct 9, 2007.

  1. zechmann macrumors member

    Joined:
    May 8, 2007
    #1
    so i want a user to input a number and then I will check if that number is in between 5-10 AND if it is odd... if BOTH statements are not true then it will ask the use for another number (or loop back up)... heres what i got so far
    Code:
    do
                    {
                            System.out.print("How many lines? ");
                            line0 = br.readLine();
    
    
    
                            double line1 = Double.parseDouble(line0);
                    }
    while(line1 < 5 || line1 > 10);
    
    i actually haven't figured out how to find out if its odd yet but lets do this first... it returns some errors :(

    edit: it seems to have a problem with "line1"
     
  2. sord macrumors 6502

    Joined:
    Jun 16, 2004
    #2
    I'm assuming this is a homework assignment, so I'll just throw some pointers your way:
    First I would probably use an int (with Integer.parseInt) instead of a double (unless your requirements say you need to support decimals).

    Second, see what happens when you enter "test" instead of a number, then throw a try/catch block around your parse.

    Thirdly, look up the mod (%) operation. Its like divide but returns the remainder. So if a < b, a % b = a (for example 5 % 8 is just 5), a % a is 0, a + 1 % a is 1, etc.

    Hint - for the odd/even, try playing around with modding your number by a small number. If you think about it, it should be pretty easy to figure out.

    EDIT (Hint 2): Note - modulus will only work with ints, not doubles, so if you need doubles, typecast it in your check.
     
  3. zechmann thread starter macrumors member

    Joined:
    May 8, 2007
    #3
    1. we need to check if it is has decimals in it but should I just switch it to an int?

    2. should i run it with errors?

    3. I still don't get mods.... 5/8 = .625 ... so wouldn't the remained be .625 ... i completely forget what remainders do

    basically i just wanna know why it is not working cause (right now) i don't see why its not
     
  4. sord macrumors 6502

    Joined:
    Jun 16, 2004
    #4
    If you need to check for decimals, then use the double and typecast it.

    It will not build - reason: scope. You are declaring line1 of type double inside of your do block, but checking its value outside of the block. These is an error because the scope of line1 is only inside of the loop.

    5/8 is .625, but 5%8 is 5. 5 goes into 8 0 times, with 5 left over. Just like 9 goes into 8 1 times with 1 left over.
    Basically you do a-(((int)a/b)*b)
    So 5-(((int)5/8)*8), or 5-(0*8), or 5.
    Read here for more information:
    http://simple.wikipedia.org/wiki/Remainder
     
  5. ChrisBrightwell macrumors 68020

    ChrisBrightwell

    Joined:
    Apr 5, 2004
    Location:
    Huntsville, AL
    #5
    5 / 8 = 0

    5.0 / 8 = 5 / 8.0 = 5.0 / 8.0 = 0.625

    Now ... take a closer look at 5/8 (integer division).

    5/8 = 0 ... with a remainder of 5. the mod operator '%' will return the remainder.

    so ...

    5 / 8 = 0
    5 % 8 = 5

    Therefore ... x % 2 = 0 for any even number.

    10 / 2 = 5, remainder 0
    8 / 2 = 4, remainder 0
    9 / 2 = 4, remainder 1 ... therefore, 9 % 2 = 1

    Check your input buffer for stray characters, spaces, or anything else. Using the .trim() function might help.

    To see what's in your input string: System.out.println("[" + line0 + "]") should do the trick, showing you everything and enclosing it so you can see any trailing spaces or whatever.
     
  6. zechmann thread starter macrumors member

    Joined:
    May 8, 2007
    #6
    alright i get remainders now... but i declared it as an int... that doesn't seem to be the problem
     
  7. sord macrumors 6502

    Joined:
    Jun 16, 2004
  8. toddburch macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #8
    Post the error you are getting, or explain it better.

    Todd
     
  9. sord macrumors 6502

    Joined:
    Jun 16, 2004
    #9
    The problem is he declared line1 in his loop, and is trying to check it outside of it. He needs to move the line1 declaration outside of that code block.
     
  10. toddburch macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #10
    Gotchya. I didn't want to make that assumption since this is partial code.

    Todd
     
  11. zechmann thread starter macrumors member

    Joined:
    May 8, 2007
    #11
    Printerz.java:21: while expected
    double line1 = Double.parseDouble(line0);
    ^
    Printerz.java:21: '(' expected
    double line1 = Double.parseDouble(line0);
    ^
    2 errors

    i declared the double line1 after the } and before the while()
     
  12. toddburch macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
  13. zechmann thread starter macrumors member

    Joined:
    May 8, 2007
    #13
    Code:
    
    // Print
    // Author: 
    // Updated: 10/03/07
    
    import java.io.*;
    public class MagicPrint
    {
            public static void main(String[] args)
            throws java.io.IOException
            {
                    String line0;
    
                    InputStreamReader isr = new InputStreamReader(System.in);
                    BufferedReader br = new BufferedReader(isr);
                                  
                    do
                    { 
                            System.out.print("How many lines? ");
                            line0 = br.readLine();
                    }
                    int line1 = Integer.parseInt(line0);
                    while(line1 < 3 || line1 > 73);
            }
    }
     
    
     
  14. ChrisBrightwell macrumors 68020

    ChrisBrightwell

    Joined:
    Apr 5, 2004
    Location:
    Huntsville, AL
    #14
    The javac errors tell you exactly what the problems are.

    Code:
    dimwell:~/desktop chris$ javac MagicPrint.java
    MagicPrint.java:21: while expected
                    int line1 = Integer.parseInt(line0);
                    ^
    MagicPrint.java:21: '(' expected
                    int line1 = Integer.parseInt(line0);
                                                       ^
    The first error tells you that it's looking for a "while" statement, but finds some other crap. Fix that first.
     
  15. zechmann thread starter macrumors member

    Joined:
    May 8, 2007
    #15
    yea i need that int statement to make sure it converts the string into a number
     
  16. ChrisBrightwell macrumors 68020

    ChrisBrightwell

    Joined:
    Apr 5, 2004
    Location:
    Huntsville, AL
    #16
    i understand that.

    you just can't have it there.
     
  17. zechmann thread starter macrumors member

    Joined:
    May 8, 2007
    #17
    alright i fixed it... next problem... anyone know how to tell if a number has a decimal in it? (basically that its a double... with a decimal)
     
  18. ChrisBrightwell macrumors 68020

    ChrisBrightwell

    Joined:
    Apr 5, 2004
    Location:
    Huntsville, AL
    #18
    Look for a . before you convert to integer.
     
  19. zechmann thread starter macrumors member

    Joined:
    May 8, 2007
    #19
    how do you get a computer to do this... would you somehow find if the converted double has a X.1-X.9?
     
  20. toddburch macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
  21. sord macrumors 6502

    Joined:
    Jun 16, 2004
    #21
    Another way would be to check if your value == (double)((int)value)
    Basically if your input is 6.5, it will check if 6.5 == 6.0 (which it doesn't)
     

Share This Page