SilabGarza - RS Private Server  
Home Forum Play Register Store Market Highscores Mark Forums Read
Go Back   SilabGarza - RS Private Server > SilGar Media > Computers > Programming
Reload this Page What's Wrong?


What's Wrong?
Old
  (#1 (permalink))
Revenge2nite is Offline
<span style="color: hotpink">Boyjam2</span>
image
 
Revenge2nite's Avatar
 
Posts: 1,059
Cookies Given: 154
Cookies Received: 25
Time Online: 9 d 18 h N/A
Clan: Imperium - PvM
   
Default What's Wrong? - 09-28-2013, 08:38 AM

What's wrong with this code
@Gir

Spoiler


^Fixed the error but now I got a new error my " " were wrong LOL


Code:
import java.io.*;  //this needs to be at the top of your program, right below the “packages”
/**
   * @author: N/A
   *  @Date: 2013-09-27
   *  @filename: ICS3UJavaAssignment2b.java
   *  @Description: Prompts for radius of a circle then calculates and outputs the circumference
   */

public class ICS3UJavaAssignment2b
{
  public static void main (String[] args) throws Exception {
    {
      BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));
      String r; //creates a string variable to get radius from user
      System.out.println("Enter radius of circle: ");  //gets radius from user
      r = buffer.readLine(); //reads a line from the console
      
      System.out.println("The circumference of a circle with a radius of " + r + " is " + (2 * 3.14 * r));
    }
  }
}
I need to be able to compile and execute this program that asks the user to type in a radius then the program calculates it and give you the circumference of the circle...

HELP! it gives me a error here:

Code:
System.out.println("The circumference of a circle with a radius of " + r + " is " + (2 * 3.14 * r));
Error:
Code:
1 error found:
File: F:\School - Grade 11\Computer Programming\Java Excercises\ICS3UJavaAssignment2b.java  [line: 18]
Error: The operator * is undefined for the argument type(s) double, java.lang.String




Cycles Always Repeat.

~Joshy~BOB~The Kai~Brad~Jεssε~


Last edited by Revenge2nite; 09-28-2013 at 08:48 AM.
  
