How to get a single StockQuote using yahoofinance API?

Question:
How to get a StockQuote using yahoofinance API?

Answer:
Here is a simple example on how to get a single quote using the Java Finance Quotes API for Yahoo.Finance

Code:
import java.io.IOException;
import yahoofinance.Stock;
import yahoofinance.YahooFinance;
import yahoofinance.quotes.stock.StockQuote;
 
public class YahooStockQuoteAPI {
 
  public static void main(String[] args) throws IOException {
    YahooFinance yahoo = new YahooFinance();
    Stock stock = yahoo.get("GOOG");
    StockQuote sq = stock.getQuote();
 
    System.out.println("Symbol: " + sq.getSymbol());
    System.out.println("Price: " + sq.getPrice());
    System.out.println("Date: " + sq.getLastTradeTime().getTime());
  }
}

Output:
$ java YahooStockQuoteAPI 
Symbol: GOOG
Price: 662.20
Date: Fri Oct 16 16:00:00 EDT 2015

Converting BigDecimal to an int

Question:
How to Convert a BigDecimal to an int?
Converting BigDecimal to Integer

Answer:
bigdecimal.intValue();

Code:
import java.math.*;
 
public class BigDecimalTest {
  public static void main(String[] args) {
 
    BigDecimal big = new BigDecimal(123.456);
    int x = big.intValue();
    System.out.println(x);
  }
}
 

Output:
$ java BigDecimalTest
123

How to multiply a BigDecimal by an integer in Java?

Question:
How to multiply a BigDecimal by an int in Java?
Multiplication with BigDecimals.

Answer:
bigdecimal.multiply(new BigDecimal(int))

Code:
import java.math.*;
 
public class BigDecimalTest {
  public static void main(String[] args) {
 
    BigDecimal bd = new BigDecimal(2.0);
    int x = 5;
    bd = bd.multiply(new BigDecimal(x));
    System.out.println(bd);
  }
}
 

Output:
$ java BigDecimalTest
10

How to test if a BigDecimal is less then zero?

Question:
How to test if a BigDecimal is less then zero?
How to compare BigDecimal to an int?

Answer:
number.compareTo(BigDecimal.ZERO)

Code:
import java.math.*;
 
public class BigDecimalTest {
  public static void main(String[] args) {
    BigDecimal bd = new BigDecimal(-1.5);
 
    if (bd.compareTo(BigDecimal.ZERO) < 0) {
      System.out.println(bd + " is less then zero");
    }
  }
}
 

Output:
$ java BigDecimalTest
-1.5 is less then zero

How to write a java program that will ask for the length and width of a rectangle and then multiply them together and create a rectangle out of astrisks

Question:
How to write a java program that will ask for the length and width of a rectangle and then multiply them together and create a rectangle out of astrisks?

Code:
import java.util.Scanner;
 
public class Rectangle {
 
  public static void main(String[] args) {
 
    Scanner sc = new Scanner(System.in);
 
    System.out.print("Enter height: ");
    int h = sc.nextInt();
 
    System.out.print("Enter width: ");
    int w = sc.nextInt();
 
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
        System.out.print("*");
      }
      System.out.println();
    }
  }
}

Output:
$ java Rectangle
Enter height: 5
Enter width: 20
********************
********************
********************
********************
********************

Find closest value to 0 for Float in Java?

Question:
Find closest value to 0 for Float in Java?

Answer:
Float.MIN_VALUE

Code:
public class MyFloat {
  public static void main(String[ ] args) {
    Float f = new Float(Float.MIN_VALUE);
    System.out.format("%.50f%n", f);
  }
}
 

Output:
$ java MyFloat
0.00000000000000000000000000000000000000000000140130

Write a java program that reads 10 numbers from the user, but does not allow the user to enter duplicates.

Question:
Write a java program that reads 10 numbers from the user, but does not allow the user to enter duplicates.

Code:
import java.util.*;
 
public class TenNumbers {
 
  public static void main(String[] args) {
 
    Scanner sc = new Scanner(System.in);
 
    Set set = new TreeSet();
    do {
      System.out.print("Enter number: ");
      try {
        int i = sc.nextInt();
        if (set.contains(i)){
          System.out.println("Duplicate.");
        } else {
          set.add(i);
        }
      } catch (InputMismatchException e) {
         System.out.println("Not a number.");
         sc.nextLine();
      }
 
    } while (set.size()<10);
 
    System.out.println("set: " + set);
  }
}

Output:
$ java TenNumbers
Enter number: 1
Enter number: 2
Enter number: 3
Enter number: 4
Enter number: 5
Enter number: 6
Enter number: 7
Enter number: 8
Enter number: 9
Enter number: ten
Not a number.
Enter number: 9
Duplicate.
Enter number: 10
set: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]