Results 1 to 19 of 19
Thread: Dividing 3 items, please help.
- 02-24-2011, 04:03 AM #1
Member
- Join Date
- Feb 2011
- Posts
- 34
- Rep Power
- 0
- 02-24-2011, 04:10 AM #2
"is not working" provides zero information. How is it not working. What happens and what do you expect to happen instead.
The output is 6. If you are expecting it to be different then please explain.Java Code:class Test { public static void main(String[] args) { int one = 50; int two = 4; int three = 2; System.out.println((one / two) / three); } }
- 02-24-2011, 04:12 AM #3
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
Perhaps it's the ordering of the division that is causing you the problems
(10/2)/3 != (2/10)/3
If this is not the case ignore :)
- 02-24-2011, 04:12 AM #4
Member
- Join Date
- Feb 2011
- Posts
- 10
- Rep Power
- 0
I really hope I can help you,but could you show the detail of this problem or show your code,so that I can understand what help do you need
- 02-24-2011, 04:32 AM #5
Member
- Join Date
- Feb 2011
- Posts
- 34
- Rep Power
- 0
Sure no problem. I can not declare the amounts b/c they are user inputted. Here is my code, it's kind of long.
Java Code:public class Widgets { private String name; private int hoursWorked; private int perHour; private int totalDays; public Widgets(String widgets, int production, int hours, int days) { name = widgets; hoursWorked = production; perHour = hours; totalDays = days; } public void setName(String widgets) { name = widgets; } public void setHoursWorked(int production) { hoursWorked = production; } public void setPerHour(int hours) { perHour = hours; } public void setTotalDays(int days) { totalDays = days; } public String getName() { return name; } public int getHoursWorked() { return hoursWorked; } public int getPerHour() { return perHour; } public int getTotalDays() { return totalDays; } }Java Code:import javax.swing.JOptionPane; public class TestWidgets { public static void main (String [] args) { String testWidgets; int testHours; int testProduction; int testDays; { testWidgets = JOptionPane.showInputDialog("What is your current quota?"); testHours = Integer.parseInt( JOptionPane.showInputDialog("How many hours have been worked? ")); testProduction = Integer.parseInt( JOptionPane.showInputDialog("Currently how many Widgets are you making per hour?")); [COLOR="Yellow"][COLOR="Red"]testDays = (testWidgets / testHours) / (testEnding) ;[/COLOR][/COLOR] } JOptionPane.showMessageDialog(null, "You need " + testWidgets + "." + " \nCurrently you have worked" + testHours + " hours. \nHow many Widgets are you currently making per hour? " + testProduction + "\nIt will take you " + testDays + " days to meet" + " your quota!"); } }
- 02-24-2011, 04:35 AM #6
sigh
So what is it about that line of code that "is not working". WE DON'T READ MINDS.
If you get a compiler error copy and paste the exact message. If it runs but gives the wrong answer tells what answer it gives and what you expect instead.
For crying out loud this is a simple matter of communication skills and nothing to do with being new to programming or the forum.
- 02-24-2011, 04:51 AM #7
Member
- Join Date
- Feb 2011
- Posts
- 34
- Rep Power
- 0
This is what the compiler is saying:
----jGRASP exec: javac -g TestWidgets.java
TestWidgets.java:32: operator / cannot be applied to java.lang.String,int
testDays = (testWidgets / testHours) / (testEnding) ; }
^
TestWidgets.java:32: cannot find symbol
symbol : variable testEnding
location: class TestWidgets
testDays = (testWidgets / testHours) / (testEnding) ; }
^
TestWidgets.java:32: illegal start of type
testDays = (testWidgets / testHours) / (testEnding) ; }
^
3 errors
----jGRASP wedge2: exit code for process is 1.
- 02-24-2011, 04:57 AM #8
:headdeask:
Did you bother to read the error messages?
Does "hello" / 3 make sense to you?TestWidgets.java:32: operator / cannot be applied to java.lang.String,int
Where have you declared that variable?TestWidgets.java:32: cannot find symbol
symbol : variable testEnding
- 02-24-2011, 05:23 AM #9
Member
- Join Date
- Feb 2011
- Posts
- 34
- Rep Power
- 0
That was the problem, thanks for your help. :)
- 02-24-2011, 05:25 AM #10
Member
- Join Date
- Feb 2011
- Posts
- 34
- Rep Power
- 0
Do you have time for one more question if I give you all the info? lol
- 02-24-2011, 05:30 AM #11
Don't ask if you can ask a question. Just post it. Someone is bound to help.
- 02-24-2011, 05:34 AM #12
Member
- Join Date
- Feb 2011
- Posts
- 34
- Rep Power
- 0
Ok. I'm getting the following errors and I don't understand why or how it's an illegal start of an expression or why a ; is expected. All the errors are the same just with different variables.
Java Code:public class Pet { private String name; private String type; private int age; public Pet(String name, String type, int age) { name = pet; type = species; age = age; } public void setName(String pet) { name = pet; { public void setType(String species) { type = species; } public void setAge(int age) { age = age; } public String getName() { return name; } public String getType() { return type; } public int getAge() { return age; } }
----jGRASP exec: javac -g Pet.java
Pet.java:28: illegal start of expression
public void setType(String species)
^
Pet.java:28: illegal start of expression
public void setType(String species)
^
Pet.java:28: ';' expected
public void setType(String species)
^
Pet.java:28: ';' expected
public void setType(String species)
^
Pet.java:33: illegal start of expression
public void setAge(int age)
^
Pet.java:33: illegal start of expression
public void setAge(int age)
^
Pet.java:33: ';' expected
public void setAge(int age)
^
Pet.java:33: ';' expected
public void setAge(int age)
^
Pet.java:38: illegal start of expression
public String getName()
^
Pet.java:38: ';' expected
public String getName()
^
Pet.java:43: illegal start of expression
public String getType()
^
Pet.java:43: ';' expected
public String getType()
^
Pet.java:48: illegal start of expression
public int getAge()
^
Pet.java:48: ';' expected
public int getAge()
^
Pet.java:52: reached end of file while parsing
}
- 02-24-2011, 05:36 AM #13
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
Look very carefully at all your curly braces {}
- 02-24-2011, 05:44 AM #14
Member
- Join Date
- Feb 2011
- Posts
- 34
- Rep Power
- 0
Lol I don't see one missing or one out of place. :(
- 02-24-2011, 05:52 AM #15
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
- 02-24-2011, 06:03 AM #16
Member
- Join Date
- Feb 2011
- Posts
- 34
- Rep Power
- 0
I'm an IDIOT lol thanks.
- 02-24-2011, 06:31 AM #17
Member
- Join Date
- Feb 2011
- Posts
- 34
- Rep Power
- 0
Ok last freaking problem and of course I have an error. What am I doing wrong, I don't understand why it is not finding the variable.
Java Code:public class Pet { private String name; private String type; private int age; public Pet(String pet, String species, int age) { name = pet; type = species; age = age; } public void setName(String pet) { name = pet; } public void setType(String species) { type = species; } public void setAge(int age) { age = age; } public String getName() { return name; } public String getType() { return type; } public int getAge() { return age; } }----jGRASP exec: javac -g TestPet.javaJava Code:import javax.swing.JOptionPane; public class TestPet{ public static void main (String [] args) { String testPet; String testSpecies; int testAge; } { testPet = JOptionPane.showInputDialog("What is your pet's name?"); testSpecies = JOptionPane.showInputDialog("What kind of animal is your pet?"); testAge = Integer.parseInt( JOptionPane.showInputDialog("How old is " + testPet + ".")); JOptionPane.showMessageDialog(null, "Your " + testSpecies + "'s name is " + testPet + ". " + testPet + " is " + testAge + " years old."); } }
TestPet.java:22: cannot find symbol
symbol : variable testPet
location: class TestPet
testPet = JOptionPane.showInputDialog("What is your pet's name?");
^
TestPet.java:23: cannot find symbol
symbol : variable testSpecies
location: class TestPet
testSpecies = JOptionPane.showInputDialog("What kind of animal is your pet?");
^
TestPet.java:24: cannot find symbol
symbol : variable testAge
location: class TestPet
testAge = Integer.parseInt(
^
TestPet.java:25: cannot find symbol
symbol : variable testPet
location: class TestPet
JOptionPane.showInputDialog("How old is " + testPet + "."));
^
TestPet.java:27: cannot find symbol
symbol : variable testSpecies
location: class TestPet
JOptionPane.showMessageDialog(null, "Your " + testSpecies + "'s name is " + testPet +
^
TestPet.java:27: cannot find symbol
symbol : variable testPet
location: class TestPet
JOptionPane.showMessageDialog(null, "Your " + testSpecies + "'s name is " + testPet +
^
TestPet.java:28: cannot find symbol
symbol : variable testPet
location: class TestPet
". " + testPet + " is " + testAge + " years old.");
^
TestPet.java:28: cannot find symbol
symbol : variable testAge
location: class TestPet
". " + testPet + " is " + testAge + " years old.");
^
8 errors
- 02-24-2011, 06:39 AM #18
Senior Member
- Join Date
- Feb 2011
- Posts
- 118
- Rep Power
- 0
Get rid of that pair of curly braces in main(), after int testAge;. That's preventing your variable declarations from being in the same scope as the rest of main().
- 02-24-2011, 06:42 AM #19
Member
- Join Date
- Feb 2011
- Posts
- 34
- Rep Power
- 0
Similar Threads
-
Dividing in Java
By hawaiifiver in forum New To JavaReplies: 19Last Post: 03-11-2011, 11:09 AM -
Sending Arrays into methods then dividing for an average.
By freebirdcal in forum New To JavaReplies: 8Last Post: 02-03-2011, 10:41 PM -
Trying to store a decimal between 0&1 after dividing by 100
By jebbo in forum New To JavaReplies: 2Last Post: 10-28-2010, 01:56 PM -
dividing video files into frames..
By swathi palla in forum AWT / SwingReplies: 0Last Post: 02-15-2009, 03:39 PM -
Dividing numbers with remainders showing
By denisdoherty in forum New To JavaReplies: 16Last Post: 04-24-2008, 05:39 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks