Calculate Power of a number

Help on sytemout.println

Page 1 of 1

9 Replies - 9796 Views - Last Post: 22 January 2010 - 02:42 PM Rate Topic: -----

#1 fwang321  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 22-January 10

Calculate Power of a number

Posted 22 January 2010 - 12:30 PM

So here is my code, it is a cheap way to calculate the power of a number to place it in scientific notation. It just counts the number of numbers and subtracts 1 from it. It does what i ask but it prints out ever process it goes through to get the final answer. How do i make it just print the final answer?
I am also trying to prevent someone from typing a letter and making the program end.

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

		Scanner scan = new Scanner( System.in );
		int i = 0;
		System.out.println ("Give me a Number");
	  
	String phrase = scan.nextLine();   

	for (i = 0; i < 3; i++){

	phrase = phrase.toLowerCase();

	int count = 0;
	for (i = 0; i < phrase.length(); i++) {
	char c = phrase.charAt(i);
	if (c=='1' || c=='2' || c=='3' || c=='4' || c=='5' || c=='6' || c=='7' || c=='8' || c=='9' || c=='0') {
	count++;
	}
	if (count == 2)
	{System.out.println( phrase + " is to the " + (count - 1) + "st " + "power");} 
	if (count == 3)
	{System.out.println( phrase + " is to the " + (count - 1) + "nd " + "power");}
	if (count == 4)
	{System.out.println( phrase + " is to the " + (count - 1) + "rd " + "power");}
	if (count > 4)
	{System.out.println( phrase + " is to the " + (count - 1) + "th " + "power");}	
	else 
		System.out.println( phrase + " includes a letter or symbol,\n symbols or letters do not have powers");
	}
	
	System.exit(0);
		}
	}
}


Edited by Locke. Please, in the future --> :code: :)

This post has been edited by Locke: 22 January 2010 - 01:13 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Calculate Power of a number

#2 SwiftStriker00  Icon User is offline

  • No idea why my code works
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,596
  • Joined: 25-December 08

Re: Calculate Power of a number

Posted 22 January 2010 - 12:47 PM

Please fix your post with [code] [ /code] tags :)
Was This Post Helpful? 0
  • +
  • -

#3 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2652
  • View blog
  • Posts: 11,189
  • Joined: 20-September 08

Re: Calculate Power of a number

Posted 22 January 2010 - 12:48 PM

Quote

It just counts the number of numbers and subtracts 1 from it.


There's a great deal of code there to do just that, which would be

int pow = input.length() - 1;



I don't understand your code
Was This Post Helpful? 0
  • +
  • -

#4 SwiftStriker00  Icon User is offline

  • No idea why my code works
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,596
  • Joined: 25-December 08

Re: Calculate Power of a number

Posted 22 January 2010 - 12:49 PM

I would just get rid of your println in your loop, and put one at the end before your system.exit call
Was This Post Helpful? 0
  • +
  • -

#5 fwang321  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 22-January 10

Re: Calculate Power of a number

Posted 22 January 2010 - 12:53 PM

View PostSwiftStriker00, on 22 Jan, 2010 - 11:49 AM, said:

I would just get rid of your println in your loop, and put one at the end before your system.exit call

DAYUM that did it! thank you
Was This Post Helpful? 0
  • +
  • -

#6 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2652
  • View blog
  • Posts: 11,189
  • Joined: 20-September 08

Re: Calculate Power of a number

Posted 22 January 2010 - 01:06 PM

fwang321, please post your current, working code for the benefit of future visitors
Was This Post Helpful? 0
  • +
  • -

#7 fwang321  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 22-January 10

Re: Calculate Power of a number

Posted 22 January 2010 - 01:28 PM

View Postg00se, on 22 Jan, 2010 - 12:06 PM, said:

fwang321, please post your current, working code for the benefit of future visitors

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

		Scanner scan = new Scanner( System.in );
		int i = 0;
		System.out.println ("Give me a Number");
	  
	String phrase = scan.nextLine();   

	for (i = 0; i < 3; i++){

	phrase = phrase.toLowerCase();

	int count = 0;
	for (i = 0; i < phrase.length(); i++) {
	char c = phrase.charAt(i);
	if (c=='1' || c=='2' || c=='3' || c=='4' || c=='5' || c=='6' || c=='7' || c=='8' || c=='9' || c=='0') {
	count++;
	}
	}
	if (count == 2)
	{System.out.println( "The number is to the " + (count - 1) + "st " + "power");} 
	if (count == 3)
	{System.out.println( "The number is to the " + (count - 1) + "nd " + "power");}
	if (count == 4)
	{System.out.println( "The number is to the " + (count - 1) + "rd " + "power");}
	if (count > 4)
	{System.out.println( "The number is to the " + (count - 1) + "th " + "power");}	
	else 
		System.out.println( phrase + " includes a letter or symbol,\n symbols or letters do not have powers");
	
	System.exit(0);
		}
	}
}


Was This Post Helpful? 0
  • +
  • -

#8 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2652
  • View blog
  • Posts: 11,189
  • Joined: 20-September 08

Re: Calculate Power of a number

Posted 22 January 2010 - 01:50 PM

OK. Firstly, the significant change you've made to your original is nothing to do with the final println - it's to do with where the inner for loop ends.

In fact your println about the input not being a number still prints, irrespective of input
Was This Post Helpful? 0
  • +
  • -

#9 fwang321  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 22-January 10

Re: Calculate Power of a number

Posted 22 January 2010 - 02:27 PM

View Postg00se, on 22 Jan, 2010 - 12:50 PM, said:

OK. Firstly, the significant change you've made to your original is nothing to do with the final println - it's to do with where the inner for loop ends.

In fact your println about the input not being a number still prints, irrespective of input

yeah i just realized it failed once again. I dun think i saved the right one
Was This Post Helpful? 0
  • +
  • -

#10 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2652
  • View blog
  • Posts: 11,189
  • Joined: 20-September 08

Re: Calculate Power of a number

Posted 22 January 2010 - 02:42 PM

Get rid of the outer loop. Test for improper input using Integer.parseInt - if it's not a number you'll get an exception
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1