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 12 of 12

Thread: Need some simple Java Help

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need some simple Java Help

    I recieved a bonus assignment for my Java class and I was wondering if anyone could help, they are pretty simple im just a new coder. (if it matters i use JGRASP)

    First one is " Write a program that reads an integer and prints whether it is negative, zero or positive"

    Second is "Write a program that displays the dimensions of a letter-size(8.5x11 inches) sheet of paper in millimeters. There are 25.4 millimeters per inch. Use constants and comments in your program.


  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: Need some simple Java Help

    Can you post the code you have and any questions about the problems you are having with it?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Sep 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need some simple Java Help

    #include <stdio.h>
    #include <conio.h>
     
    void main()
    {
     int number;
     clrscr();
     
     printf("Enter a number\n");
     scanf ("%d", &number);
     
     if (number > 0)
      printf ("%d, is a positive number\n", number);
     else
      printf ("%d, is a negative number\n", number);
     
    }

    is what I have so far from problem one, also im not sure about how to get it to print if its 0.


    And I am clueless on the second one (sorry for the inexperience)
    Last edited by helloworld922; September 26th, 2012 at 12:09 PM.

  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: Need some simple Java Help

    #include <stdio.h>
    #include <conio.h>
     
    void main()
    {
    int number;
    clrscr();
     
    printf("Enter a number\n");
    scanf ("%d", &number);
     
    if (number > 0)
    printf ("%d, is a positive number\n", number);
    else
    printf ("%d, is a negative number\n", number);
     
    }

    Have you posted the right code? The code in post#3 looks like C not java.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Sep 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need some simple Java Help

    // Description: Determines positive, negative, sum, and average of integers. Ends when 0 input.
    import java.util.Scanner;
     
    public class Compute {
    	public static void main (String[] args) {
    	  //name integers
    	  int data;
    	  int sum = 0;
    	  double ave = 0;
    	  int positive = 0;
    	  int negative = 0;
     
     
    	//Create Scanner	  
    	Scanner input = new Scanner(System.in);
     
    	// initial input and continue until 0 is entered
    	System.out.print(
    	  "Enter an int value (The program exits if the input is 0): ");
    	data = input.nextInt();
     
     
    	while (data != 0) {
    	  sum += data; //add input to running sum
    	  if data <=0 { += positive++ //count positive by adding 1 to the count
    		else = negative++; //count negative integers
    	  }     
    	}  
     
    	// end result of user input
     
    	  ave = ((double)sum) / count;
    	  System.out.println ("The number of positives is " + positive);
    	  System.out.println ("The number of negatives is " + negative);
    	  System.out.println ("The total is " + sum);
    	  System.out.println ("The average is " + ave);
     
     
     
       }
    }

    ** I just tried it on my own is this correct?
    Last edited by Norm; September 26th, 2012 at 03:57 PM. Reason: added code tags

  6. #6
    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: Need some simple Java Help

    is this correct?
    What happens when you compile and execute the program?
    If you get any errors you need help with, copy the full text and paste it here.

    If it executes, Does it give you the correct answers?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Sep 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need some simple Java Help

    Quote Originally Posted by Norm View Post
    What happens when you compile and execute the program?
    If you get any errors you need help with, copy the full text and paste it here.

    If it executes, Does it give you the correct answers?
    Exception in thread "main" java.lang.Error: Unresolved compilation problems:
    Syntax error on token "if", ( expected after this token
    Syntax error on tokens, Expression expected instead
    Syntax error on token "else", delete this token
    Syntax error, insert ") Statement" to complete IfStatement
    Syntax error on token "}", delete this token

    at Compute.main(MainClass.java:25)

  8. #8
    Junior Member
    Join Date
    Sep 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need some simple Java Help

    // Description: Determines positive, negative, sum, and average of integers. Ends when 0 input.
    import java.util.Scanner;
     
    public class Compute {
    	public static void main (String[] args) {
    	  //name integers
    	  int data;
    	  int sum = 0;
    	  double ave = 0;
    	  int positive = 0;
    	  int negative = 0;
    	  int count = 0;
     
    	//Create Scanner	  
    	Scanner input = new Scanner(System.in);
     
    	// initial input and continue until 0 is entered
    	System.out.print ("Enter an int value (The program exits if the input is 0): ");
    	    data = input.nextInt();
     
     
    	while (data != 0) {
    	  sum += data; //add input to running sum
    	  count++;
    	  if (data <=0) { positive++; }//count positive by adding 1 to the count
    		else {negative++;} //count negative integers   
    	  System.out.print ("Enter an int value (The program exits if the input is 0): ");
    	    data = input.nextInt();
    	} 
     
    	// end result of user input
     
    	  ave = ((double)sum) / count;
    	  System.out.println ("The number of positives is " + positive);
    	  System.out.println ("The number of negatives is " + negative);
    	  System.out.println ("The total is " + sum);
    	  System.out.println ("The average is " + ave);
     
     
     
       }
    }

    I fixed it one more time but i get an error of "Exception in thread "main" java.lang.Error: Unresolved compilation problem:

    at Compute.main(MainClass.java:6)"
    Last edited by Norm; September 26th, 2012 at 04:49 PM. Reason: added code tags

  9. #9
    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: Need some simple Java Help

    Can you just compile the program and not try to execute it so that you get the error messages from the compiler? There is important information in the error messages. They show the source line number and a good description of the error.
    Here is a sample:
    TestSorts.java:138: cannot find symbol
    symbol  : variable var
    location: class TestSorts
             var = 2;
             ^

    In future posts please wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Junior Member
    Join Date
    Sep 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need some simple Java Help

    I finally got it thank you it was just a computation error.

    Any idea, on where to start for question 2?

  11. #11
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Need some simple Java Help

    You could always copy and paste a C program you found online like you did for the first question.

  12. #12
    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: Need some simple Java Help

    What is there to do in question 2? Print the values of some expressions.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Simple Java Program
    By Robbiep in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 24th, 2011, 07:27 AM
  2. eclipse java simple example
    By aswra1 in forum Object Oriented Programming
    Replies: 2
    Last Post: March 24th, 2011, 10:21 AM
  3. New to Java I need help with a simple mistake
    By Reynalto in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 2nd, 2010, 04:12 PM
  4. simple java help needed
    By miss confused in forum What's Wrong With My Code?
    Replies: 6
    Last Post: June 27th, 2010, 12:29 PM
  5. Simple game in Java
    By velop in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 27th, 2010, 05:04 AM