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

Thread: Finding the lowest number in an array.

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    22
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Finding the lowest number in an array.

    Shouldn't minstetall be the lowest number here? It only prints 0.

    import java.util.*;
    public class Kap3Oppg5 {
    	public static void main(String[] args) {
    		int x = 0;
    		int tabell[] = new int[10];
    		int heltall = 0;
    		Scanner in = new Scanner(System.in);
    		int minstetall = tabell[0];
    		int indeks = 0;
     
    		while (x < 10) {
    			System.out.println("Tast inn neste tall:");
    			heltall = in.nextInt();
    			tabell[x] = heltall;
    			x = x + 1;
    		}
     
    		for (int i=0;i<tabell.length;i++) {
    			if (tabell[i] < tabell[0])
    				minstetall = tabell[i];
     
    		}
     
    		System.out.print(minstetall);
    	}
     
    }

    EDIT: I am happy to report that I solved the problem myself. If anyone should have problems with anything that looks the same, here is the code: (Note that the full task was to find the lowest integer in the array, report which index it had, and swap said number with tabell[0].)

    import java.util.*;
    public class Kap3Oppg5 {
    	public static void main(String[] args) {
    		int x = 0;
    		int tabell[] = new int[10];
    		int heltall;
    		Scanner in = new Scanner(System.in);
    		int minstetall = tabell[0]; //
    		int indeks = 0;
     
    		while (x < 10) {
    			System.out.println("Tast inn neste tall:");
    			heltall = in.nextInt();
    			tabell[x] = heltall; //Definerer heltallet som tabell[x]
    			x = x + 1; //Hvor x begynner på 0 og øker til 9.
    		}
     
    		for (int i=0;i<tabell.length;i++) {
    			if (tabell[i] < tabell[indeks])
    				indeks = i;
     
    		}
    		minstetall = tabell[indeks];
    		System.out.println("Det minste tallet er " +minstetall);
    		System.out.println("Dette tallet har indeksen " +indeks);
    		int midlertidig = tabell[indeks];
    		tabell[indeks] = tabell[0];
    		tabell[0] = midlertidig;
    		System.out.println("Den nye rekkefølgen på tallene er: ");
    		System.out.println(tabell[0]);
    		System.out.println(tabell[1]);
    		System.out.println(tabell[2]);
    		System.out.println(tabell[3]);
    		System.out.println(tabell[4]);
    		System.out.println(tabell[5]);
    		System.out.println(tabell[6]);
    		System.out.println(tabell[7]);
    		System.out.println(tabell[8]);
    		System.out.println(tabell[9]);
    	}
     
    }

  2. The Following User Says Thank You to EatMyBible For This Useful Post:

    vividMario52 (February 4th, 2013)


Similar Threads

  1. [SOLVED] flightpaths, finding lowest cost -- lowest amount of crossovers
    By CjStaal in forum Algorithms & Recursion
    Replies: 4
    Last Post: May 8th, 2012, 12:47 AM
  2. Replies: 1
    Last Post: December 4th, 2010, 04:26 PM
  3. Loop to note position of lowest number
    By fortune2k in forum Loops & Control Statements
    Replies: 3
    Last Post: November 23rd, 2010, 09:05 AM
  4. Finding the highest number in an array?
    By halfwaygone in forum Algorithms & Recursion
    Replies: 1
    Last Post: April 17th, 2010, 03:56 PM
  5. Replies: 4
    Last Post: June 10th, 2009, 01:04 AM