Results 1 to 3 of 3
  1. #1
    Aivy is offline Member
    Join Date
    Nov 2011
    Posts
    3
    Rep Power
    0

    Default what is the use of char type=in.nextLine().toUpperCase().toUpperCaseArray( )[0]; ?

    need your help please...
    what is the use of char type=in.nextLine().toUpperCase().toUpperCaseArray( )[0]; in this program?
    Java Code:
    import java.util.Scanner;
    public class weight1 {
    public static void main(String[] args){
    Scanner in = new Scanner(System.in);
    System.out.println("Enter pounds or ounce as your original unit of weight (P/O): ");
    char type = in.nextLine().toUpperCase().toCharArray()[0];
    System.out.println("Enter your weight: ");
    double weight = in.nextDouble();
    switch(type){
    case 'P':
    System.out.println(weight + " pounds is " + (weight * 0.453592) + " in kilograms and " + ( weight * 453.59232)+ " in grams. ");
    break;
    case 'O':
    System.out.println(weight + " ounce is " + (weight  * 0.02835) + " in kilograms and " + ( weight * 28.34952) +  " in grams. ");
    break;
    }
    }
    }
    Last edited by Norm; 11-26-2011 at 02:20 PM. Reason: added code tags

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,090
    Rep Power
    29

    Default Re: what is the use of char type=in.nextLine().toUpperCase().toUpperCaseArray( )[0];

    Separate the compound statement into separate single statements if you have a hard time seeing what that statement is doing.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,312
    Blog Entries
    7
    Rep Power
    24

    Default Re: what is the use of char type=in.nextLine().toUpperCase().toUpperCaseArray( )[0];

    That code is a bit silly for a couple of reasons:

    1) if the input line was empty the size of the array will be zero and an ArrayOutOfBoundsException will be thrown;
    2) there is no need to convert an entire String to an array and fetch the first char from it; it could've been fetched from the String direcly.

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

Similar Threads

  1. Recreating toUpperCase method
    By theoneroo in forum New To Java
    Replies: 1
    Last Post: 01-28-2011, 06:59 AM
  2. Replies: 1
    Last Post: 07-27-2010, 06:01 PM
  3. toUpperCase() method
    By arson09 in forum New To Java
    Replies: 15
    Last Post: 05-03-2010, 04:18 AM
  4. Replies: 2
    Last Post: 02-24-2009, 05:32 PM
  5. Char type for first character typed?
    By Sageinquisitor in forum New To Java
    Replies: 3
    Last Post: 07-17-2007, 07:08 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
  •