0

I have a rudimentary piece of code that's meant to update a properties file. However, it seems that of the two possible keywords to update, only the second is updated by the user's input, as opposed to one after the other.

Here is the full code:

    import java.io.*;
import java.util.Properties;
import java.util.Scanner;
public class UpdateProperty{
private static int choice;
 static  Scanner sc = new Scanner(System.in);
    public static void main(String args[]) throws Exception 
    {   
  FileInputStream in = new FileInputStream("Stats.properties");
  Properties props = new Properties(); //creates a Properties object named prop
  props.load(in); //loads in as value of prop
  in.close(); //no idea
  
  System.out.println("1- BlackBerryIzzie: " + props.getProperty("BlackBerryIzzie")); 
  System.out.println("2- GrapeFruitIzzie: " + props.getProperty("GrapeFruitIzzie"));
  System.out.println("");
  String blackAmount = props.getProperty("BlackBerryIzzie");
  String grapeAmount = props.getProperty("GrapeFruitIzzie");
  
  
  //System.out.println("Selling BlackBerry Izzie");
  //blackAmount = itemSold(blackAmount);
  System.out.println("Do you wish to update inventory? Type 2");
  choice = sc.nextInt();
  
  if (choice == 2){
  FileOutputStream out = new FileOutputStream("Stats.properties");
  
  
    System.out.println("Insert BlackBerry Amount");
    blackAmount = sc.nextLine();
    props.setProperty("BlackBerryIzzie", blackAmount);   
  
    System.out.println("Insert GrapeFruit Amount");
    grapeAmount = sc.nextLine();
    props.setProperty("GrapeFruitIzzie", grapeAmount);
 
 
 
  props.store(out, null);
  out.close();  
    }
    }
    
    public static String itemSold(String s){
    int i=Integer.parseInt(s);
    i -= 1;
    String ret=Integer.toString(i);
    return ret;
    }
}

The bit that seems to be malfunctioning:

if (choice == 2){
  FileOutputStream out = new FileOutputStream("Stats.properties");

    System.out.println("Insert BlackBerry Amount");
    blackAmount = sc.nextLine();
    props.setProperty("BlackBerryIzzie", blackAmount);
 
    System.out.println("Insert GrapeFruit Amount");
    grapeAmount = sc.nextLine();
    props.setProperty("GrapeFruitIzzie", grapeAmount);

  props.store(out, null);
  out.close();  
    }

This is meant to ask the user for blackberry amount, then update the BlackBerryIzzie keyword to that amount. Then, it is meant to do the same for grapefruit after blackberry is done. However, it skips blackberry and only asks for one scanner input and sets grapefruit to that. Thanks for your time!

1 Answers1

0

Don't mix nextLine and nextAnythingElse.

The solution is to set your scanner's delimiter to what you want. You want 'user presses enter' to be the delimiter, surely. So, tell scanner that. Run scanner.useDelimiter("\\R") immediately after making it. Then, to get 'an entire line', call .next(), if you want that line to be read as e.g. an int, call .nextInt(), etc. Don't call nextLine() for anything.

Explaining why mixing nextLine and nextAnythingElse is bad is a bit of a story - this SO answer explains part of it. Unfortunately the 1000-vote accepted answer is not the right solution (.useDelimiter("\\R") and then .next() to read a line is the right solution).

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72