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: Small help with my code :) (SOS)

  1. #1
    Junior Member
    Join Date
    Dec 2022
    Location
    My mom's womb.
    Posts
    4
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Small help with my code :) (SOS)

    So, I have homework until tomorrow, I've been sitting trying to find my problem for an hour and I couldn't find. No, it doesn't give me an error, but the compilator doesn't progress after a user enters a number.
    The assignment is to write a program which detects if a number is a positive one, a negative one or a zero. (We must use nested if statement)

    	package gg;
    import java.util.*;
    public class AAA {
    	public static void main(String[] args) {
    int a;
    Scanner kb = new Scanner (System.in);
         System.out.print("Please enter any number: ");
     a=kb.nextInt();
     
     if (a==0){
        	System.out.print("The number is a zero");
        if(a>0) {
        	System.out.print("The number is positive.");
        }
        else {
        	System.out.print("The number is negative.");
        }
      }
     
        kb.close();
    	}
     
    }
    I feel so stupid lol, I need a more difficult program in class, yet cannot do this simple one.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Small help with my code :) (SOS)

    Check that the enclosing {}s are correctly placed.

    Also fix the indentations of the code.
    See this: https://en.wikipedia.org/wiki/Indentation_style
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2022
    Location
    My mom's womb.
    Posts
    4
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Small help with my code :) (SOS)

    Quote Originally Posted by Norm View Post
    Check that the enclosing {}s are correctly placed.

    Also fix the indentations of the code.
    See this: https://en.wikipedia.org/wiki/Indentation_style
    I did check them, everything is correct there. I am trying to write a new code though, hopefully it'll work lol.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Small help with my code :) (SOS)

    I did check them, everything is correct there
    It looks to me like the test for a > 0 is only executed if a == 0. Logically that can never be true.
    There is a misplaced }
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    robolokidA (February 7th, 2023)

Similar Threads

  1. Small help with my code :)
    By robolokidA in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 25th, 2022, 01:29 PM
  2. Help..see the code and jst i need small help..
    By Simarpreet in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 23rd, 2014, 05:22 AM
  3. Small Problem with my Code
    By Cyril in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 20th, 2013, 03:02 AM
  4. Small problem in big code, not sure why
    By diesal11 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: October 16th, 2011, 10:25 AM
  5. WatchService - small code for monitoring folders
    By zincc in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 8th, 2011, 10:19 AM