Fires of Heaven Guild Message Board  

Go Back   Fires of Heaven Guild Message Board > General forums > Development
User Name
Password
ForumSpy Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Old 09-28-2007, 08:04 PM   #31 (permalink)
Neph
Late to the party
 
Join Date: Nov 2006
Location: Maryland
Posts: 644
+0 Internets
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.
Neph is offline   Reply With Quote
Old 09-28-2007, 08:10 PM   #32 (permalink)
Kuriin
Registered User
 
Kuriin's Avatar
 
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 !
Kuriin is offline   Reply With Quote
Old 09-28-2007, 08:12 PM   #33 (permalink)
Neph
Late to the party
 
Join Date: Nov 2006
Location: Maryland
Posts: 644
+0 Internets
Quote:
Originally Posted by Kuriin View Post
edit 2: Okay, I just ran this program and it works. But, something is wrong. The spacing is a bit iffy at the end of my sentences. There is no spacing in between equal and the final result of that operation. I'm not sure what's going on or how to fix it. Do I need to start a new line?
No need to start a new line, just put a space inbetween equal and "

Code:
System.out.println("When added, these two integers equal " + Ti);
Neph is offline   Reply With Quote
Old 09-28-2007, 08:13 PM   #34 (permalink)
Kuriin
Registered User
 
Kuriin's Avatar
 
Join Date: Aug 2006
Location: Raleigh, NC
Posts: 1,777
Quote:
Originally Posted by Neph View Post
No need to start a new line, just put a space inbetween equal and "

Code:
System.out.println("When added, these two integers equal " + Ti);
Oh! Gracias!
__________________
VOCA ME BENEDICTUM !
SANA MEAM ANIMAM !
Kuriin is offline   Reply With Quote
Old 09-28-2007, 08:21 PM   #35 (permalink)
Neph
Late to the party
 
Join Date: Nov 2006
Location: Maryland
Posts: 644
+0 Internets
Quote:
Originally Posted by Kuriin View Post
# 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);
To input a string just use: keyboard.nextString()
To output its length it'd be like:

Code:
str1 = keyboard.nextString(); str1.length();
To output the character at an index, use charAt(i)
Code:
str1.charAt(i)
gives you the value at the index "i"

And to output the first occurrence of a char in str1, use indexOf(char)
Code:
str1.indexOf(char)
gives you the index of first occurrence of the character "char"
Neph is offline   Reply With Quote
Old 09-28-2007, 08:26 PM   #36 (permalink)
Neph
Late to the party
 
Join Date: Nov 2006
Location: Maryland
Posts: 644
+0 Internets
Quote:
Originally Posted by Kuriin View Post
# 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.
Code:
date = keyboard.nextString(); eurodate = date.substring(3, 4) + "-" + date.substring(0, 1) + "-" + date.substring(6, 9);
My java's kinda hazy, but I'm fairly sure that this will work. Substring cuts up the string you give it at the different indeces you supply it. So date.substring(3, 4) evaluates to the string "dd".
Neph is offline   Reply With Quote
Old 09-28-2007, 08:27 PM   #37 (permalink)
Kuriin
Registered User
 
Kuriin's Avatar
 
Join Date: Aug 2006
Location: Raleigh, NC
Posts: 1,777
Thanks, Neph. Will give this a shot.
__________________
VOCA ME BENEDICTUM !
SANA MEAM ANIMAM !
Kuriin is offline   Reply With Quote
Old 09-28-2007, 08:28 PM   #38 (permalink)
Neph
Late to the party
 
Join Date: Nov 2006
Location: Maryland
Posts: 644
+0 Internets
Quote:
Originally Posted by Kuriin View Post
Thanks, Neph. Will give this a shot.
Not a problem! It's always more fun to do someone else's work rather than your own...(I have my own MP due on sunday night )
Neph is offline   Reply With Quote
Old 09-28-2007, 08:34 PM   #39 (permalink)
Kuriin
Registered User
 
Kuriin's Avatar
 
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 !
Kuriin is offline   Reply With Quote
Old 09-28-2007, 08:36 PM   #40 (permalink)
Kuriin
Registered User
 
Kuriin's Avatar
 
Join Date: Aug 2006
Location: Raleigh, NC
Posts: 1,777
man this string shit is confusing. ;[ lol
__________________
VOCA ME BENEDICTUM !
SANA MEAM ANIMAM !
Kuriin is offline   Reply With Quote
Old 09-28-2007, 08:41 PM   #41 (permalink)
Kuriin
Registered User
 
Kuriin's Avatar
 
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.
Kuriin is offline   Reply With Quote
Old 09-29-2007, 01:43 AM   #42 (permalink)
slitz
euro scum
 
slitz's Avatar
 
Join Date: Aug 2002
Location: Sweden
Posts: 785
-6 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
Old 10-04-2007, 09:37 AM   #43 (permalink)
Murr
Token Gnome
 
Murr's Avatar
 
Join Date: Jan 2002
Location: Chapel Hill, NC
Posts: 1,752
+7 Internets
Send a message via AIM to Murr
Doesn't java have string.split? This might not compile but a general flow and alternative to hardcoded substring ops.:
Code:
String inDate, outDate; String[] dateSplit; // holds split string inDate = keyboard.nextString(); dateSplit = inDate.split("/"); // splits the string on "/" character outDate = dateSplit[1] + "-" + dateSplit[0] + "-" + dateSplit[2]; System.out.println(outDate);
Split divides a string up into segments based on the character you "split" on and puts them into an array, so if you pass it
"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
Murr is offline   Reply With Quote
Old 10-04-2007, 11:36 PM   #44 (permalink)
slitz
euro scum
 
slitz's Avatar
 
Join Date: Aug 2002
Location: Sweden
Posts: 785
-6 Internets
Quote:
Originally Posted by Murr View Post
Doesn't java have string.split? This might not compile but a general flow and alternative to hardcoded substring ops.:
jupp, java has split and it would be totally correct to do it that way as well
slitz is offline   Reply With Quote
Old 10-06-2007, 10:07 AM   #45 (permalink)
Kuriin
Registered User
 
Kuriin's Avatar
 
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 !
Kuriin is offline   Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On
uberguilds network



All times are GMT -7. The time now is 10:52 AM.


Powered by vBulletin® Version 3.6.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.0.0 RC6