Results 1 to 19 of 19
  1. #1
    Quizzle23 is offline Member
    Join Date
    Feb 2011
    Posts
    34
    Rep Power
    0

    Default Dividing 3 items, please help.

    Can someone tell me how to format the division of 3 items?

    I have testDays = (testWidgets / testHours) / (testEnding) and that is not working, what other ways can this be formatted without having to change anything else?

    Thanks in advance, oh I kinda need this ASAP :)

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default

    "is not working" provides zero information. How is it not working. What happens and what do you expect to happen instead.
    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);
        }
    
    }
    The output is 6. If you are expecting it to be different then please explain.

  3. #3
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    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 :)

  4. #4
    liyuyu_852000 is offline Member
    Join Date
    Feb 2011
    Posts
    10
    Rep Power
    0

    Default

    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

  5. #5
    Quizzle23 is offline Member
    Join Date
    Feb 2011
    Posts
    34
    Rep Power
    0

    Default

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

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default

    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.

  7. #7
    Quizzle23 is offline Member
    Join Date
    Feb 2011
    Posts
    34
    Rep Power
    0

    Default

    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.

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default

    :headdeask:

    Did you bother to read the error messages?

    TestWidgets.java:32: operator / cannot be applied to java.lang.String,int
    Does "hello" / 3 make sense to you?

    TestWidgets.java:32: cannot find symbol
    symbol : variable testEnding
    Where have you declared that variable?

  9. #9
    Quizzle23 is offline Member
    Join Date
    Feb 2011
    Posts
    34
    Rep Power
    0

    Default

    That was the problem, thanks for your help. :)

  10. #10
    Quizzle23 is offline Member
    Join Date
    Feb 2011
    Posts
    34
    Rep Power
    0

    Default

    Do you have time for one more question if I give you all the info? lol

  11. #11
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default

    Don't ask if you can ask a question. Just post it. Someone is bound to help.

  12. #12
    Quizzle23 is offline Member
    Join Date
    Feb 2011
    Posts
    34
    Rep Power
    0

    Default

    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
    }

  13. #13
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Look very carefully at all your curly braces {}

  14. #14
    Quizzle23 is offline Member
    Join Date
    Feb 2011
    Posts
    34
    Rep Power
    0

    Default

    Lol I don't see one missing or one out of place. :(

  15. #15
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Quote Originally Posted by Quizzle23 View Post
    Lol I don't see one missing or one out of place. :(
    Matching { } ? All of them? setName method....

  16. #16
    Quizzle23 is offline Member
    Join Date
    Feb 2011
    Posts
    34
    Rep Power
    0

    Default

    I'm an IDIOT lol thanks.

  17. #17
    Quizzle23 is offline Member
    Join Date
    Feb 2011
    Posts
    34
    Rep Power
    0

    Default

    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;
    		}
    }
    Java 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.");
    	}
    }
    ----jGRASP exec: javac -g TestPet.java

    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

  18. #18
    NRitH is offline Senior Member
    Join Date
    Feb 2011
    Posts
    118
    Rep Power
    0

    Default

    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().

  19. #19
    Quizzle23 is offline Member
    Join Date
    Feb 2011
    Posts
    34
    Rep Power
    0

    Default

    That gave me this:


    TestPet.java:15: ';' expected
    public static void main (String [] args)
    ^
    1 error


    Then I added the ; and got this:

    TestPet.java:15: missing method body, or declare abstract
    public static void main (String [] args);
    ^
    1 error

Similar Threads

  1. Dividing in Java
    By hawaiifiver in forum New To Java
    Replies: 19
    Last Post: 03-11-2011, 11:09 AM
  2. Sending Arrays into methods then dividing for an average.
    By freebirdcal in forum New To Java
    Replies: 8
    Last Post: 02-03-2011, 10:41 PM
  3. Replies: 2
    Last Post: 10-28-2010, 01:56 PM
  4. dividing video files into frames..
    By swathi palla in forum AWT / Swing
    Replies: 0
    Last Post: 02-15-2009, 03:39 PM
  5. Dividing numbers with remainders showing
    By denisdoherty in forum New To Java
    Replies: 16
    Last Post: 04-24-2008, 05:39 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •