View Single Post
Old 09-29-2007, 02:43 AM   #42 (permalink)
slitz
euro scum
 
slitz's Avatar
 
Join Date: Aug 2002
Location: Sweden
Posts: 819
-9 Internets
Btw, never start variables with a upper case unless it's a final variable. It's also a good thing if your variables makes sense for the program or it's very hard for someone to read your code.
Classes, final variables (and thus constants) and enums can all start with upper case, constants normally use upper cases on the whole name.
Anyhoo, to the problem.
Quote:
this is the part that I am stuck on:

# Input a text string, output its length; input an index within the string, and output the character at that position in the string; input another text string, and output the location of the first occurrence of the second string in the first one (if such location exists, otherwise output -1);
Code:
Scanner scanner = new Scanner(); System.out.println("Enter your first string: "); String firstString = scanner.next(); // this is the same as your nextString() just that nextString() is old API. System.out.println("Enter index of the character: "); int firstIndex = scanner.nextInt(); System.out.println("The character at position " +firstIndex + "in the String " +firstString + " is " +firstString.charAt(firstIndex)); System.out.println("Enter your second String: "); String secondString = scanner.next(); System.out.println("The first occurance of " +secondString +" in " +firstString + " is at index " +firstString.indexOf(secondString)); // firstString.indexOf(secondString); will automatically return -1 if it doesn't exist so you don't have to put up a statement to check that.
Quote:
# Finally, input a date (a text string) in the format mm/dd/yyyy (i.e., two digits for the month, followed by a '/', followed by two digits for the day, followed by a '/', followed by four digits for the year), and output it in the common European format dd-mm-yyyy (i.e., two digits for the day, followed by a '-', followed by two digits for the month, followed by a '-', followed by four digits for the year.
Ok so you were actually using Strings to setup a date, my bad 8)

Code:
String date = scanner.next(); // String month = date.substring(0,2); String day = date.substring(3,5); String year = date.substring(6); System.out.println(day + "-" +month+ "-" +year);
slitz is offline   Reply With Quote

 
Uberguilds Network