0

when my program gets to the part where it asks for the name of the fruit, it will output the string asking for the name, then immediately go to the next string output without waiting for the user input. This seems to automatically assign a value of null to my name variable.

Fruit.java

public class Fruit {

    String Name;
    int Quantity;
    double Mass;

    public Fruit(String Name, int Quantity, double Mass) {
        this.Name = Name;
        this.Quantity = Quantity;
        this.Mass = Mass;
    }

    public void Information() {
        System.out.println("This fruit is an " + Name + ", there's " + Quantity
                + " of it and it weighs " + Mass + " grams");
    }
}

Fruits.java

import java.util.Scanner;

public class Fruits {

    public static void main(String[] args) {

        Fruit menu[];
        int number;
        String name;
        int quantity;
        double mass;

        System.out
                .print("How many fruits would you like to add to the menu?: ");

        Scanner input = new Scanner(System.in);

        number = input.nextInt();
        input.nextLine();
        menu = new Fruit[number];

        for (int i = 0; i < menu.length; i++) {

            System.out.println("What would you like to name the fruit?: ");
            name = input.nextLine();

            System.out.println("How much fruits are there?: ");
            quantity = input.nextInt();

            System.out.println("What is the mass of the Fruit in grams?: ");
            mass = input.nextDouble();

            menu[i] = new Fruit(name, quantity, mass);

            menu[i].Information();
        }
        input.close();
    }
}
Jagger
  • 9,590
  • 7
  • 42
  • 78
als26
  • 91
  • 9
  • 7
    possible duplicate of [Skipping nextLine() after use nextInt()](http://stackoverflow.com/questions/13102045/skipping-nextline-after-use-nextint) – RealSkeptic Jan 04 '15 at 18:39
  • Position in arrays in Java are numbered from 0. I corrected your code. – Jagger Jan 04 '15 at 18:42
  • Ah yes thanks for that @Jagger. I thought it didn't matter because I had i<=, but I realized I was using i as the numbers for my Array! Thanks – als26 Jan 04 '15 at 18:50

3 Answers3

2

instead of input.nextInt(); use Integer.parseInt(input.nextLine()). it might solve your issue.

Prashant
  • 2,480
  • 2
  • 17
  • 26
1

the problem is scanning for ints, then nextLine. When you run .nexInt() I believe there is a newline character not scanned in, so this immediately messes with the following .nextLine(), as it only takes in the newline and nothing after The easiest fix I am aware of is

number = input.nextInt();
input.nextLine();
menu = new Fruit[number];

And then the rest of the code should work

As an aside, usually you would start the loop from 0, because arrays start from 0, and you will have a blank first entry, but I don't think it matters in this particular piece of code

spyr03
  • 745
  • 1
  • 6
  • 25
1

When you use input.nextInt() there is a %n char in hub. You need to use a input.nextLine() after to remove the line-break charcater. You can also use input.nextLine() for each variables and then parse it yourself.

Warning! In java convention the method name, attribute name and parameter name must begin by an lower case character.

Nemolovich
  • 353
  • 2
  • 10