12 Replies - 427 Views - Last Post: 14 October 2012 - 07:35 AM Rate Topic: -----

#1 thorax232  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • 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) {

	//Ask for user input
	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  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#3 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7994
  • View blog
  • Posts: 13,678
  • 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? :)
Was This Post Helpful? 1
  • +
  • -

#4 thorax232  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#5 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 584
  • View blog
  • Posts: 2,738
  • 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)


Was This Post Helpful? 2
  • +
  • -

#6 rfs02  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 26
  • View blog
  • 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 :balloon:
Was This Post Helpful? 2
  • +
  • -

#7 thorax232  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • 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. :D

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

Was This Post Helpful? 0
  • +
  • -

#8 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 584
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: Odd Bracket Conundrum

Posted 13 October 2012 - 08:34 PM

Set the value of value within the loop!
Was This Post Helpful? 1
  • +
  • -

#9 thorax232  Icon User is offline

  • D.I.C Head

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

Re: Odd Bracket Conundrum

Posted 13 October 2012 - 08:42 PM

View PostDimitriV, 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
Was This Post Helpful? 0
  • +
  • -

#10 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 584
  • View blog
  • Posts: 2,738
  • 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.
Was This Post Helpful? 2
  • +
  • -

#11 thorax232  Icon User is offline

  • D.I.C Head

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

Re: Odd Bracket Conundrum

Posted 13 October 2012 - 09:01 PM

View PostDimitriV, 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. :P

Thanks a lot Dimitri!

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

Was This Post Helpful? 0
  • +
  • -

#12 rfs02  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 26
  • View blog
  • 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
Was This Post Helpful? 1
  • +
  • -

#13 thorax232  Icon User is offline

  • D.I.C Head

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

Re: Odd Bracket Conundrum

Posted 14 October 2012 - 07:35 AM

View Postrfs02, 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. >.<
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1