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

Thread: Need help with Java assignment

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need help with Java assignment

    Hi guys,

    I'm having trouble with this simple java exercise. You are supposed to make java print following lines:

    Insert first number:66
    Insert second number: 3
    66+3 = 69 System.out.println(summa = first + second);
    66-3 = 63 System.out.println(summa = first - second);
    66*3 = 198 System.out.println(summa = first * second);
    66/3 = 22.0 System.out.println(sum = first / second);
    66%3: 0 System.out.println(sum = first % second);

    the question is how you make it print it like that (66+3 = 69). I've posted the way i have written it.
    I have written the rest of the code such as getting the input etc. I only need to know you make it print numbers like that?

    Thanks


  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 help with Java assignment

    how you make it print it like that (66+3 = 69)
    The data types of the variables will determine how the compiler processes the + operand.
    It they are numeric (like int): 6 + 7 will return 13. If Strings: "6"+"7" will return "67"
    If you want the + to be included in the String enclose it in "s: "+"

    Can you post the code you are working with and the output that it creates to show the problem.
    Also post what you want the output to be.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help with Java assignment

    Thanks for the quick reply,

    Here's what I have come up with:

    import java.io.*;

    public class Lasku {
    public static void main(String[] args){
    int eka;
    int toka;
    int summa;

    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

    try{System.out.print("Syötä ensimmäinen kokonaisluku:");
    eka = Integer.parseInt(in.readLine());

    System.out.print("Syötä toinen kokonaisluku:");
    toka = Integer.parseInt(in.readLine());

    System.out.println(eka & toka + (summa = eka + toka));
    System.out.println(summa = eka - toka);
    System.out.println(summa = eka * toka);
    System.out.println(summa = eka / toka);
    System.out.println("Jakojäännös:" + (summa = eka % toka));

    }
    catch (Exception e){}
    }
    }

    Here's the output, dont worry about the language it's finnish

    Syötä ensimmäinen kokonaisluku: 66 <-- means insert first number
    Syötä toinen kokonaisluku: 3 <-- means insert second number
    69
    63
    198
    22
    Jakojäännös:0

    It should be:

    Syötä ensimmäinen kokonaisluku:66
    Syötä toinen kokonaisluku:3
    66+3 = 69
    66-3 = 63
    66*3 = 198
    66/3 = 22.0
    Jakojäännös: 0

  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 help with Java assignment

    66+3 = 69
    To get that to print out you need to print 5 different Strings:
    66
    +
    3
    =
    69

    You can build the full String to be printed by using the + operator to concatenate the individual Strings into a single String.
    Try printing this: 4 + "+" + 5
    It has three parts:
    4
    +
    5
    that are concatenated into one String with the + operator.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Nov 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help with Java assignment

    Yeah that was it, now it works perfectly. Thanks alot

Similar Threads

  1. java assignment; please help
    By willc86 in forum Java Theory & Questions
    Replies: 3
    Last Post: November 4th, 2012, 08:05 AM
  2. assignment troubles polymorphism (guide for assignment included)
    By tdawg422 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 8th, 2011, 10:01 AM
  3. Help with a java assignment?
    By rberry719 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: September 30th, 2011, 01:02 PM
  4. my java assignment -- please help
    By java_beginner in forum Java Theory & Questions
    Replies: 6
    Last Post: May 20th, 2010, 08:32 AM
  5. Replies: 1
    Last Post: February 22nd, 2010, 08:20 AM

Tags for this Thread