Odd Bracket Conundrum

Page 1 of 1

12 Replies - 528 Views - Last Post: 14 October 2012 - 07:35 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=295512&amp;s=a91f847c97fd12a8605b29c36c78953f&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 thorax232

Reputation: 1
• Posts: 94
• Joined: 10-September 12

Odd Bracket Conundrum

Posted 13 October 2012 - 06:47 PM

Code:

```//Tells how many numbers are positive and negative. Plus prints total.
//Prepared by Ethan Glover

import java.util.Scanner;
public class Prog4i1 {
public static void main(String[] args) {

Scanner input = new Scanner(System.in);
System.out.println("Enter an integer, the input ends if it is 0: ");

//Set variables
int total = 0;
double average = 0.0;
int numOfPos = 0;
int numOfNeg = 0;
int numOfNum = 0;
int value;

//Calcuate numbers
do {
value = scan.nextInt();
if (value > 0) {
numOfPos++;
numOfNum++;
total += value;
}
else {
numOfNeg++;
numOfNum++;
total += value;
} while (value != 0)
}

//Calculate average
average = total/(numOfNum);

//Display output
if (total > 0) {
System.out.println("The number of positives is " + numOfPos);
System.out.println("The number of negatives is " + numOfNeg);
System.out.println("The total is " + total);
System.out.println("The average is " + average);
}
else {
System.out.println("No numbers are entered except 0");
}
}
}

```

Errors:

```Prog4i1.java:33: error: illegal start of expression
}
^
Prog4i1.java:48: error: while expected
}
^
Prog4i1.java:49: error: reached end of file while parsing
}
^

```

Expected Output:

```Enter an integer, the input ends if it is 0: ex. 1 2 -1 3 0
The number of positives is 3
... negatives is 1
... total is 5
... average is 1.25

-or-

Enter an integer, the input ends if it is 0: ex. 0
No numbers are entered except 0

```

This post has been edited by thorax232: 13 October 2012 - 06:48 PM

Is This A Good Question/Topic? 0

Replies To: Odd Bracket Conundrum

#2 thorax232

Reputation: 1
• Posts: 94
• Joined: 10-September 12

Re: Odd Bracket Conundrum

Posted 13 October 2012 - 07:22 PM

Found bracket error, my while was inside the do brackets.

Except now I have another problem. I'm getting the error:
```Prog4il.java:22: error: cannot find symbol
value = scan.nextInt();
^

```

Is this not how you allow the user to input as many number as they want?

#3 jon.kiparsky

• Pancakes!

Reputation: 9530
• Posts: 16,478
• Joined: 19-March 11

Re: Odd Bracket Conundrum

Posted 13 October 2012 - 07:24 PM

``` Scanner input = new Scanner(System.in);
```

scan? What's scan?

#4 thorax232

Reputation: 1
• Posts: 94
• Joined: 10-September 12

Re: Odd Bracket Conundrum

Posted 13 October 2012 - 07:55 PM

I know it should be:

```int value = input.nextInt();

```

NOT
```value = scan.nextInt();
```

I have Scanner.input at the top. Now, with that scan thing fixed I get:

```32: error: cannot find symbol
while (value != 0);
^
```

=/ Here's a partebin link with my updated code: http://pastebin.com/ekNEs4Ru

#5 DimitriV

• vexing conundrum

Reputation: 587
• Posts: 2,746
• Joined: 24-July 11

Re: Odd Bracket Conundrum

Posted 13 October 2012 - 08:06 PM

Value lives inside the loop, not outside. Declare it outside of the loop, because inside the loop it is only inside the scope of the loop.
```int value;
do {
// code
}
while (value != 0)

```

#6 rfs02

Reputation: 26
• Posts: 70
• Joined: 30-September 12

Re: Odd Bracket Conundrum

Posted 13 October 2012 - 08:14 PM

Now of course, the moment you fix this problem, you will realize that you are counting 0 as a negative number, which messes up your count of negative integers and your average

#7 thorax232

Reputation: 1
• Posts: 94
• Joined: 10-September 12

Re: Odd Bracket Conundrum

Posted 13 October 2012 - 08:32 PM

Ugh. >.> I may need to start a new thread for this stupid thing. I can run it now, but it won't print anything, just sits like it's in an infinite loop, when I put in "1 2 -1 3 0"

I've even changed it from a do while to a do loop just trying to figure it out.
Again here's pastebin: http://pastebin.com/ekNEs4Ru

I just don't get where any logic errors may be. I'm new to loops, maybe I'm using it wrong? I don't know.

Oh and thanks rfs02 for pointing that out about 0, more issues for me to fret over.

This post has been edited by thorax232: 13 October 2012 - 08:33 PM

#8 DimitriV

• vexing conundrum

Reputation: 587
• Posts: 2,746
• Joined: 24-July 11

Re: Odd Bracket Conundrum

Posted 13 October 2012 - 08:34 PM

Set the value of value within the loop!

#9 thorax232

Reputation: 1
• Posts: 94
• Joined: 10-September 12

Re: Odd Bracket Conundrum

Posted 13 October 2012 - 08:42 PM

DimitriV, on 13 October 2012 - 08:34 PM, said:

Set the value of value within the loop!

Sorry, I've kind of overcomplicated this thread making my own changes, are you referring to doing this in a DoLoop or DoWhileLoop. (My original post or pastebin.) I know the changes don't help, but I'm kind of tenacious when it comes to these things. XD

#10 DimitriV

• vexing conundrum

Reputation: 587
• Posts: 2,746
• Joined: 24-July 11

Re: Odd Bracket Conundrum

Posted 13 October 2012 - 08:49 PM

```int value;
do {

value = input.nextInt();
//Count variables
numOfNum++;
total += value;
if (value > 0) {
numOfPos++;
}
else {
numOfNeg++;
}
} while (value != <number>)
```

That's kind of what I thought but I'm not sure if it is right or not.

#11 thorax232

Reputation: 1
• Posts: 94
• Joined: 10-September 12

Re: Odd Bracket Conundrum

Posted 13 October 2012 - 09:01 PM

DimitriV, on 13 October 2012 - 08:49 PM, said:

That's kind of what I thought but I'm not sure if it is right or not.

Ha, that works well! I wrote up code almost exactly like that just before you posted, except I didn't put the int = value; right above the beginning of the loop. Now I just need to figure out how to get it to count the 0 with the total. That's my problem though.

Thanks a lot Dimitri!

This post has been edited by thorax232: 13 October 2012 - 09:02 PM

#12 rfs02

Reputation: 26
• Posts: 70
• Joined: 30-September 12

Re: Odd Bracket Conundrum

Posted 14 October 2012 - 12:37 AM

Also, check what happens if you enter:

-5, 1, 0

AND - what happens if you just enter 0

#13 thorax232

Reputation: 1
• Posts: 94
• Joined: 10-September 12

Re: Odd Bracket Conundrum

Posted 14 October 2012 - 07:35 AM

rfs02, on 14 October 2012 - 12:37 AM, said:

Also, check what happens if you enter:

-5, 1, 0

AND - what happens if you just enter 0

Oooo, very interesting. Why do you say -5, 1, 0? That prints out "nothing was entered but 0". =/ 0 works though.