PDA

View Full Version : Just a little Java Help :)




zechmann
Oct 9, 2007, 09:58 PM
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

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
Oct 9, 2007, 10:10 PM
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 (&#37;) 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
Oct 9, 2007, 10:17 PM
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
Oct 9, 2007, 10:35 PM
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
Oct 9, 2007, 10:38 PM
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
Oct 9, 2007, 10:53 PM
alright i get remainders now... but i declared it as an int... that doesn't seem to be the problem

sord
Oct 9, 2007, 10:56 PM
As I said, your problem is scope.

toddburch
Oct 9, 2007, 10:57 PM
Post the error you are getting, or explain it better.

Todd

sord
Oct 9, 2007, 11:01 PM
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.

toddburch
Oct 9, 2007, 11:06 PM
Gotchya. I didn't want to make that assumption since this is partial code.

Todd

zechmann
Oct 9, 2007, 11:08 PM
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()

toddburch
Oct 10, 2007, 12:26 AM
Can you post the whole program please?

Todd

zechmann
Oct 10, 2007, 01:00 AM
// 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
Oct 10, 2007, 01:18 AM
The javac errors tell you exactly what the problems are.

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
Oct 10, 2007, 01:20 AM
yea i need that int statement to make sure it converts the string into a number

ChrisBrightwell
Oct 10, 2007, 01:21 AM
yea i need that int statement to make sure it converts the string into a number

i understand that.

you just can't have it there.

zechmann
Oct 10, 2007, 02:21 AM
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)

ChrisBrightwell
Oct 10, 2007, 02:28 AM
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)

Look for a . before you convert to integer.

zechmann
Oct 10, 2007, 03:11 AM
how do you get a computer to do this... would you somehow find if the converted double has a X.1-X.9?

toddburch
Oct 10, 2007, 07:40 AM
BufferedReader.readLine() returns a String.

http://java.sun.com/javase/6/docs/api/java/io/BufferedReader.html#readLine()

The String class has a method to find a character, called String.indexOf().

http://java.sun.com/javase/6/docs/api/java/lang/String.html#indexOf(int)

Todd

sord
Oct 10, 2007, 06:08 PM
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)