1

I was wondering if someone could help me fix small issue with the code I that I cant seem to figure it out or point me in the right direction. Every iteration after the first one at the begining the console skips a line before description input which later on does not allow me to search for an item and automatically gives me error item not found. I'm assuming it might have something to do with the buffer? Please excuse my language, english is not my first language.

import java.util.*;
public class inventory 
{

    //private int inventorySize;
    Scanner sc=new Scanner(System.in);
    String s1,s2;
    int ch,num;
    String[] list;
    String[] list1;
    public inventory()
    {

        System.out.print("How many items do you have?: ");
         num=sc.nextInt();
        list = new String[num];
         list1= new String[num];
        inventory();
    }

    public void inventory()
    {
        for(int i=0;i<list.length;i++)
        {
            System.out.print("Item name: ");
            sc.nextLine();
            list[i]=sc.nextLine();

            System.out.print("items description: ");

            list1[i]=sc.nextLine();
            //sc.nextLine();

        }

    }


    public void start()
    { 
        int ch;
        System.out.println("1.Seach for an item by name or: ");
        System.out.println("2.Display whole inventory or : ");
        System.out.println("3.Quit: ");
        ch=sc.nextInt();
        switch(ch)
        {
        case 1:
                System.out.println("Enter item name: "); 

                s1=sc.nextLine();
                searchAndDisplay(s1);
                break;

        case 2: displayWholeInventory();
                break;

        case 3: System.exit(0);



        }
    }

    public void searchAndDisplay(String name)
    { 
        for(int i=0;i<list.length;i++)
            if(list[i].equals(name))
            {
        System.out.println("Item name          Item Description:");
           System.out.println(" "+list[i]+"              "+list1[i]);
                  start();
            }


            else
            {
                System.out.println("You enterd incorrct name: ");
                start();
            }
    }


    public void displayWholeInventory()
    { 
           System.out.println("Item name         Item Description:");
      for(int i=0;i<list.length;i++)
      {
          System.out.println(""+list[i]+"              "+list1[i]);

      }
      start();
    }

    public static void main(String[] args) 
    {
        inventory store = new inventory();
       store.start(); 
    }
    } 
uszy123345
  • 43
  • 8
  • Thank you! I shall figure out the rest now! it's always something small that's being overlooked :) – uszy123345 Dec 05 '16 at 02:10
  • 1
    In inventory() method.System.out.print("Item name: "); sc.nextLine();--- this is extra which is causing problem – Akash Dec 05 '16 at 03:42

0 Answers0