• 10-15-2011, 10:39 AM
andnlou2678
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 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.
• 10-15-2011, 11:02 AM
andnlou2678
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..
• 10-15-2011, 11:08 AM
Koala
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...
• 10-15-2011, 11:51 AM
JosAH
Quote:

Originally Posted by andnlou2678
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
• 10-15-2011, 05:09 PM
andnlou2678
yes tested the program works really well thank you

when i typed in -999 it stopped the program...
• 10-15-2011, 05:44 PM
JosAH
Quote:

Originally Posted by andnlou2678
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
• 10-15-2011, 06:29 PM
andnlou2678
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
• 10-15-2011, 07:16 PM
JosAH
Quote:

Originally Posted by andnlou2678
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
• 10-15-2011, 11:18 PM
andnlou2678
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-16-2011, 06:42 AM
skaterboy987
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.
• 10-16-2011, 06:50 AM
JosAH
Quote:

Originally Posted by andnlou2678
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
• 10-16-2011, 06:58 AM
pbrockway2
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.
• 10-16-2011, 10:15 AM
andnlou2678
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..
• 10-16-2011, 10:30 AM
JosAH
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
• 10-16-2011, 10:33 AM
andnlou2678
could you explain this please as still at the learning stage

am getting there slowly

many thanks Andy..
• 10-16-2011, 10:45 AM
JosAH