Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

zechmann

macrumors member
Original poster
May 8, 2007
92
0
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"
 

sord

macrumors 6502
Jun 16, 2004
352
0
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.
 

zechmann

macrumors member
Original poster
May 8, 2007
92
0
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
 

sord

macrumors 6502
Jun 16, 2004
352
0
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
 

ChrisBrightwell

macrumors 68020
Apr 5, 2004
2,294
0
Huntsville, AL
3. I still don't get mods.... 5/8 = .625 ... so wouldn't the remained be .625 ... i completely forget what remainders do

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.
 

zechmann

macrumors member
Original poster
May 8, 2007
92
0
alright i get remainders now... but i declared it as an int... that doesn't seem to be the problem
 

sord

macrumors 6502
Jun 16, 2004
352
0
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.
 

zechmann

macrumors member
Original poster
May 8, 2007
92
0
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()
 

zechmann

macrumors member
Original poster
May 8, 2007
92
0
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);
        }
}
 

ChrisBrightwell

macrumors 68020
Apr 5, 2004
2,294
0
Huntsville, AL
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.
 

zechmann

macrumors member
Original poster
May 8, 2007
92
0
yea i need that int statement to make sure it converts the string into a number
 

zechmann

macrumors member
Original poster
May 8, 2007
92
0
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)
 

zechmann

macrumors member
Original poster
May 8, 2007
92
0
how do you get a computer to do this... would you somehow find if the converted double has a X.1-X.9?
 

sord

macrumors 6502
Jun 16, 2004
352
0
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)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.