Results 1 to 3 of 3
  1. #1
    hyunski is offline Member
    Join Date
    Mar 2009
    Posts
    2
    Rep Power
    0

    Default Scanner-While Loop

    Hello,

    I am having a problem with this bit of code. I am trying to use a while(true) loop to display a "menu" and when the user chooses an option, more options pop up. For example, if I chose option 3 below, then it would ask me for the name of the company (ex. YAHOO) then ask me how many shares I would like to buy.

    For some reason after it asks me the name of the company, it skips asking me how many shares I would like to buy and when I enter "5" to exit, it does so then.

    import java.util.*;
    import java.io.*;
    import java.net.*;


    public class BSoftware{

    Portfolio portfolio;

    public BSoftware() throws IOException, FileNotFoundException{

    Scanner sc = new Scanner(System.in);

    while(true){
    System.out.println("BSoftware v1.0");
    System.out.println("Options:");
    System.out.println("1. View Current Portfolio");
    System.out.println("2. Make New Portfolio");
    System.out.println("3. Invest");
    System.out.println("4. Research");
    System.out.println("5. Save & Exit");

    String choice = sc.next();

    //NON-RELEVANT CODE THAT CONTAINS BUNCH OF OTHER OPTIONS

    if(choice.equals("3")){
    System.out.println("Input Symbol: ");
    Stock temp = new Stock(sc.next());//asks user for which stock to invest in

    //EVERYTHING IS FINE UNTIL HERE AND BELOW DOES NOT HAPPEN

    printQuote(temp);//shows relevant info on stock
    System.out.println("Amount: ");
    Boolean result = portfolio.add(temp, Integer.parseInt(sc.next()));
    if(result){ System.out.println("Funds left: " + portfolio.getFunds()); }
    }
    if(choice.equals("4")){}//EDIT LATER
    if(choice.equals("5")){ System.exit(1); }//EDIT LATER
    else{ System.out.println("\nPlease choose options 1 - 4\n"); }
    }
    }

    public static void main(String args[]) throws IOException{
    BSoftware start = new BSoftware();
    }

    public void loadPortfolio(String file) throws IOException{
    Scanner sc = new Scanner(new File(file));

    while(sc.hasNext()){
    Stock temp = new Stock(sc.next());//first word is always quote
    portfolio.add(temp, Integer.parseInt(sc.next()));//next word is amount
    }

    }

    public void savePortfolio(String file) throws IOException{
    PrintWriter out = new PrintWriter(new FileWriter("recentPortfolio.txt"));
    Set keys = portfolio.getPort().keySet();
    Iterator iter = keys.iterator();
    while(iter.hasNext()){
    String temp = (String)iter.next();
    out.print(temp + " " + portfolio.getPort().get(temp) + " ");
    }
    out.close();
    }

    public void printQuote(Stock stock) throws IOException{//print the relevant necessary of all the given Symbol
    System.out.println(stock.getName());
    System.out.println("Price: " + stock.getPrice());
    System.out.println("52Wk Range: " + stock.getRange());
    System.out.println("Change: " + stock.getChange());
    System.out.println("Volume: " + stock.getVolume());
    System.out.println("Avg. Volume: " + stock.getAvgVolume());
    System.out.println("Market Cap: " + stock.getMarketCap());
    System.out.println("Dividend: " + stock.getDiv());
    System.out.println("P/E Ratio: " + stock.getPE());
    System.out.println("EPS: " + stock.getEPS());
    System.out.println("EBITDA: " + stock.getEBITDA());
    }

    public Float getRf(){ return (float)0; }//EDIT LATER

    public InputStream urlStream(String u) throws IOException{//stream URL to use for Scanners and such
    URL url = new URL(u);
    return url.openStream();
    }

    public ArrayList getCSV(String u) throws IOException, MalformedURLException{//process .csv files
    ArrayList<Object> data = new ArrayList<Object>();
    Scanner sc;
    try{ sc = new Scanner(urlStream(u)).useDelimiter(","); }//if .csv file is online
    catch(MalformedURLException e){ sc = new Scanner(u).useDelimiter(","); }//if .csv file is local (offline)

    while(sc.hasNext()){
    String temp = sc.next();
    data.add(temp);
    }

    return data;
    }

    }

    Thanks in advance!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Try using the Scanner#nextLine() method rather than the Scanner#next() method.

    As a side note:
    I can't compile your code as it contains references to classes that we don't have available such as Portfolio and Stock. When posting code I suggest that you simplify it so that it is compilable and have require outside dependencies. Also, when posting code here, please use code tags so that your code will retain its formatting and thus will be readable -- after all, your goal is to get as many people to read your post and understand your code as possible, right?

    To do this, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.

    Another way to do this is to manually place the tags into your code by placing the tag [cod&#101;] above your pasted code and the tag [/cod&#101;] below your pasted code like so:

    Java Code:
    [cod&#101;]
      // your code goes here
      // notice how the top and bottom tags are different
    [/cod&#101;]
    Oh, and welcome to the forum!

  3. #3
    hyunski is offline Member
    Join Date
    Mar 2009
    Posts
    2
    Rep Power
    0

    Default

    Thanks for the reply! I will keep it in mind when I do future posts.

    I figured out the problem and it actually wasn't with Scanner, but recursion. Everytime I ran the program, it would call BSoftware which would call Stock which in its constructor also called BSoftware. I made a new class to separate the calls and end the never-ending calls on the two classes towards each other.

    Everything works now. Thanks again.

Similar Threads

  1. how to use Scanner with a number
    By cew27 in forum New To Java
    Replies: 10
    Last Post: 04-03-2009, 06:23 PM
  2. Scanner
    By choko in forum New To Java
    Replies: 10
    Last Post: 01-24-2009, 03:37 PM
  3. Scanner Class...
    By TheRocket in forum New To Java
    Replies: 5
    Last Post: 12-05-2008, 09:48 AM
  4. need help with scanner
    By whiterex in forum New To Java
    Replies: 1
    Last Post: 04-22-2008, 01:41 PM
  5. help with IP scanner
    By tommy in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 08:00 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •