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 8 of 8

Thread: How to find where the biggest number is in an array

  1. #1
    Junior Member
    Join Date
    May 2013
    Posts
    4
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to find where the biggest number is in an array

    Hi i've been trying to figure out where to find the biggest number in an array. I know how to find the biggest number but i need to find out where the biggest number is, like which row or whatever it's in.

    import java.util.Scanner;
    public class array {
     
    	public static void main (String args[]) {
     
    	Scanner keyboard = new Scanner(System.in);
     
    	int r[] = new int[50];
    	int n=0;
    	int x, bigSoFar;
     
    	System.out.print("Enter list: ");
    	x = keyboard.nextInt();
     
    	while( x!= -999); {
    		r[n] = x;
    		n++;
    		x = keyboard.nextInt();
    	}
     
    	for(int k = 0; k < n; k++)
    		if(r[k]> bigSoFar) {
     
    			bigSoFar = r[k]; 
    		}	
     
    		System.out.println(r[k]); 
     
    	}	
    }

    Although first i'm having trouble trying to get my first code to work, i keep getting this error
    array.java:34: error: cannot find symbol
    System.out.println(r[k]);
    ^
    symbol: variable k
    location: class array
    1 error


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: How to find where the biggest number is in an array

    Check the { }
    It seems the compiler believes k is not defined within scope every time it is being used.
    Look at line 34's use of k

  3. #3
    Junior Member
    Join Date
    May 2013
    Posts
    4
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to find where the biggest number is in an array

    k is defined as k=0 inside the parenthesis and as for the braces, where else could i place them?

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: How to find where the biggest number is in an array

    Check the positioning of the { } again. For some reason the scope of the for loop does not include k on line 34.
    class ForDemo {
        public static void main(String[] args){
             for(int i=1; i<11; i++){
                  System.out.println("Count is: " + i);
             }
             for(int i=1; i<11; i++)
             System.out.println("Count is: " + i);
             System.out.println("I am outside the loop);
        }
    }
    If the for loop is not followed by a curly brace, but instead followed by a statement ending with a semicolon, the scope of the for loop terminates at the semicolon. Demonstrated by the class above, a modified version from the java for loop tutorial

  5. #5
    Junior Member
    Join Date
    May 2013
    Posts
    4
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to find where the biggest number is in an array

    ok I fixed it and I was able to compile it but now whenever i try to put a list in in the command prompt it will just sit there and not do anything, just freeze and think. I can't figure it out

  6. #6
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,468
    My Mood
    Grumpy
    Thanks
    0
    Thanked 158 Times in 149 Posts

    Default Re: How to find where the biggest number is in an array

    Post updated code.
    Improving the world one idiot at a time!

  7. #7
    Junior Member
    Join Date
    May 2013
    Posts
    4
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to find where the biggest number is in an array

    import java.util.Scanner;
     
    public class array {
     
    	public static void main (String args[]) {
     
    	Scanner keyboard = new Scanner(System.in);
     
    	int r[] = new int[50];
    	int n=0;
    	int x, bigSoFar=0;
     
    	System.out.print("Enter list: ");
    	x = keyboard.nextInt();
     
    	while( x!= -999); {
     
    		r[n] = x;
    		n++;
    		x = keyboard.nextInt();
     
    	}
     
    	for(int k = 0; k < n; k++){
    		if(r[k] > bigSoFar) {
     
    			bigSoFar = r[k]; 
    		}	
     
    		System.out.println(r[k]); 
    		}
     
    	}
     
     
    }

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: How to find where the biggest number is in an array

    it will just sit there and not do anything, just freeze and think.
    Is it waiting for the user to enter more data? What happens when more data is typed in?
    What does it do if -999 is entered?

    Add some calls to the println statement to print out messages as the code executes to show you where it is executing.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Printing highest number of array. Can't find my error.
    By Praetorian in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 15th, 2013, 11:46 PM
  2. 3x3 array find largest number
    By joecan2010 in forum Loops & Control Statements
    Replies: 1
    Last Post: November 27th, 2012, 10:56 PM
  3. [SOLVED] how to find all occurrences of a number in an array recursivelly
    By mia_tech in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 13th, 2012, 01:37 PM
  4. Find Biggest Number in Array
    By jo15765 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: May 12th, 2012, 03:01 PM
  5. Replies: 4
    Last Post: June 10th, 2009, 01:04 AM

Tags for this Thread