8 Replies - 297 Views - Last Post: 18 December 2013 - 05:24 AM Rate Topic: -----

#1 AMDee  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 56
  • Joined: 17-December 13

Addition in System.out.print

Posted 17 December 2013 - 11:04 PM

for(int rowNum = 0; rowNum < x.length; rowNum++)
            {
                System.out.println("Enter your grade for number "+rowNum":");
                x[rowNum] = input.nextInt();
            }



I want to print out
--
Enter your grade for number 1 //Instead of starting with 0
Enter your grade for number 2
Enter your grade for number 3
and etc......
--
I was wondering if you could do addition within the System.out.print();.

Is This A Good Question/Topic? 0
  • +

Replies To: Addition in System.out.print

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10384
  • View blog
  • Posts: 38,434
  • Joined: 27-December 08

Re: Addition in System.out.print

Posted 17 December 2013 - 11:06 PM

Use a pair of parentheses, like so:
System.out.println("Enter your grade for number "+ (rowNum + 1) + ":");



Happy coding!
Was This Post Helpful? 2
  • +
  • -

#3 astonecipher  Icon User is offline

  • Major DIC Head
  • member icon

Reputation: 561
  • View blog
  • Posts: 2,516
  • Joined: 03-December 12

Re: Addition in System.out.print

Posted 17 December 2013 - 11:10 PM

I believe you can, but why not start at 1 instead of 0?
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10384
  • View blog
  • Posts: 38,434
  • Joined: 27-December 08

Re: Addition in System.out.print

Posted 17 December 2013 - 11:12 PM

Because of the array. Alternatively, starting at 1 and subtracting 1 from the index in the array on the second line would work too. :)
Was This Post Helpful? 1
  • +
  • -

#5 astonecipher  Icon User is offline

  • Major DIC Head
  • member icon

Reputation: 561
  • View blog
  • Posts: 2,516
  • Joined: 03-December 12

Re: Addition in System.out.print

Posted 17 December 2013 - 11:18 PM

If adding 1 in the print, wouldn't that cause an increment of 2 in the loop? What about ++rowNum?
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10384
  • View blog
  • Posts: 38,434
  • Joined: 27-December 08

Re: Addition in System.out.print

Posted 17 December 2013 - 11:19 PM

Look at my code in my first post. Because there is no addition, the effect you described will not happen.
Was This Post Helpful? 0
  • +
  • -

#7 astonecipher  Icon User is offline

  • Major DIC Head
  • member icon

Reputation: 561
  • View blog
  • Posts: 2,516
  • Joined: 03-December 12

Re: Addition in System.out.print

Posted 17 December 2013 - 11:41 PM

I see. I was thinking that by doing (i + 1) it would pre-increment the counter. But, the variable does not change.
Was This Post Helpful? 0
  • +
  • -

#8 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1940
  • View blog
  • Posts: 4,027
  • Joined: 11-December 07

Re: Addition in System.out.print

Posted 18 December 2013 - 01:23 AM

Now would be a good time to make sure you understand the subtle differences between these three. What do they each print out? What is the value of rowNum before and after the line is printed out?

System.out.println("Enter your grade for number "+ (rowNum + 1) + ":");

System.out.println("Enter your grade for number "+ (rowNum++) + ":");

System.out.println("Enter your grade for number "+ (++rowNum) + ":");

Was This Post Helpful? 0
  • +
  • -

#9 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2651
  • View blog
  • Posts: 11,182
  • Joined: 20-September 08

Re: Addition in System.out.print

Posted 18 December 2013 - 05:24 AM

You'll get code that's both more readable and clean with printf

        for (int rowNum = 0; rowNum < x.length; rowNum++) {
            System.out.printf("Enter your grade for number %d: ", rowNum + 1);
            x[rowNum] = input.nextInt();
        }


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1