Results 1 to 3 of 3

Thread: Two problems

  1. #1
    frobiz176 is offline Member
    Join Date
    Sep 2013
    Posts
    1
    Rep Power
    0

    Default Two problems

    Hello all,

    I have two problems when trying to code. The first question, I have no idea how to do at all, and the second question I only need to know how to prompt the user to insert a letter (either C or F).

    Thank you.


    Question #1. Write a program that prompts the user to enter a number and prints out
    the sum of all the numbers in an even positions. The number 52934 has a 4 in position
    1, 3 in position 2, 9 in position 3, 2 in position 4, and 5 in position 5. So the numbers in
    even positions are 3 (in the position 2) and 2 (in the position 4). Save your program in
    NumberSums.java.
    Here are example runs:
    Enter a number: 52934
    The sum of all even positions is 5
    Enter a number: 1
    The sum of all even positions is 0

    Question #2. Write a program that prompts the user to enter outside temperature and
    whether it is in Celsius or Fahrenheit. If the user enters the temperature in Celsius, then
    display the temperature in Fahrenheit and if the user enters the temperature in
    Fahrenheit, then display the temperature in Celsius. If the user enters an invalid entry,
    print “Invalid entry”. Save your program in TempConversion.java.
    The formulas for the conversion are as follows
    Celsius to Fahrenheit: (C is the temperature in Celsius) °C x 9/5 + 32 = °F
    Fahrenheit to Celsius: (F is the temperature in Fahrenheit) (°F - 32) x 5/9 = °C
    Here are two example runs.
    Enter the current outside temperature: 37
    Celsius or Fahrenheit (C or F): C
    The current temperature in Fahrenheit is: 98.6
    Enter the current outside temperature: 98.6
    Celsius or Fahrenheit (C or F): F
    The current temperature in Celsius is: 37

  2. #2
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    CA, USA
    Posts
    264
    Rep Power
    6

    Default Re: Two problems

    For getting input from the console, you may find what you're looking for by looking up how to use a Scanner with System.in in Java. I'll also provide an example below.

    Java Code:
    public static void main(String args[])
    {
    	// create a new Scanner object, which will read input from the console (aka: System.in)
    	final Scanner in = new Scanner(System.in);
    	
    	// Scanner has some nice methods which allow you to read different
    	// types of input very easily. Here are a couple examples;
    	
    	// Ask for and obtain an int from the console
    	System.out.print("Enter an integer: ");
    	int someInt = in.nextInt();
    	System.out.println();
    	
    	// Ask for and obtain a double from the console
    	System.out.print("Enter a double: ");
    	double someDouble = in.nextDouble();
    	System.out.println();
    	
    	// close the Scanner to free up its resources
    	in.close();
    }

  3. #3
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Two problems

    I recommend browsing the javadoc for the standard API to get an idea of what's in there. Then even if you don't remember the exact method names, you know what's in there and you can look it up.

    Here are a couple methods to get you started on the first question:

    String (Java Platform SE 6)
    Integer (Java Platform SE 6)
    Get in the habit of using standard Java naming conventions!

Similar Threads

  1. InputStream/Jar Problems/File IO Problems
    By rdjava in forum Advanced Java
    Replies: 31
    Last Post: 01-17-2011, 12:12 PM
  2. JAR problems
    By Evil Smurf in forum New To Java
    Replies: 4
    Last Post: 09-01-2009, 02:17 AM
  3. Few Problems Help!
    By elad_bj in forum New To Java
    Replies: 4
    Last Post: 04-07-2009, 04:45 AM
  4. Problems in JSP : Need help
    By raj4u in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 02-07-2008, 11:06 AM
  5. problems with JPA
    By Ed in forum New To Java
    Replies: 2
    Last Post: 07-04-2007, 06:34 AM

Posting Permissions

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