Results 1 to 16 of 16
Thread: java program check please
- 10-15-2011, 10:39 AM #1
Member
- Join Date
- Sep 2011
- Posts
- 69
- Rep Power
- 0
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.
- 10-15-2011, 11:02 AM #2
Member
- Join Date
- Sep 2011
- Posts
- 69
- Rep Power
- 0
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..
- 10-15-2011, 11:08 AM #3
Member
- Join Date
- Oct 2011
- Posts
- 22
- Rep Power
- 0
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.
- 10-15-2011, 11:51 AM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
- 10-15-2011, 05:09 PM #5
Member
- Join Date
- Sep 2011
- Posts
- 69
- Rep Power
- 0
Re: java program check please
yes tested the program works really well thank you
when i typed in -999 it stopped the program...
- 10-15-2011, 05:44 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
- 10-15-2011, 06:29 PM #7
Member
- Join Date
- Sep 2011
- Posts
- 69
- Rep Power
- 0
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
- 10-15-2011, 07:16 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Re: java program check please
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 10-15-2011, 11:18 PM #9
Member
- Join Date
- Sep 2011
- Posts
- 69
- Rep Power
- 0
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-16-2011, 06:42 AM #10
Member
- Join Date
- Sep 2011
- Posts
- 56
- Rep Power
- 0
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.
- 10-16-2011, 06:50 AM #11
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Re: java program check please
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 10-16-2011, 06:58 AM #12
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
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.
- 10-16-2011, 10:15 AM #13
Member
- Join Date
- Sep 2011
- Posts
- 69
- Rep Power
- 0
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..
- 10-16-2011, 10:30 AM #14
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 10-16-2011, 10:33 AM #15
Member
- Join Date
- Sep 2011
- Posts
- 69
- Rep Power
- 0
Re: java program check please
could you explain this please as still at the learning stage
am getting there slowly
many thanks Andy..
- 10-16-2011, 10:45 AM #16
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Re: java program check please
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
Please check my Java program
By Zora in forum New To JavaReplies: 6Last Post: 09-29-2011, 11:43 PM -
please check the program
By katturv in forum New To JavaReplies: 1Last Post: 01-08-2011, 03:38 PM -
java program check
By ryamz in forum New To JavaReplies: 4Last Post: 07-22-2010, 04:55 AM -
CRC check program 1 error
By javakid9000 in forum New To JavaReplies: 1Last Post: 03-19-2008, 05:04 AM -
how to check available resources in java program
By lealea in forum New To JavaReplies: 3Last Post: 08-13-2007, 08:35 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks