Results 1 to 6 of 6
  1. #1
    jpab29 is offline Member
    Join Date
    Jun 2012
    Location
    USA
    Posts
    5
    Rep Power
    0

    Default Very new to Java. Please help me correct my code.

    Hello everyone! I am new to Java. This is my 4 week in Java Programming 1 class. We only meet once a week as a class and everything is jam-packed I can't keep up.

    I have a homework which I need to work on but I wanted to try out simple practice codes so that I may be able to modify it and apply it to my program.

    Basically, I need to find the largest number in my array and display it. That's my goal for now. But my return value gets a red line, I do not know why. I apologize for being so silly. If you could help me out, I would greatly appreciate it.

    Very new to Java. Please help me correct my code.-error.jpg

    next couple things I need to do is subtract the lower values from the largest value and display it too. But I'll do that later once I get this solved. Thanks so much. Homework is due in 12 hours, and I am so stuck as of now.

  2. #2
    jpab29 is offline Member
    Join Date
    Jun 2012
    Location
    USA
    Posts
    5
    Rep Power
    0

    Default Re: Very new to Java. Please help me correct my code.

    I just realized, there's inconsistency of int and double. sorry. I changed it all to double. but the if statement turned red haha

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Very new to Java. Please help me correct my code.

    You didn't change the for loop index, i, to a double did you? Little details matter.

    Welcome to the forum. Next time a suggestion: please post your code itself in here with code tags and paste in the error message as well. Also, consider your question title as if it were a headline summarizing your problem. This should summarize the error message, not "correct my code".

  4. #4
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default Re: Very new to Java. Please help me correct my code.

    OP, please next time copy+paste your code in
    Java Code:
     /*copy paste your code here */
    tags, instead of making .jpg file.

    Here is code from OP .jpg file:
    Java Code:
    public class Main {
    	public static void main(String[] args) {
    		// This is an example of a 1-dimensional array.
    		// type arrayname[] = new type[size]; is the format
    		// there are 4 indeces (index), but counting will being at 0 and ends at 3, that is 4 in total
    		
    		double stockPrices[] = new double[10];
    		
    		stockPrices[0] = 23.5;
    		stockPrices[1] = 22.9;
    		stockPrices[2] = 24.3;
    		
    		System.out.println("Maximum value = " + getMaxValue(stockPrices));
    		
    	}
    	
    	// Find maximum (largest) value in array using loop
    	public static int getMaxValue(double[] stockPrices) {
    		double maxValue = stockPrices[0];
    		
    		for(int i = 1; i < stockPrices.length; i++) {
    			if(stockPrices[i] > maxValue) {
    				maxValue = stockPrices[i];
    			}
    		}
    		return maxValue;
    	}
    }

  5. #5
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default Re: Very new to Java. Please help me correct my code.

    @jpab29:

    stockPrices[] is double array, maxValue is double value, and then you have written that your function getMaxValue returns int even if you're working with double variables so this function should return double instead of int.

  6. #6
    jpab29 is offline Member
    Join Date
    Jun 2012
    Location
    USA
    Posts
    5
    Rep Power
    0

    Default Re: Very new to Java. Please help me correct my code.

    Quote Originally Posted by cselic View Post
    @jpab29:

    stockPrices[] is double array, maxValue is double value, and then you have written that your function getMaxValue returns int even if you're working with double variables so this function should return double instead of int.
    Thank you everyone for your help! It is working now! I am now applying it to another program.

    I will also work on the code pasting. Thank you for your patience! I apologize for my newness.

Similar Threads

  1. Please correct this JSP code
    By rajesh4851 in forum New To Java
    Replies: 5
    Last Post: 06-13-2011, 12:12 PM
  2. Replies: 10
    Last Post: 06-06-2010, 10:12 AM
  3. Replies: 1
    Last Post: 10-24-2009, 02:13 AM

Tags for this Thread

Posting Permissions

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