CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Hybrid View

  1. #1
    Join Date
    Jun 2013
    Posts
    4

    How to add strings to this Code?

    srgrtshnbra
    Last edited by harman; June 10th, 2013 at 09:48 AM.

  2. #2
    Join Date
    Jun 2013
    Location
    New York, USA
    Posts
    21

    Re: How to add strings to this Code?

    Code:
    import java.util.*;
    
    public class FinalProject {
    
      static Scanner cm = new Scanner(System.in); // accessible to all methods
    
      public static void main (String args[]){
      // 2.54cm is 1 inch
      System.out.println("Enter the CM: "); // Write input
      String input = cm.next();
    
      if (input == "b" || input == "B") {
        getDegree();
    }
    else {
    
      //double
      double centimeters = cm.nextDouble();
      double inches = centimeters/2.54;
      System.out.println(inches + " Inch Is " + centimeters + " centimeters");
    }
    
    
    }
      public void getDegree() {
      double celsius=0, fahrenheit=0;
    
      System.out.println("Enter the amount of Fahrenheit to be converted: ");
      fahrenheit = cm.nextDouble();
    
      celsius = (fahrenheit-32)*5/9;
    
      System.out.println("The entered amount of Fahrenheit is equal to " + celsius + " degrees Celsius.");
    
    
    
    }
    }
    this should work all in one class. Let me know if it doesn't, I just threw in an if else statement

  3. #3
    Join Date
    Jun 2013
    Posts
    4

    Re: How to add strings to this Code?

    Thanks for replying but it is showing me this error > Name:  Untitled.jpg
Views: 1002
Size:  86.6 KB

  4. #4
    Join Date
    Jun 2013
    Location
    New York, USA
    Posts
    21

    Re: How to add strings to this Code?

    I didn't put and if statement for the cm converter I just made it default if they don't type in B or b that it goes to the cm converter.

  5. #5
    Join Date
    Jun 2013
    Location
    New York, USA
    Posts
    21

    Re: How to add strings to this Code?

    Try making it public static void getDegree()

  6. #6
    Join Date
    Jun 2013
    Posts
    4

    Re: How to add strings to this Code?

    no still showing error
    Last edited by harman; June 10th, 2013 at 09:46 AM.

  7. #7
    Join Date
    Jun 2013
    Posts
    4

    Re: How to add strings to this Code?

    it is showing this error
    Attached Images Attached Images  
    Last edited by harman; June 10th, 2013 at 09:46 AM.

  8. #8
    Join Date
    Jun 2013
    Location
    New York, USA
    Posts
    21

    Re: How to add strings to this Code?

    you have to change public void getDegree() to public static void getDegree().

    Code:
    import java.util.*;
    
    public class FinalProject {
    	
    	  static Scanner cm = new Scanner(System.in); // accessible to all methods
    	
    	  public static void main (String args[]){
    	  // 2.54cm is 1 inch
    	  System.out.println("Enter the CM: "); // Write input
    	  String input = cm.next();
    	
    	  if (input == "b" || input == "B") {
    	    getDegree();
    	}
    	else {
    	
    	  //double
    	  double centimeters = cm.nextDouble();
    	  double inches = centimeters/2.54;
    	  System.out.println(inches + " Inch Is " + centimeters + " centimeters");
    	}
    	
    	
    	}
    	  public static void getDegree() {
    	  double celsius=0, fahrenheit=0;
    	
    	  System.out.println("Enter the amount of Fahrenheit to be converted: ");
    	  fahrenheit = cm.nextDouble();
    	
    	  celsius = (fahrenheit-32)*5/9;
    	
    	  System.out.println("The entered amount of Fahrenheit is equal to " + celsius + " degrees Celsius.");
    	
    	
    	
    	}
    }

  9. #9
    Join Date
    May 2009
    Location
    Lincs, UK
    Posts
    298

    Re: How to add strings to this Code?

    When comparing objects (Strings in particular) == tests for identity (ie, if the references compared refer to the same object); to test equality use the equals method. In this case you can also use equalsIgnoreCase:
    Code:
    if ("b".equalsIgnoreCase(input)) {

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured