Results 1 to 16 of 16
  1. #1
    andnlou2678 is offline Member
    Join Date
    Sep 2011
    Posts
    69
    Rep Power
    0

    Default java program check please

    task
    Write a program to input an integer and output whether it is positive, negative or zero. Fully test your program.

    my code

    import java.util.Scanner;
    public class positive {
    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter a number..");
    int reader = sc.nextInt();
    int a;
    for(a=-5; a <= 5; a++) {
    if (a >0)
    System.out.println("The number "+a+" is positive");
    if (a <0)
    System.out.println("The number "+a+" is negative");
    if (a==0)
    System.out.println("The number "+a+" is neutral");

    results i get

    Enter a number..
    20
    The number -5 is negative
    The number -4 is negative
    The number -3 is negative
    The number -2 is negative
    The number -1 is negative
    The number 0 is neutral
    The number 1 is positive
    The number 2 is positive
    The number 3 is positive
    The number 4 is positive
    The number 5 is positive

    i get this on every number i input
    where have i gone wrong please

    many thanks

    p.s how would i test this when complete.

  2. #2
    andnlou2678 is offline Member
    Join Date
    Sep 2011
    Posts
    69
    Rep Power
    0

    Default Re: java program check please

    i think i found the problem guys

    i was using A variable in my loop and control statement, i thing this was where it is making problem because A will start from -5 to +5 and all conditions will come true

    so i used this code instead i think this will work
    import java.util.Scanner;
    public class positive {
    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int a = 0;
    while( a != -999 ) {
    System.out.print("Enter a number (-999 to quit): ");
    a = sc.nextInt();
    if( a >0 ) System.out.println("The number "+a+" is positive");
    if (a <0) System.out.println("The number "+a+" is negative");
    if (a==0) System.out.println("The number "+a+" is neutral");
    }

    is this ok?

    if so how would i do a test plan for this

    many thanks Andy..

  3. #3
    Koala is offline Member
    Join Date
    Oct 2011
    Posts
    22
    Rep Power
    0

    Default Re: java program check please

    You're never assigning the user's input to the variable a. You are assigning it to 'reader' which you never use again. Then you're setting 'a' to -5 incrementing it one by one til it gets to 5, printing its value each time. So first, whatever variable you assign the user's input to needs to be the same one you check in your if statements. Second, you don't need a for loop at all.

    Edit: looks like you figured it out in the time it took me to post...
    Last edited by Koala; 10-15-2011 at 11:10 AM.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default Re: java program check please

    Quote Originally Posted by andnlou2678 View Post
    is this ok?
    Did you test your program? What did it do when you type "-999" (the sentinel value)? Did you like what it did?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    andnlou2678 is offline Member
    Join Date
    Sep 2011
    Posts
    69
    Rep Power
    0

    Default Re: java program check please

    yes tested the program works really well thank you

    when i typed in -999 it stopped the program...

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default Re: java program check please

    Quote Originally Posted by andnlou2678 View Post
    yes tested the program works really well thank you

    when i typed in -999 it stopped the program...
    Good; is your program supposed to print that -999 is negative before it ends? (that would be an assignment requirement I guess).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    andnlou2678 is offline Member
    Join Date
    Sep 2011
    Posts
    69
    Rep Power
    0

    Default Re: java program check please

    yes it does that so all is good

    many thanks Andy..

    i have one more that is doing my head in lol


    Write a program to input the type of letter received as a string (either bill, circular, postcard or letter) and output what to do with it as follows:
     bills must be paid
     circulars are thrown away
     postcards are put on the wall
     personal letters are read and have replies written for them

    The program should also output an error message if the letter type is not recognized.

    what u reckon to this

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default Re: java program check please

    Quote Originally Posted by andnlou2678 View Post
    what u reckon to this
    Read the API documentation for the equals( ... ) method in the String class; that method and a few if-else if-else ... statements can solve your problem (together with a readLine() method from the Scanner class or the BufferedReader class).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    andnlou2678 is offline Member
    Join Date
    Sep 2011
    Posts
    69
    Rep Power
    0

    Default Re: java program check please

    thanks for your advice i have tried this but i get an error could you take a look please

    many thanks Andy..


    import java.util.*;
    public class Post
    {
    public static void main(String[]args)
    {
    Scanner s = new Scanner(System.in);
    System.out.print ("Enter the letter type: ");
    String type = s.nextLine();
    processLetter(type);
    }
    private void processLetter(String input)
    {
    if (input.equalsIgnoreCase("bill"))
    System.out.println("Bills must be paid.");
    else if (input.equalsIgnoreCase("circular"))
    System.out.println("Circulars are thrown away.");
    else if (input.equalsIgnoreCase("postcard"))
    System.out.println("Postcards are put on the wall.");
    else if (input.equalsIgnoreCase("letter"))
    System.out.println("Personal letters are read and have replies written for them.");
    else
    System.out.println ("Not recognized.");
    }
    }

    error

    H:\FPT>javac Post.java
    Post.java:11: error: <identifier> expected
    Private void processLetter(String input)
    ^
    Post.java:11: error: invalid method declaration; return type required
    Private void processLetter(String input)
    ^
    2 errors

  10. #10
    skaterboy987 is offline Member
    Join Date
    Sep 2011
    Posts
    56
    Rep Power
    0

    Default Re: java program check please

    I can't figure out what's wrong here but I suggest always adding the brackets on if statements even if it is one line, it's considered sloppy coding and teachers take points off for it.

    Best of luck with a solution!

    PS: I might be able to come up with a fix when I can get to a real computer.

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default Re: java program check please

    Quote Originally Posted by andnlou2678 View Post
    Java Code:
    	private void processLetter(String input)
    
    Post.java:11: error: <identifier> expected
            Private void processLetter(String input)
                   ^
    Your compiler is complaining about "Private" (capital P) but I don't see a capital P in your code. Is the code you posted equal to the code you tried to compile?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: java program check please

    Private's in the army are capitalised. (and so are Majors). But not so the keyword in Java.

    I completely agree with skaterboy about the braces.

  13. #13
    andnlou2678 is offline Member
    Join Date
    Sep 2011
    Posts
    69
    Rep Power
    0

    Default Re: java program check please

    hi thanks for all your comments when i added brackets i got more errors and then took them of and got 1 error this is the error

    H:\FPT>javac Post.java
    Post.java:9: error: non-static method processLetter(String) cannot be referenced
    from a static context
    processLetter(type);
    ^
    1 error


    this is my code ...

    import java.util.Scanner;
    public class Post
    {
    public static void main(String[] args)
    {
    Scanner sc = new Scanner(System.in);
    System.out.print ("Enter the letter type: ");
    String type = sc.nextLine();
    processLetter(type);
    }
    private void processLetter(String input)
    {
    if (input.equalsIgnoreCase("bill"))

    System.out.println("Bills must be paid.");
    else if (input.equalsIgnoreCase("circular"))

    System.out.println("Circulars are thrown away.");
    else if (input.equalsIgnoreCase("postcard"))

    System.out.println("Postcards are put on the wall.");
    else if (input.equalsIgnoreCase("letter"))

    System.out.println("Personal letters are read and have replies written for them.");
    else

    System.out.println ("Not recognized.");
    }
    }

    many thanks Andy..

  14. #14
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default Re: java program check please

    The method processLetter( ... ) isn't static so you need an object of class Post to call it. You don't have such an object so you can't call that method. Either make that method static or create a new Post object and call the method against it.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  15. #15
    andnlou2678 is offline Member
    Join Date
    Sep 2011
    Posts
    69
    Rep Power
    0

    Default Re: java program check please

    could you explain this please as still at the learning stage

    am getting there slowly

    many thanks Andy..

  16. #16
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default Re: java program check please

    Quote Originally Posted by andnlou2678 View Post
    could you explain this please as still at the learning stage

    am getting there slowly
    If a class implements the (non) static methods m1, m2, m3 ... an object (instance) of that class 'understands' those methods m1, m2, m3 ... iow, you can call those methods against the object. If there is no object, you can't call the methods m1, m2, m3 ... In SmallTalk it's easier to explain: an object is a noun and those methods are verbs. You can say, e.g. john.eat(apple) where john is an object and eat is a method (apple is a parameter to the method). If there is no john, you can't tell anyone or anything to eat. In Java, there are static methods and you don't need an object to call them against; you call them against the class itself, in your case: Post.processLetter( ... ) if a method isn't static you can't do that. In your example the processLetter( ... ) isn't static so you can't call it if you don't have a Post object. The compiler rightly complained about it.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Please check my Java program
    By Zora in forum New To Java
    Replies: 6
    Last Post: 09-29-2011, 11:43 PM
  2. please check the program
    By katturv in forum New To Java
    Replies: 1
    Last Post: 01-08-2011, 03:38 PM
  3. java program check
    By ryamz in forum New To Java
    Replies: 4
    Last Post: 07-22-2010, 04:55 AM
  4. CRC check program 1 error
    By javakid9000 in forum New To Java
    Replies: 1
    Last Post: 03-19-2008, 05:04 AM
  5. how to check available resources in java program
    By lealea in forum New To Java
    Replies: 3
    Last Post: 08-13-2007, 08:35 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •