Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 16 of 16

Thread: need help

  1. #1
    Junior Member
    Join Date
    Dec 2010
    Posts
    6
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default need help

    my teacher posted this exercise and i still dont understand how to do it. i fixed some of the little things that i knew how to fix but the rest of it im not sure of. can anyone help?
    Attached Images Attached Images

  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: need help

    It's better if you post all of your code, see how to in my signature.

    also, it's kinda hard to help when we don't know what's going wrong.

    What are you trying to do?

  3. #3
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: need help

    Wow, your user name pretty much sums it up. Read this before posting another question: How To Ask Questions The Smart Way

    This wouldn't hurt either: SSCCE

  4. #4
    Junior Member
    Join Date
    Dec 2010
    Posts
    6
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: need help

    sorry its my first post thats why all the code wasnt shown. and if the errors and output isnt what you meant sorry

    import java.util.Scanner;
     
    public class Test3Program
    {
       public static void main(String[] args)
    	{
     
     
    		// Replace the ??? with the appropriate constructor.  
    	[exception] Scanner myIn = new ???; [/exception]
     
    		String itemDescription;  // Temporary variable to hold each description entered
     
    		String scanResult;  // Temporary String variable to hold each value entered
     
    		double itemValue;  // Temporary double variable to hold each value converted from scanResult 
     
    		int numOfDataItems,  // Number of data items to enter/entered
    		    minLoc,          // Location of the minimum element.
    			 maxLoc;          // Location of the maximum element.
     
    		do
    		{
    		   System.out.print("Please enter the number of data items (minimum of 10 items): ");
     
    		   // Replace the ??? with the appropriate method to parse the String read into an integer.
    		   numOfDataItems = Integer.parseInteger(myIn.nextLine());
    		}
    		while  (numOfDataItems < 10); // Enter at least 10 data points
     
    		Item[] dataItems = new Item[numOfDataItems];  // Create an array of Items
     
    		// Replace the ??? with the appropriate item to control the loop to enter all of the data points.
    	[exception]	for (int count=0; count<dataItems.???; count++) [/exception]
    		{
    		   System.out.print("Please enter a description of data item #"+(count+1)+": ");
     
    			itemDescription = myIn.nextLine();
     
    		   System.out.print("Please enter the value of data item #"+(count+1)+": ");
     
    			scanResult = myIn.nextLine();
     
    			itemValue = Double.parseDouble(scanResult);
     
    			// Replace the ??? with the appropriate identifier for the array index
    	[exception]	dataItems[???] = new Item(itemDescription, itemValue); [/exception]
    		}
     
    		double min = dataItems[0].getValue(), 
    		       max = dataItems[0].getValue(),
    				 sum = dataItems[0].getValue();
     
    		minLoc = 0;
    		maxLoc = 0;
     
    		for (int count = 1; count<dataItems.length; count++)
    		{
    		   // Replace the ??? with the appropriate identifier for the array index to accumulate the values in the sum.
    		[exception]   sum = sum + dataItems[???].getValue(); [/exception]
     
    		   if (dataItems[count].getValue() < min)
    			{
    			   min = dataItems[count].getValue();
    				minLoc = count;
    			}
    			else if (dataItems[count].getValue() > max)
    			     {
    			        // Replace the ??? with the appropriate array element
    			   [exception]     max = dataItems[???].getValue(); [/exception]
    					  maxLoc = count;
    				  }
    		}
    [console]		
    		double average = sum / numOfDataItems;
     
    		System.out.println("The highest value of the data values was "+max+".");
    		System.out.println("The highest value item is described as a(n):" + dataItems[maxLoc].getDescription()+".");
     
    		// Replace the ??? with the appropriate identifier
    		System.out.println("The lowest value of the data values was "+min+".");
    		System.out.println("The lowest value item is described as a(n):" + dataItems[minLoc].getDescription()+".");
     
    		System.out.println("The average of the item's data values was "+average+".");
     
    		// Replace the ??? with the data item at the position equal to the last digit of your student ID number.
    		System.out.println(???);  // Include the description and value of data item.
    	[/console]	
    	}
    }

    im not sure if u needed to see this one too but it cant hurt
    // Class - Item (used to contain a value and description of the value).
    public class Item
    {
       private String description;
    	private double value;
     
    	public Item()  // Default constructor.
    	{
    	   this("", 0.0);
    	}
     
    	public Item(String description, double value)  // Detailed constructor.
    	{
    	   setDescription(description);
    		setValue(value);
    	}
     
    	public void setDescription(String description)   // Set the object's description attribute.
    	{
    	   this.description = description;
    	}
     
    	public void setValue(double value)  // Set the object's value attribute.
    	{
    	   this.value = value;
    	}
     
    	public String getDescription()  // Return a String containing the object's description attributes.
    	{
    	   return this.description;
    	}
     
    	public double getValue()  // Return a double containing the object's value attributes.
    	{
    	   return this.value;
    	}
     
    	public String toString()  // Return a String containing all of the object's attributes.
    	{
    	   return "Description: "+getDescription()+"\n"+
    		       "Value: "+getValue()+"\n";
    	}
     
    }

  5. #5
    Junior Member
    Join Date
    Dec 2010
    Posts
    6
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: need help

    @kevinworkman
    its my first post and ive only been using java for about a month and a half, granted that might be enough time to figure this out but my teacher isnt the best, so i have to go off of guessing alot of the time

  6. #6
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: need help

    Are you scanning from a file or from the console?

    If from the console

    do something like

    Scanner console = new Scanner(System.in);

    if from a file

    Scanner input = new Scanner("FileName.extension");
    and, if from a file, your main method needs to throw a FileNotFoundException

    like this

    public static void main(String[] args) throws FileNotFoundException

  7. The Following User Says Thank You to javapenguin For This Useful Post:

    Lazy (December 10th, 2010)

  8. #7
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: need help

    Have an array of Items that are the length of number of items entered by Scanner.

    Item[] items = new Item[numOfDataItems];

    Have a for loop for each item

    for (int i =0; i < items.length; i++)
    {
    System.out.println("Enter Description for item " + (i+1) + ".");
    itemDescription = myIn.nextLine();
    System.out.print("Please enter the value of data item #"+(count+1)+": ");

    scanResult = myIn.nextLine();

    itemValue = Double.parseDouble(scanResult);

    items[i] = new Item(itemDescription, itemValue);
    }

  9. The Following User Says Thank You to javapenguin For This Useful Post:

    Lazy (December 10th, 2010)

  10. #8
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: need help

    double min = dataItems[0].getValue(),
    double max = dataItems[0].getValue(),
    double sum = dataItems[0].getValue();

  11. The Following User Says Thank You to javapenguin For This Useful Post:

    Lazy (December 10th, 2010)

  12. #9
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: need help

    max = dataItems[???].getValue();
    replace ??? with
    count
    That's your variable in the for loop.

  13. The Following User Says Thank You to javapenguin For This Useful Post:

    Lazy (December 10th, 2010)

  14. #10
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: need help

    Quote Originally Posted by Lazy View Post
    @kevinworkman
    its my first post and ive only been using java for about a month and a half, granted that might be enough time to figure this out but my teacher isnt the best, so i have to go off of guessing alot of the time
    That's why I posted the two links. Did you read them? Where's your SSCCE? What errors are you seeing? Are they Exceptions? Is the program acting differently from what you're expecting? What are you expecting? What happens instead? Point out any line numbers from the Exceptions' stack trace in the SSCCE that you post so that we don't have to count.

  15. #11
    Junior Member
    Join Date
    Dec 2010
    Posts
    6
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: need help

    Quote Originally Posted by KevinWorkman View Post
    That's why I posted the two links. Did you read them? Where's your SSCCE? What errors are you seeing? Are they Exceptions? Is the program acting differently from what you're expecting? What are you expecting? What happens instead? Point out any line numbers from the Exceptions' stack trace in the SSCCE that you post so that we don't have to count.
    the SSCCE i dont have one. my teacher just said heres an exercise and you have the weekend to fix it. the other stuff i honestly dont know he never provides/provided an answer key for us to look at until we get to class

  16. #12
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: need help

    Quote Originally Posted by Lazy View Post
    the SSCCE i dont have one. my teacher just said heres an exercise and you have the weekend to fix it. the other stuff i honestly dont know he never provides/provided an answer key for us to look at until we get to class
    I'd recommend creating one. Part of debugging and solving problems such as this involves breaking down the problem into smaller and more manageable pieces which can be fixed. Creating an SSCCE helps you in this process (and quite often just in the process of creating one the problem becomes self evident - if not we are here to help). So for the code you posted, does it compile? If not, break it down until it does. If it does compile, do you get exceptions? What are there? Where are they thrown? Again, break the code down until they are not thrown. If you break it down to an extreme minimum and still can't get it to compile or still get errors, post the code for help with a description of the errors..
    Last edited by copeg; December 10th, 2010 at 03:29 PM.

  17. #13
    Junior Member
    Join Date
    Dec 2010
    Posts
    6
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: need help

    import java.util.Scanner;
     
    public class Test3Program
    {
       public static void main(String[] args)
    	{
     
     
    		// Replace the ??? with the appropriate constructor.  
    		Scanner myIn = new Scanner(Item.java);
     
    		String itemDescription;  // Temporary variable to hold each description entered
     
    		String scanResult;  // Temporary String variable to hold each value entered
     
    		double itemValue;  // Temporary double variable to hold each value converted from scanResult 
     
    		int numOfDataItems,  // Number of data items to enter/entered
    		    minLoc,          // Location of the minimum element.
    			 maxLoc;          // Location of the maximum element.
     
    		do
    		{
    		   System.out.print("Please enter the number of data items (minimum of 10 items): ");
     
    		   // Replace the ??? with the appropriate method to parse the String read into an integer.
    		   numOfDataItems = Integer.parseInteger(myIn.nextLine());
    		}
    		while  (numOfDataItems < 10); // Enter at least 10 data points
     
    		Item[] dataItems = new Item[numOfDataItems];  // Create an array of Items
     
    		// Replace the ??? with the appropriate item to control the loop to enter all of the data points.
    		for (int count=0; count<dataItems.length; count++)
    		{
    		   System.out.print("Please enter a description of data item #"+(count+1)+": ");
     
    			itemDescription = myIn.nextLine();
     
    		   System.out.print("Please enter the value of data item #"+(count+1)+": ");
     
    			scanResult = myIn.nextLine();
     
    			itemValue = Double.parseDouble(scanResult);
     
    			// Replace the ??? with the appropriate identifier for the array index
    			dataItems[count+1] = new Item(itemDescription, itemValue);
    		}
     
    		double min = dataItems[0].getValue(), 
    		       max = dataItems[0].getValue(),
    				 sum = dataItems[0].getValue();
     
    		minLoc = 0;
    		maxLoc = 0;
     
    		for (int count = 1; count<dataItems.length; count++)
    		{
    		   // Replace the ??? with the appropriate identifier for the array index to accumulate the values in the sum.
    		   sum = sum + dataItems[count].getValue();
     
    		   if (dataItems[count].getValue() < min)
    			{
    			   min = dataItems[count].getValue();
    				minLoc = count;
    			}
    			else if (dataItems[count].getValue() > max)
    			     {
    			        // Replace the ??? with the appropriate array element
    			        max = dataItems[count].getValue();
    					  maxLoc = count;
    				  }
    		}
     
    		double average = sum / numOfDataItems;
     
    		System.out.println("The highest value of the data values was "+max+".");
    		System.out.println("The highest value item is described as a(n):" + dataItems[maxLoc].getDescription()+".");
     
    		// Replace the ??? with the appropriate identifier
    		System.out.println("The lowest value of the data values was "+min+".");
    		System.out.println("The lowest value item is described as a(n):" + dataItems[minLoc].getDescription()+".");
     
    		System.out.println("The average of the item's data values was "+average+".");
     
    		// Replace the ??? with the data item at the position equal to the last digit of your student ID number.
    		System.out.println("???");  // Include the description and value of data item.
     
    	}
    }

    i have 2 exceptions in this after compiling. the "(error)" is where the ^ is supposed to be.

     

    Test3Program.java:16: cannot find symbol
    symbol : variable java
    location: class Item
    Scanner myIn = new(error) Scanner(Item.java);
    ^
    Test3Program.java:33: cannot find symbol
    symbol : method parseInteger(java.lang.String)
    location: class java.lang.Integer
    numOfDataItem(error)s = Integer.parseInteger(myIn.nextLine());
    ^
    2 errors




    one other thing im not sure of is if this looks right... line 52/53
    // Replace the ??? with the appropriate identifier for the array index
    dataItems[count+1] = new Item(itemDescription, itemValue);

  18. #14
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: need help

    What is Item.java? Is it a file? Do you mean "Item.java"? Take a look at the Scanner API to see what kinds of arguments its constructors take.

    Similarly, consult the API for Integer. Where do you see a parseInteger method?

    Java Platform SE 6

  19. #15
    Junior Member
    Join Date
    Dec 2010
    Posts
    6
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: need help

    Quote Originally Posted by KevinWorkman View Post
    What is Item.java? Is it a file? Do you mean "Item.java"? Take a look at the Scanner API to see what kinds of arguments its constructors take.

    Similarly, consult the API for Integer. Where do you see a parseInteger method?

    Java Platform SE 6
    yes, item.java is a file, if were to put quotes around it then wouldnt that show up as written text? and the parseInteger is the way my teacher puts it in there and it works when he does it so i dont change it

  20. #16
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: need help

    Quote Originally Posted by Lazy View Post
    yes, item.java is a file, if were to put quotes around it then wouldnt that show up as written text? and the parseInteger is the way my teacher puts it in there and it works when he does it so i dont change it
    Look at the API for Scanner to see how to properly load files.

    Scanner (Java Platform SE 6)