Java NumberTable

By | 13/01/2015

Program asks you to enter 10 numbers and lists them from biggest to smallest. After the list is shown, the program asks if you would like to check for a specific number from the list.

import java.util.Scanner;

public class NumberTable {
 Scanner numberReader=new Scanner(System.in);
 Scanner textReader=new Scanner(System.in);
 double[] numbers=new double[10];
	
 private void run(){
  getNumbers();
  arrangeNumbers();
  printNumbers();
  checkNumber();
 }
 private void getNumbers(){
  System.out.println("Enter 10 numbers");

  for(int i=0;i<10;i++){
   numbers[i]=numberReader.nextDouble();
  }
 }
 private void arrangeNumbers(){
  int tableLen = numbers.length-1;

  for (int k=0; knumbers[i+1]){
     numbers[i]+=numbers[i+1];
     numbers[i+1]=numbers[i]-numbers[i+1];
     numbers[i]-=numbers[i+1];
    }
   }
  }	 
 }
 private void printNumbers(){
  System.out.println();
  for (int i=numbers.length-1; i>=0; i--){
   System.out.println((i+1)+": "+numbers[i]);
  }
 }
 private void checkNumber(){
  String yn;
  do {
   System.out.println();
   System.out.print("Would you like to check the existance of a specific number? (y/n) ");
   yn=textReader.nextLine();
   while(!(yn.equalsIgnoreCase("y"))&&!(yn.equalsIgnoreCase("n"))){
    System.out.print("Please try again (y/n:) ");
    yn=textReader.nextLine();
   }
   if(yn.equalsIgnoreCase("y")) {
    System.out.print("Which number would you like to check? ");
    double input=numberReader.nextDouble();
    boolean foundIt=false;
				
    for(int i=0;i<10;i++) {
     if(numbers[i]==input) {
      System.out.println(input+" found");
      foundIt=true;
      break;
     }
    }
    if(!foundIt){
     System.out.println(input+" wasn't found");
    }
   }
   else{
    System.out.println("Bye!");
   }
  }while(yn.equalsIgnoreCase("y"));
 }
 public static void main(String[] args) {
  NumberTable program = new NumberTable();
  program.run();
 }
}

Leave a Reply

Your email address will not be published. Required fields are marked *