Reading string and numbers with decimal from text file

Hello fellow, programmers,

The code must read strings and numbers with decimals and display them. Please see image.
I am having a small problem with my if statement on case 1. For some reason, it displays the right stock ticker (acronym) but the incorrect price. Please refer to image, can any of you suggest how to fix it. Please refer to the text file image to see what I am referring to.

Any help is greatly appreciated,

down_805

`
import java.io.*;
import java.util.*;

public class SQRT
{

public static void main(String[] Giovanni)
throws FileNotFoundException
{
    // declare and initialize the variables
    String stockTicker;




    // Will read the Stock Tickers and the stock's price on file and display
    Scanner inFile = new Scanner(new FileReader("file.txt"));
    // Scanner console: Will interpret user input
    Scanner console = new Scanner(System.in);


    // MENU
    System.out.println("Enter '1' to get price by stock ticker \n" );
    System.out.println("Enter '2' to get stocks greater than or equal to specidied price \n" );
    System.out.println("Enter '3' to quit \n" );
    System.out.println("Your choice : " );
    int userChoice = console.nextInt();  // once the user inputs either 1, 2, or 3, the program will then take
    // this option and take it into the switch statement
    switch ( userChoice )
    {

        case 1:
            System.out.println("Your choice : " + userChoice );
            System.out.println("Enter a stock Ticker : " );
            stockTicker = console.next();
            stockTicker = stockTicker.toUpperCase();


            if ( stockTicker != inFile.next()   )
            {
                System.out.println(stockTicker + " price is " + inFile.nextDouble() + "\n ");

            }
            else
            {
                System.out.println(inFile.nextLine()  + " price is " + inFile.nextLine() );
            }




        case 2:
        case 3:
            System.out.println("\t\t\t\t    Goodbye!");
        default:
            System.out.println("\t\t\t The program is now ending, arrivederci!");
    }
}

}

`

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion