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

Thread: simple multiplication exercise.

  1. #1
    Junior Member
    Join Date
    Aug 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default simple multiplication exercise.

    Id be glad if someone can explain whats wrong with my code below to achieve the goal of this exercise:



    Write a times table programme. The programme should ask a user to input a number. This number is then used as the times table. So if the user enters 10, the 10 times table should be displayed. Your Output window should look something like this, when your programme is run.



    output_times_table.gif







    my noob code ;
    package calculator;
     
    import java.util.Scanner;
     
     
     
    public class Calculator {
     
     
        public static void main(String[] args) {
     
            Scanner omar = new Scanner(System.in);
     
     
            System.out.println("Enter a number: ");
     
            int fnum= 9;
     
            int snum= 10;
     
            for (int n=1; n<=snum; n++) 
     
            System.out.println(fnum + "x" + n + "=" + fnum*n);
    Last edited by helloworld922; August 22nd, 2012 at 12:12 AM.


  2. #2
    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: simple multiplication exercise.

    Can you first tell us what is wrong? Are you getting a compilation error? Is the code throwing a run-time exception? Is it compiling and running but not behaving well? These details can help us help you.

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: simple multiplication exercise.

    In the future please use code tags. They make your code much more readable.

    The first problem I can see is that the code is incomplete. You're missing at least a few closing braces at the end. This could be a copy-paste error, or you just need to add them. Properly formatting your code is the easiest way to catch these types of problems.

    You say there's something wrong with your code. What is it doing that's wrong? What output are you getting? If there's a compile error or exception, what exception are you getting? Please post the full message/output.

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

    Default Re: simple multiplication exercise.

    Well im not getting the output I want.

    run:
    Enter a number:
    9x1=9
    9x2=18
    9x3=27
    9x4=36
    9x5=45
    9x6=54
    9x7=63
    9x8=72
    9x9=81
    9x10=90
    BUILD SUCCESSFUL (total time: 0 seconds)


    i want it so that i get the same output as the exercise results i posted above. wont let me input number ^^

    i already defined int fnum= 9; but thats supposed to be the user input. how do i make it whatever the user inputs? ty <3

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: simple multiplication exercise.

    how do i make it whatever the user inputs?
    Use a method of the Scanner class to read in the number the user enters.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: simple multiplication exercise.

    i tried like this but i get red underline error

    int fnum= new Scanner(System.in);

    int snum= 10;

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: simple multiplication exercise.

    Using a method of a class is a two step process.
    First define an instance of the class
    Then call one of its methods.
    int fnum= new Scanner(System.in);
    The new statement returns an instance of the class, not an int value
    The code in post #1 was correct:
     Scanner omar = new Scanner(System.in);
    Now use the omar reference to a Scanner class object to call one of the Scanner class methods that reads from the user and returns an int value.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: simple multiplication exercise.

    You need to assign the Scanner object to a Scanner variable. You can't assign it to an int.

    int fnum = new Scanner(System.in); // error: fnum is an int variable, not a Scanner
    Scanner reader = new Scanner(System.in); // correct: reader is a scanner variable and can hold Scanner objects

    You'll also need to specifically poll the scanner for an integer. Likely you'll want to use the nextInt() method. Read the Javadoc to determine how this method works and how to use it.

Similar Threads

  1. Multiplication program Loop
    By PhilThomspon in forum Loops & Control Statements
    Replies: 7
    Last Post: May 6th, 2012, 03:29 PM
  2. Multiplication program
    By PhilThomspon in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 6th, 2012, 01:54 PM
  3. Replies: 4
    Last Post: May 15th, 2011, 06:03 AM
  4. Recursive multiplication and Karatsuba
    By jsp01 in forum Algorithms & Recursion
    Replies: 0
    Last Post: March 14th, 2011, 02:04 AM
  5. Multiplication code
    By bomboy in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 5th, 2011, 02:25 AM