Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Logical Flaw HELP!

  1. #1
    Junior Member
    Join Date
    Mar 2019
    Location
    India
    Posts
    18
    My Mood
    Confused
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Logical Flaw HELP!

    So I have a string Streams in a class
    and a method which requires user to enter either 1,2,3
    if they enter 1 Streams=Science, if they enter 2 Streams=2, If they enter 3 Stream= what ever the user inputs. The logic is fairly simple but I can't get it to work
    here is the code demo
    Scanner input = new Scanner(System.in);
    String Streams;
    public void InputStream() {
    	System.out.println("Enter the Stream.");
    	System.out.println("1 for Science, 2 for Arts, 3 for others");
    	int answer=input.nextInt();
    	if (answer==1) {
    	 Streams="Science";
    	}
    if(answer==2) {
    	 Streams="Arts";
    }
    else {
    	System.out.println("enter the subject");
    	input.reset();
    	Streams=input.nextLine();
    }
    }
    //i am gonna call the constructor "method"
    public method(){
    InputStream();
    System.out.println(Streams);
    also
    in output console the text from the 3rd if statement"enter the subject" appears every single time even when the user enters 1 or 2.
    A quick reply will be really appreciated
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Logical Flaw HELP!

    Can you copy the contents of the console from when you execute the program and paste it here to show the problem.
    Add some comments to describe the problem and show what the program should have done.
    For example
    1
    4 <<< This should be 2
    3
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Jul 2019
    Posts
    36
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default Re: Logical Flaw HELP!

      String Streams="";  //=""
     
      public void InputStream() {
       ...
     
        if (answer==1) {
            Streams="Science";
        } else if(answer==2) {  //else
            Streams="Arts";
        } else {
          System.out.println("enter the subject");
          //input.reset();
          while(Streams.isBlank() ) {  //while
             Streams=input.nextLine();
          }
        }
    Last edited by zemiak; July 13th, 2019 at 11:13 AM.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Logical Flaw HELP!

    @zemiak Please read: http://www.javaprogrammingforums.com...n-feeding.html
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. logical errors
    By return0 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 15th, 2013, 06:21 PM
  2. Logical
    By snarayana.murthy86 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: July 16th, 2013, 01:08 AM
  3. Logical Problem
    By dougie1809 in forum AWT / Java Swing
    Replies: 18
    Last Post: February 21st, 2013, 02:17 PM
  4. Help with logical operators
    By BiaxialPainter in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 11th, 2012, 12:35 AM
  5. Flaw in my code
    By miss confused in forum What's Wrong With My Code?
    Replies: 14
    Last Post: July 7th, 2010, 04:02 AM