Old
  (#2 (permalink))
N0vlafye is Offline
Senior Member
image
 
N0vlafye's Avatar
 
Posts: 2,442
Cookies Given: 235
Cookies Received: 451
Time Online: 108 d 1 h 31 m
Clan: Imperium, WIN
   
Default 09-28-2013, 09:02 AM

Before you open this spoiler, you MUST agree that you will understand what you did wrong, not just copy the code

Your primary problem is the line
Code:
 System.out.println("The circumference of a circle with a radius of " + r + " is " + (2 * 3.14 * r));
The variable r is a STRING. You can't multiply a string. First, you must parse it to a double. I finished it up for you and added a failsafe to prevent the code from breaking if the user enters something wrong.

Scanner would be a more elegant choice than BufferedReader here. If you know how to use it, perhaps you should use Scanner instead.

Spoiler


Note: if you do copy+paste the code to your IDE, you may need to change the quotation marks. vBulletin has a habit of changing ASCII (regular characters) to Unicode (special characters) that JAVA cannot read.

Last edited by N0vlafye; 09-28-2013 at 09:05 AM.
  
The Following User Says Thank You to N0vlafye For This Useful Post:
Revenge2nite (09-28-2013)
Old
  (#3 (permalink))
Revenge2nite is Offline
<span style="color: hotpink">Boyjam2</span>
image
 
Revenge2nite's Avatar
 
Posts: 1,059
Cookies Given: 154
Cookies Received: 25
Time Online: 9 d 18 h N/A
Clan: Imperium - PvM
   
Default 09-28-2013, 09:09 AM

Quote:
Originally Posted by N0vlafye View Post
Before you open this spoiler, you MUST agree that you will understand what you did wrong, not just copy the code

Your primary problem is the line
Code:
 System.out.println("The circumference of a circle with a radius of " + r + " is " + (2 * 3.14 * r));
The variable r is a STRING. You can't multiply a string. First, you must parse it to a double. I finished it up for you and added a failsafe to prevent the code from breaking if the user enters something wrong.

Scanner would be a more elegant choice than BufferedReader here. If you know how to use it, perhaps you should use Scanner instead.

Spoiler


Note: if you do copy+paste the code to your IDE, you may need to change the quotation marks. vBulletin has a habit of changing ASCII (regular characters) to Unicode (special characters) that JAVA cannot read.
It would be stupid of me to just copy the code because when we have the test teachers gonna be like then how'd you do this ... 1 more thing you wanna make it more grade 11 friendly? by like getting rid of the failsafe and all the extra stuff make it less confusing it's only like what 4 week of class we started java like last week I can't understand most of this... and yeah I did know the problem was that I was multiplying the string but I couldn't solve that.



Cycles Always Repeat.

~Joshy~BOB~The Kai~Brad~Jεssε~

  
Old
  (#4 (permalink))
N0vlafye is Offline
Senior Member
image
 
N0vlafye's Avatar
 
Posts: 2,442
Cookies Given: 235
Cookies Received: 451
Time Online: 108 d 1 h 31 m
Clan: Imperium, WIN
   
Default 09-28-2013, 09:15 AM

I am in 11th grade >.>

Code:
public class ICS3UJavaAssignment2b
{
  public static void main (String[] args) throws Exception {
    {
      BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));
      String r; //creates a string variable to get radius from user
      double radius = 0.0;//the double value for the radius
     // boolean badinput = true;// a boolean to check that the input is valid
     // do{
		System.out.print("Enter radius of circle: ");  //gets radius from user
		r = buffer.readLine(); //reads a line from the console
		//try{
			radius = Double.parseDouble(r);//extract a double value from the string
		//	badinput = false;//if you are able to get the double value, change the boolean to break the loop
		//}
	//	catch (NumberFormatException e)
	//	{
		//	System.out.println("Please enter a number.");//if you can't convert the string to a double, prompts the user to input a new number
		//}
	//}while(badinput);
      System.out.println("The circumference of a circle with a radius of " + r + " is " + (2 * 3.14 * radius));//calculates the circumference using the double value
    }
  }
}
Delete the commented lines for a simpler (though less fail-safe) version of it.
  
The Following User Says Thank You to N0vlafye For This Useful Post:
Revenge2nite (09-28-2013)
Old
  (#5 (permalink))
Revenge2nite is Offline
<span style="color: hotpink">Boyjam2</span>
image
 
Revenge2nite's Avatar
 
Posts: 1,059
Cookies Given: 154
Cookies Received: 25
Time Online: 9 d 18 h N/A
Clan: Imperium - PvM
   
Default 09-28-2013, 09:18 AM

Thank you so much

/will post to close or not in a while (trying it out)

Edit: works, thanks again.

/keep open (more assignments to do)



Cycles Always Repeat.

~Joshy~BOB~The Kai~Brad~Jεssε~


Last edited by Revenge2nite; 09-28-2013 at 09:28 AM.
  
Old
  (#6 (permalink))
Revenge2nite is Offline
<span style="color: hotpink">Boyjam2</span>
image
 
Revenge2nite's Avatar
 
Posts: 1,059
Cookies Given: 154
Cookies Received: 25
Time Online: 9 d 18 h N/A
Clan: Imperium - PvM
   
Default 09-28-2013, 11:39 AM

Spoiler


^SOLVED



Cycles Always Repeat.

~Joshy~BOB~The Kai~Brad~Jεssε~


Last edited by Revenge2nite; 09-28-2013 at 12:28 PM.
  
adsense code2.3
Closed Thread

Bookmarks

What's Wrong?

Programming




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
adsense2.6
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -7. The time now is 11:59 PM.





Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2017, vBulletin Solutions, Inc.