-1

I am a beginner programmer and i am trying a program for my father.

import java.util.*;
import java.lang.*;
import java.io.*;
class Employee
{

    String m1,m2,m3,m4,m5,m6,m7;
    void main()
    {
         Scanner w=new Scanner(System.in);
         Scanner n=new Scanner(System.in);
         System.out.println("Please enter your name ");
         String name=w.nextLine();
         System.out.println("Please choose your client");
         System.out.println("1 - XXXXXX");
         int client=n.nextInt();
         m1=name;//Storing name
         if(client==1)//If statement storing client
        {  
              m2="XXXXXX";
   }
    else
    {
        System.out.println("You have entered a wrong choice");
        return;
    }

    String msg=m1+"\t"+m2;
    System.out.println(msg);
}

}

This Code will give the output "as you have entered a wrong choice'" It jumps to elsse statement. What is the error and is there an easier way to run this program. Thanks Could yo please inform me on my error as

  • 3
    Why are you using 2 `Scanner`s? Also if your `main` method should be the entry point for the program, it's signature is wrong. – fabian Nov 22 '14 at 09:25
  • 1
    You need to look at java docs for: a) formatting/indentation, b) how to write and qualify a `main()` method, c) how to take input with the `Scanner` class (you don't need two). – u3l Nov 22 '14 at 09:26
  • I apologise about my indentation. I was 14 years old when I posted this haha. My entire coding structure has improved considerably – Mr.Awesome Nov 15 '20 at 11:00

4 Answers4

1

Ok try this code:

    import java.util.Scanner;

public class Try
{

    static String m1,m2,m3,m4,m5,m6,m7;
    public static void main(String[] args)
    {
         Scanner w=new Scanner(System.in);

         System.out.println("Please enter your name ");
         String name=w.nextLine();
         System.out.println("Please choose your client");
         System.out.println("1 - XXXXXX");
         int client=w.nextInt();
         m1=name;//Storing name
         if(client==1)//If statement storing client
        {  
              m2="XXXXXX";
   }
    else
    {
        System.out.println("You have entered a wrong choice");
        return;
    }

    String msg=m1+"\t"+m2;
    System.out.println(msg);
}

}
Sarthak Mittal
  • 5,794
  • 2
  • 24
  • 44
0

You have missed you main method signature. In Java there is a specification of main method. Your main method should be like

public static void main(String []args){
}

In your case you main method should be

public static void main(String args[]) {
    String m1, m2, m3, m4, m5, m6, m7;
    Scanner w = new Scanner(System.in);
    Scanner n = new Scanner(System.in);
    System.out.println("Please enter your name ");
    String name = w.nextLine();
    System.out.println("Please choose your client");
    System.out.println("1 - XXXXXX");
    int client = n.nextInt();
    m1 = name;//Storing name
    if (client == 1)//If statement storing client
    {
        m2 = "XXXXXX";
    } else {
        System.out.println("You have entered a wrong choice");
        return;
    }

    String msg = m1 + "\t" + m2;
    System.out.println(msg);
}
S. M. Mohiuddin
  • 378
  • 3
  • 10
0

Your problem are the 2 scanners. Because a scanner work with an iterator, that keep the position inside the given inputstream (in this case), when you instantiate the 2 scanners, they both set their iterator at the same position into the stream, then you use "w.nextLine();", and the first scanner advances trough the stream returning the first line, as you wish, but the second scanner, that you haven't used, is still at the beginning of the stream, so basically when you use n.nextInt();, the scanner tries to parse your name as int, and it's strange that it doesn't throws an InputMismatchException, as it should do ("https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#nextInt%28%29"). Rework your code as @Sarthak Mittal suggested and it should work.

PS: keep in mind indentation, it's important, really

salvatore
  • 511
  • 4
  • 11
0

First:

void main()

There is no such thing in Java. It should be,

public static void main(String[] args)

To know the meanings of public, static, String[] args read this: Explanation of 'String args[]' and static in 'public static void main(String[] args)'

Secondly,

int client = n.nextInt();

The value inside client depends on your input. If you input 2 or 3 instead of 1, your code'll definitely go to the else part. So make sure your input is 1.

Thirdly,

Get rid of the extra scanner. You need only one.

The rest of your code is ok.

Community
  • 1
  • 1