954,506 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Stringtokenizer needs to ignore Negative numbers and terminate when 0 is enter


How do I get this to ignore negative number and terminate when the number 0 is entered?
Please HELP

public void productTokenizer()throws IOException
{
System.out.println("Enter numbers to accumlulate their total.");//Prompt user to enter data
//System.out.println("Zero and all negative numbers will Exit the Program:");

double sum = 0; int num = 0;

BufferedReader br = new BufferedReader(
new InputStreamReader(System.in));
String input = br.readLine();

StringTokenizer tokenizer = new StringTokenizer(input);


while (tokenizer.hasMoreTokens())
{



String token = tokenizer.nextToken();

double x = Double.parseDouble(token);
sum = sum + x;
num++;
}

System.out.println("The sum is: " + sum);

}
}

Katherine692008
Light Poster
32 posts since Feb 2008
Reputation Points: 10
Solved Threads: 0
 

i think it can be done after converting into double please ckeck with an if condition if(x>0) then i think you can access only positive numbers

Samudricka
Newbie Poster
22 posts since Nov 2007
Reputation Points: 10
Solved Threads: 1
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You