Results 1 to 3 of 3
- 11-26-2011, 01:01 PM #1
Member
- Join Date
- Nov 2011
- Posts
- 3
- Rep Power
- 0
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 01:20 PM. Reason: added code tags
- 11-26-2011, 01:22 PM #2
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.
- 11-26-2011, 02:01 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
Recreating toUpperCase method
By theoneroo in forum New To JavaReplies: 1Last Post: 01-28-2011, 05:59 AM -
Incompatible type for method. Can't convert java.lang.String to char.
By renu in forum New To JavaReplies: 1Last Post: 07-27-2010, 06:01 PM -
toUpperCase() method
By arson09 in forum New To JavaReplies: 15Last Post: 05-03-2010, 04:18 AM -
Problem using the toUpperCase method with character array...
By lisalala in forum New To JavaReplies: 2Last Post: 02-24-2009, 04:32 PM -
Char type for first character typed?
By Sageinquisitor in forum New To JavaReplies: 3Last Post: 07-17-2007, 07:08 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks