-2

a busy cat http://maradastudios.ucoz.com/school/Capture3.png

For some reason, when inputting employee names the insertion point skips employee 1 and goes right to employee 2. However, it appears that employee 3 and beyond will input correctly. Here's my full code. Ignore the stupidity of it, I do things unconventionally.import

java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.util.*;
import java.text.*;
public class program1
{
    public static boolean isclearing=false;
    public static int[] num={0,0,0,0,0,0,0};
    public static String[] name={"","","","","","",""};
    public static String[] dept={"","","","","","",""};
    public static double[] rate={0,0,0,0,0,0,0};
    public static int[] hours={0,0,0,0,0,0,0};
    public static int[] code={0,0,0,0,0,0,0};
    public static Scanner in=new Scanner(System.in);
    public static NumberFormat money=NumberFormat.getCurrencyInstance();
    public static int inval=0;
    public static void main(String[] args)
    {
        while(inval!=7)
        {
            showmenu();
            inval=in.nextInt();
            switch(inval)
            {
                case 1:
                {
                    if(isclearing==false){
                    input();
                    break;}
                }
                case 2:
                {
                    employees();
                    break;
                }
                case 3:
                {
                    production();
                    break;
                }
                case 4:
                {
                    machineshop();
                    break;
                }
            }
        }
    }
    public static void showmenu()
    {
        System.out.println("\n1. Enter data");
        System.out.println("2. Show all employees");
        System.out.println("3. Show Production employees");
        System.out.println("4. Show Machine Shop employees");
        System.out.println("5. Gross pay report");
        System.out.println("6. Display by union (618/410)");
        System.out.print("7. Exit\nEnter your choice >> ");
        inval=in.nextInt();
        clearme();
        for(int crap=0;crap<99999;crap++){}
    }
    public static void input()
    {
        for(int i=0;i<7;i++)
        {
            System.out.print("Enter employee number "+(i+1)+": ");
            num[i]=in.nextInt();
        }
        for(int i=0;i<7;i++)
        {
            System.out.print("Enter employee name "+(i+1)+": ");
            name[i]=in.nextLine();
        }
        for(int i=0;i<7;i++)
        {
            System.out.print("Enter employee department "+(i+1)+": ");
            dept[i]=in.nextLine();
        }
        for(int i=0;i<7;i++)
        {
            System.out.print("Enter current rate "+(i+1)+": ");
            rate[i]=in.nextDouble();
        }
        for(int i=0;i<7;i++)
        {
            System.out.print("Enter current hours "+(i+1)+": ");
            hours[i]=in.nextInt();
        }
        for(int i=0;i<7;i++)
        {
            System.out.print("Enter union code "+(i+1)+": ");
            code[i]=in.nextInt();
        }
    }
    public static void employees()
    {
        System.out.println("Employee\tEmployee\tEmployee");
        System.out.println("Number  \tName    \tDepartment\n");
        for(int i=0;i<7;i++)
        {
            //System.out.println(num[i]+"\t"+name[i]+"\t"+dept[i]+"\t"+money.format(rate[i])+"\t"+hours[i]+"\t"+code[i]);
            System.out.println(num[i]+"\t"+name[i]+"\t"+dept[i]);
        }
    }
    public static void production()
    {
        System.out.println("Employee\tEmployee\tEmployee  ");
        System.out.println("Number  \tName    \tDepartment\n");
        for(int i=0;i<7;i++)
        {
            if(dept[i].equals("Production"))
            {
                //System.out.println(num[i]+"\t"+name[i]+"\t"+dept[i]+"\t"+money.format(rate[i])+"\t"+hours[i]+"\t"+code[i]);
                System.out.println(num[i]+"\t"+name[i]+"\t"+dept[i]);
            }
        }
    }
    public static void machineshop()
    {
        System.out.println("Employee\tEmployee\tEmployee  ");
        System.out.println("Number  \tName    \tDepartment\n");
        for(int i=0;i<7;i++)
        {
            if(dept[i].equals("Machine Shop"))
            {
                //System.out.println(num[i]+"\t"+name[i]+"\t"+dept[i]+"\t"+money.format(rate[i])+"\t"+hours[i]+"\t"+code[i]);
                System.out.println(num[i]+"\t"+name[i]+"\t"+dept[i]);
            }
        }
    }
    public static void clearme()
    {
        isclearing=true;
        try
        {
            Robot pressbot = new Robot();
            pressbot.keyPress(17); // Holds CTRL key.
            pressbot.keyPress(76); // Holds L key.
            pressbot.keyRelease(17); // Releases CTRL key.
            pressbot.keyRelease(76); // Releases L key.
            pressbot.keyPress(KeyEvent.VK_SPACE);
            pressbot.keyRelease(KeyEvent.VK_SPACE);
            pressbot.keyPress(KeyEvent.VK_ENTER);
            pressbot.keyRelease(KeyEvent.VK_ENTER);
        }
        catch (AWTException ex)
        {
            //Hi.
        }
        isclearing=false;
    }
}
TheEwook
  • 11,037
  • 6
  • 36
  • 55
Brandon Durst
  • 71
  • 1
  • 4
  • 12

1 Answers1

2

Scanner#nextInt() does not consume newline characters, instead passing through to the loop that reads employee names.

name[i] = in.nextLine();

The first line in the iteration now will not block as it has already received input from the previous block.

Solution: add in.nextLine() prior to the second loop to consume the newline character.

Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • i commented that out, the problem wasnt solved. the clearme method only runs after the input for the menu is entered. i dont see why it would affect the input method – Brandon Durst Apr 22 '13 at 16:58
  • yes but it just skips a line before entering the employee numbers, even after commenting out the enter key presses i have the same issue. it seems that when entering a string (according to my classmates) using in.nextLine() it will always skip the first input for some unknown reason. – Brandon Durst Apr 22 '13 at 17:04