-1

I'm very new to java and taking a course on it in high school, we are just starting out with simple programs to get familiar with the syntax, but for some reason I am unable to use Scanner to input strings into string variables. The line of code just gets ignored. I've tried directly copy/pasting my teacher's notes he gives us, and they run fine in his own programs even when run on my computer, but they don't work in my programs. What am I doing wrong?

import java.util.Scanner;
public class Control5
{
    public static void main (String [] args)
    {
        Scanner input = new Scanner(System.in);
        String job;
        System.out.println("1");
        int years;
        System.out.print("How many years have you been employed here? ");
        years = input.nextInt();
        System.out.print("Enter your job title: ");
        job = input.nextLine();
        if ((years > 5) & (job.toLowerCase() == "salesman")) {
            System.out.println("Eligible for promotion.");
        }
        else {
            System.out.println("Not eligible for promotion.");
        }
    }
}
Ryan M
  • 1

3 Answers3

0

check them 1 name of file must be name of your class ( now must be Control5.java )

2 public static void main ( String [] args) is incorrect and correct is public static void main ( String[] args) means the [] must placed into a datatype that means an array ( sum of multi var ) that all variables are String type ( search array in java )

3 if((years > 5)&(job.toLowerCase() == "salesman"))

& is bitwise AND operator and compare AND operator is && ( but both works )

PooiaFerdowsi
  • 71
  • 1
  • 9
  • ``public static void main (String [] args)`` is perfectly correct. I wouldn't have put the extra space between the String and the [], but that doesn't matter for Java syntax. Did you try compiling this before telling everybody that it was wrong? – NomadMaker Feb 10 '21 at 06:30
0

The below example should solve your problem. ALWAYS close your input and output streams example: input.close(). Think about using equals() or compareTo() for string comparison.

public static void main (String[] args) {
         
         // Input stream
         Scanner input = new Scanner(System.in);
         
         // Request User Input and assign corresponding variables the entered value(s)
         System.out.print("How many years have you been employed here? ");
         int years = input.nextInt();
         System.out.print("Enter your job title: ");
         String job = input.next();
         
         // Determine if user is eligible for promotion 
         if (years > 5 && job.toLowerCase().equals("salesman")) {
            System.out.println("Eligible for promotion.");
         }
         else { System.out.println("Not eligible for promotion."); }
         
         // Close input
         input.close();
}
  • I suggest you show try-with-resources syntax to automatically close the resources. – Basil Bourque Feb 10 '21 at 07:51
  • I was keeping it simple for the young man. He may not have learned that yet. I assume since he is using a scanner he has at least been taught how to close the scanner in the most basic way. – Adamthecreator369 Feb 10 '21 at 16:27
0

Sorry my English is bad. Method nextInt receives only int values, it doesn't read or skip anything after the Integer. Let, s say you enter in console "123/n",method nextInt retrieves 123 and leaves it in buffer /n, thereafter method nextLine retrieves /n. Because of this, the condition is not met.

We can use method next instead nextLine or Use method nextLine before the method nextInt or years = Integer.parseInt(input.nextLine());