|
| | #31 (permalink) |
| Late to the party Join Date: Nov 2006 Location: Maryland
Posts: 644
| I haven't really read too deeply into your code yet, but one thing you might want to do is rename your variables to something you might remember better. int1, int2, intsum, intdiff, ..., doub1, doub2, doubsum, doubdiff, etc. |
| | |
| | #32 (permalink) |
| Registered User Join Date: Aug 2006 Location: Raleigh, NC
Posts: 1,777
| That's what I was thinking and right now, it is pretty easy to remember. Fourth, Fifth, etc. I'm really not too concerned with the variables themselves (even though I will pay close attention to them later). Can you help with the last part? :] edit: And also, for these types of problems, I tend to make the most important variables the first one or two so I always remember where they are.
__________________ VOCA ME BENEDICTUM ! SANA MEAM ANIMAM ! |
| | |
| | #33 (permalink) | |
| Late to the party Join Date: Nov 2006 Location: Maryland
Posts: 644
| Quote:
Code:
| |
| | |
| | #35 (permalink) | |
| Late to the party Join Date: Nov 2006 Location: Maryland
Posts: 644
| Quote:
To output its length it'd be like: Code:
Code:
And to output the first occurrence of a char in str1, use indexOf(char) Code:
| |
| | |
| | #36 (permalink) | |
| Late to the party Join Date: Nov 2006 Location: Maryland
Posts: 644
| Quote:
Code:
| |
| | |
| | #39 (permalink) |
| Registered User Join Date: Aug 2006 Location: Raleigh, NC
Posts: 1,777
| Well, I learn a lot when people help me as I can look at their code and use it for future reference. keyboard.nextString(); str1 = keyboard.nextString(); str1.length(); str1.charAt(i); str1.indexOf(char); date = keyboard.nextString(); eurodate = date.substring(3, 4) + "-" + date.substring(0, 1) + "-" + date.substring(6, 9); As you can tell, I'm still sort of confused. And, where did you get the numbers for 3,4, 6,9, and 0,1?
__________________ VOCA ME BENEDICTUM ! SANA MEAM ANIMAM ! |
| | |
| | #41 (permalink) |
| Registered User Join Date: Aug 2006 Location: Raleigh, NC
Posts: 1,777
| String name; System.out.println("What's your favorite movie?"); name = keyboard.nextString(); I'm gonna try this way, then eventually lead to char and length. edit: k, gonna worry abotu this tomorrow I think. I'm too tired, lol. :|
__________________ VOCA ME BENEDICTUM ! SANA MEAM ANIMAM ! Last edited by Kuriin : 09-28-2007 at 08:48 PM. |
| | |
| | #42 (permalink) | ||
| euro scum Join Date: Aug 2002 Location: Sweden
Posts: 785
| 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:
Code:
Quote:
Code:
| ||
| | |
| | #43 (permalink) |
| Token Gnome Join Date: Jan 2002 Location: Chapel Hill, NC
Posts: 1,752
+7 Internets | Doesn't java have string.split? This might not compile but a general flow and alternative to hardcoded substring ops.: Code:
"09/11/2001" and split on "/" you get an array with elements [09,11,2001] So when I say dateSplit=inDate.split("/") dateSplit[0]="09", dateSplit[1]="11", and dateSplit[2]="2001" Now you can just recombine the separate strings as shown and you get 11-09-2001 |
| | |
| | #45 (permalink) |
| Registered User Join Date: Aug 2006 Location: Raleigh, NC
Posts: 1,777
| Having trouble yet again. :| We quickly went over booleans and barely went over our lab work and homework. This is what I have thus far. Not sure if it's right or anything. The program asks the user to enter a string and inputs one. Then it looks at the string and prints a message stating whether it is the empty string, a word, a number, or something else. For this lab, a string is considered to be a word if it starts and ends with a letter; it is considered to be a number if it starts and ends with a digit; and something else if it is not empty and it is not a word and it is not a number. For example, here are several sample runs of the program you will write (user inputs are in bold): * Enter a string: Table "Table" is a word * Enter a string: 2abl3 "2abl3" is a number * Enter a string: Tabl7 "Tabl7" is something else * Enter a string: Empty string Checking if a character is a letter or a digit To check whether a character is a letter or a digit, you can use the following methods: * boolean Character.isLetter(char c) returns true if and only if c is a letter (lower case or upper case), * boolean Character.isDigit(char c) returns true if and only if c is a digit (0-9). import java.util.Scanner; public class Lab3Part1 { public static void main(String[] args) { keyboard = new Scanner(System.in); String stringOne; System.out.print("Please enter a string."); stringOne = keyboard.nextString(); if ( stringOne == word ) { System.out.println("Your string is a word."); } else ( stringOne = digit ) { System.out.println("Your string is a digit."); } boolean StringOne.isLetter(char ); boolean StringOne.isDigit(char ); } }
__________________ VOCA ME BENEDICTUM ! SANA MEAM ANIMAM ! |
| | |
![]() |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
| |