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

Thread: newbie Question.....

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

    Red face newbie Question.....

    this is my first time using Java.... self Study only.. and i have a problem in looping can any one correct me... or give me sample code...

    Scanner user = new Scanner(System.in);
    int loopVal;
    int end_value = 11;
    int total;
    int num = 10;

    for (loopVal = 1; loopVal < end_value; loopVal++){
    total = loopVal * num;
    System.out.println(+loopVal +"i want to insert a number using a string that can times to my loopVal "+" Times "+" "+" = " +total);
    }

    }
    }



    Thanks


  2. #2
    Junior Member
    Join Date
    Aug 2012
    Posts
    8
    My Mood
    Mellow
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Is your problem loop logic or how to extract an integer from a string? Maybe a little of both?

    Please take your question out of the middle of your code and restate it more clearly.

  3. #3
    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: newbie Question.....

    Please Edit your post and 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.

  4. #4
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: newbie Question.....

    The way that java Strings work is: If you add a numeric variable to a String (with the + operator), the value of the numeric variable is converted to a String and the converted String's value is concatenated with the original String value to form a new single String.

    And then...

    If you add a String to a String (with the + operator), their values are concatenated to form a new single String.

    Furthermore...

    If you add a bunch of strings (with + operators), all of their values are concatenated to form a new single String.

    public class Z
    {
        public static void main(String [] args)
        {
            int x = 42;
            System.out.println("The answer is " + x + ".  Now what was the question?");
        }
    }

    Output:

    The answer is 42. Now what was the question?



    Cheers!

    Z

  5. #5
    Junior Member
    Join Date
    Aug 2012
    Posts
    5
    My Mood
    Inspired
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: newbie Question.....

    Quote Originally Posted by Zaphod_b View Post
    The way that java Strings work is: If you add a numeric variable to a String (with the + operator), the value of the numeric variable is converted to a String and the converted String's value is concatenated with the original String value to form a new single String.

    And then...

    If you add a String to a String (with the + operator), their values are concatenated to form a new single String.


    Furthermore...

    If you add a bunch of strings (with + operators), all of their values are concatenated to form a new single String.

    public class Z
    {
        public static void main(String [] args)
        {
            int x = 42;
            System.out.println("The answer is " + x + ".  Now what was the question?");
        }
    }

    Output:

    The answer is 42. Now what was the question?



    Cheers!

    Z

    My problem is i want to time the loopVal to a User entry.. is it possible to use this kind of code??

    total = loopVal * input number here;
    Last edited by Haseo; August 21st, 2012 at 10:09 PM.

  6. #6
    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: newbie Question.....

    Quote Originally Posted by Haseo View Post
    My problem is i want to time the loopVal to a User entry.. is it possible to use this kind of code??

    total = loopVal * input number here;
    Sure. Why not simply get the user's input and then have your loop code immediately after you get the user's input. Where exactly are you stuck?

  7. #7
    Junior Member
    Join Date
    Aug 2012
    Posts
    5
    My Mood
    Inspired
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: newbie Question.....

    Quote Originally Posted by curmudgeon View Post
    Sure. Why not simply get the user's input and then have your loop code immediately after you get the user's input. Where exactly are you stuck?
    here's my code can you correct me....
            int end_value = 11;
            int loopVal;
            int total;
     
            String number;
            System.out.println();
            number = user.next();
     
          for(loopVal = 1; loopVal < end_value; loopVal++){
     
     
              System.out.println("Loop Value "+loopVal);
          }


    I try to use this code before System.out.println...
    total = loopVal * number; <<<< Error..

  8. #8
    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: newbie Question.....

    1. what error do you see? Please post the whole error message.
    2. The Scanner#next() method returns a String when you want to get an int, and that won't work for you.
    3. Scanner has a better method for getting an int. Have a look at the Scanner API and see if you can figure out what it is.
    4. You're declaring number as a String variable. Don't you want this to be an int variable instead?

  9. #9
    Junior Member
    Join Date
    Aug 2012
    Posts
    5
    My Mood
    Inspired
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: newbie Question.....

    Quote Originally Posted by curmudgeon View Post
    1. what error do you see? Please post the whole error message.
    2. The Scanner#next() method returns a String when you want to get an int, and that won't work for you.
    3. Scanner has a better method for getting an int. Have a look at the Scanner API and see if you can figure out what it is.
    4. You're declaring number as a String variable. Don't you want this to be an int variable instead?
    i try another some code like this.


        int i;
        int n = 10;
        int sum;
     
        for (i = 0; i < n; i++){
            sum = i * n;
            System.out.println(i + " Times " + n +" = "+ sum);
    output:

    0 Times 10 = 0
    1 Times 10 = 10
    2 Times 10 = 20
    3 Times 10 = 30
    4 Times 10 = 40
    5 Times 10 = 50
    6 Times 10 = 60
    7 Times 10 = 70
    8 Times 10 = 80
    9 Times 10 = 90
    before > sum = i * n; <<< i wan't to * any number that the user input any number but my problem is is't possible? ... int * string??

    now > total = user.nextint(); System.out.println(loopVal + " Times = " + loopVal * total); << it work but the problem is it's not looping

    System.out.println("Enter A Number");
     
       for (i = 1; i < n; i++){
           sum = conin.nextInt();
           System.out.println(i + " total = " + i * sum);
    output:
    Enter A Number
    10
    1 total = 10 << problem not continues looping....
    Last edited by Haseo; August 22nd, 2012 at 01:45 AM.

  10. #10
    Junior Member
    Join Date
    Jul 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: newbie Question.....

    I think you miss initial value of n variable ....

Similar Threads

  1. Newbie Question
    By mmazur in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: August 15th, 2012, 10:15 PM
  2. Newbie Java Question
    By tinkersp in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 23rd, 2011, 01:41 PM
  3. Newbie Question on a Code - I don't know if this is Java
    By fresca in forum Java Theory & Questions
    Replies: 4
    Last Post: April 7th, 2011, 08:39 PM
  4. newbie question about Abstract methods
    By FailMouse in forum Java Theory & Questions
    Replies: 3
    Last Post: August 10th, 2010, 11:51 PM
  5. Looping Question (newbie)
    By xdrechsler in forum Loops & Control Statements
    Replies: 13
    Last Post: July 19th, 2010, 09:12 PM