Author |
Message |
|
todlangweilig
Advanced Member
Joined: 14 Feb 2006 Posts: 470
|
Posted: 29 Jan 2007 11:54:53 am Post subject: |
|
|
Code: Scanner scan = new Scanner(System.in);
scan.useDelimiter(".");
String priceDollars, priceCents;
String paymentDollars, paymentCents;
System.out.println("Enter price of item in dollars:");
priceDollars = scan.next();
priceCents = scan.next();
System.out.print(priceDollars +"/"+ priceCents);
priceDollars and priceCents both contain empty strings as far as I can tell. I really don't know what is wrong with this, there is an example like what I have written in my Java textbook.
Edit: My teacher doesn't know what is wrong with it either.
Last edited by Guest on 29 Jan 2007 11:55:26 am; edited 1 time in total |
|
Back to top |
|
|
JoeImp Enlightened
Active Member
Joined: 24 May 2003 Posts: 747
|
|
Back to top |
|
|
Arcane Wizard `semi-hippie`
Super Elite (Last Title)
Joined: 02 Jun 2003 Posts: 8993
|
Posted: 29 Jan 2007 12:38:24 pm Post subject: |
|
|
From what I understand of the purpose of your program (I'm no mind reader and you didn't supply any information regarding this) I think you should try escaping the dot character so that it matches a dot in the input instead of all characters.
scan.useDelimiter("\.");
However, since the code looks valid (you didn't provide full source and javac is somehow missing from my java installation on my laptop) I can't tell what's wrong with it unless you state in what way it's operating incorrectly.
Quote: Screw scanner. Also that.
Last edited by Guest on 29 Jan 2007 12:40:04 pm; edited 1 time in total |
|
Back to top |
|
|
JoeImp Enlightened
Active Member
Joined: 24 May 2003 Posts: 747
|
Posted: 29 Jan 2007 01:46:06 pm Post subject: |
|
|
Yea, arcane had the right idea, except \. won't be allowed since it's an illegal escape character, so use \\. instead.
Code: public class Delimiters
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
scan.useDelimiter(Pattern.compile("\\."));
String priceDollars, priceCents;
String paymentDollars, paymentCents;
System.out.println("Enter price of item in dollars:");
priceDollars = scan.next();
priceCents = scan.next();
System.out.print(priceDollars +"/"+ priceCents);
}
}
Code: Enter price of item in dollars:
4.5.6
4/5
Makes no sense, but shows delimiting by periods.
[EDIT] - Just noticed I still had that Pattern.compile in there, you might be able to take that out and use \\. as just a string, I don't know. Don't have time to test it now, have to book to class. G'luck.
Last edited by Guest on 29 Jan 2007 01:47:03 pm; edited 1 time in total |
|
Back to top |
|
|
todlangweilig
Advanced Member
Joined: 14 Feb 2006 Posts: 470
|
Posted: 29 Jan 2007 01:53:09 pm Post subject: |
|
|
Code: import java.util.*;
public class Problem9 {
/**
* @param args
*/
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
scan.useDelimiter(".");
String priceDollars, priceCents;
String paymentDollars, paymentCents;
System.out.println("Enter price of item in dollars:");
priceDollars = scan.next();
priceCents = scan.next();
System.out.print(priceDollars +"/"+ priceCents);
/**
System.out.println("Enter payment of item in dollars:");
paymentDollars = scan.next();
paymentCents = scan.next();
System.out.println("priceDollars: " + priceDollars);
System.out.println("priceCents: " + priceCents);
System.out.println("paymentDollars: " + paymentDollars);
System.out.println("paymentCents: " + paymentCents);
int tmpPrice = 0;
int quarters = 0;
int dimes = 0;
int nickels = 0;
tmpPrice = 100 - price;
quarters = tmpPrice/25;
tmpPrice = tmpPrice%25;
dimes = tmpPrice/10;
tmpPrice = tmpPrice%10;
nickels = tmpPrice/5;
System.out.println("\nYou bought an item for " +
price + " cents and gave me a dollar,");
System.out.println("so your change is");
System.out.println(quarters + " quarters,");
System.out.println(dimes + " dimes,");
System.out.println(nickels + " nickel.");
**/
}
}
My appologies for not explaining what it supposed to do. I am writing a change-maker program. The user enters the cost of the item to be purchased, and the amount they are paying. Both numbers are to be entered "dollar.cents". I would like to input the numbers as strings, I will later convert them to integers. The code below " int tmpPrice = 0;" is not valid.
Lets say the item costs $3.45, we'd enter
3.45
And I would expect it to print back (this is just for test purposes)
3/45
but instead I get
/
All the options I tried below for delimiter failed:
scan.useDelimiter("\."); //Invalid escape sequence (valid ones are \b \t \n \f \r \" \' \\ )
scan.useDelimiter(\p{Punct}); //Syntax error on token ")", delete this token
Syntax error on tokens, EnumHeader expected instead
scan.useDelimiter("\p{Punct}"); //Invalid escape sequence
scan.useDelimiter("\p{Punct} Punctuation: One of !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~"); ////Invalid escape sequence (although, the quotes are not properly done. ie the ';' at the end of the line is highlighted as a string. )
Edit:
I got it working with JoeImp code. It does work fine with "//." Im running into a problem with needing two delimiters. Let me see if I can write some psuedo code to explain
String A, B, C, D;
Console:
"Text goes here"
1.2
"More text"
3.4
How can I make:
A = "1"
B = "2"
C = "3"
D = "4"
Would it just be easier to scrap the delimiter thing and break the strings apart later in the program?
Last edited by Guest on 29 Jan 2007 02:03:46 pm; edited 1 time in total |
|
Back to top |
|
|
JoeImp Enlightened
Active Member
Joined: 24 May 2003 Posts: 747
|
Posted: 29 Jan 2007 04:05:17 pm Post subject: |
|
|
Quote: scan.useDelimiter("\p{Punct}"); //Invalid escape sequence
scan.useDelimiter("\p{Punct} Punctuation: One of !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~"); ////Invalid escape sequence (although, the quotes are not properly done. ie the ';' at the end of the line is highlighted as a string. )
.....
..........
Wait, please don't tell me you tried to use lines from the documentation as legal java code. |
|
Back to top |
|
|
todlangweilig
Advanced Member
Joined: 14 Feb 2006 Posts: 470
|
Posted: 29 Jan 2007 05:03:33 pm Post subject: |
|
|
JoeImp wrote: Quote: scan.useDelimiter("\p{Punct}"); //Invalid escape sequence
scan.useDelimiter("\p{Punct} Punctuation: One of !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~"); ////Invalid escape sequence (although, the quotes are not properly done. ie the ';' at the end of the line is highlighted as a string. )
.....
..........
Wait, please don't tell me you tried to use lines from the documentation as legal java code.
[post="96039"]<{POST_SNAPBACK}>[/post]
Tried, knew it wasn't going to work. Well, the second one anyway. |
|
Back to top |
|
|
elfprince13 Retired
Super Elite (Last Title)
Joined: 11 Apr 2005 Posts: 3500
|
Posted: 29 Jan 2007 05:43:35 pm Post subject: |
|
|
JoeImp wrote:
agreed, Scanner sucks, its 1.5 and up only, screwing over anyone running Panther, or older unixes and its so easy to implement your own in older, more universally compliant java.
Arcane Wizard wrote: From what I understand of the purpose of your program (I'm no mind reader and you didn't supply any information regarding this) I think you should try escaping the dot character so that it matches a dot in the input instead of all characters.
scan.useDelimiter("\.");
However, since the code looks valid (you didn't provide full source and javac is somehow missing from my java installation on my laptop) I can't tell what's wrong with it unless you state in what way it's operating incorrectly.
Quote: Screw scanner. Also that.
[post="96036"]<{POST_SNAPBACK}>[/post]
you know that the JRE doesn't install the JDK by default, correct? |
|
Back to top |
|
|
Arcane Wizard `semi-hippie`
Super Elite (Last Title)
Joined: 02 Jun 2003 Posts: 8993
|
Posted: 29 Jan 2007 06:01:14 pm Post subject: |
|
|
Get a full string of input from a buffered input stream and use Pattern.split().
Quote: Arcane Wizard wrote: javac is somehow missing from my java installation on my laptop you know that the JRE doesn't install the JDK by default, correct? Yes. Both installed. No javac or any path variables. Meh.
Last edited by Guest on 29 Jan 2007 06:02:36 pm; edited 1 time in total |
|
Back to top |
|
|
elfprince13 Retired
Super Elite (Last Title)
Joined: 11 Apr 2005 Posts: 3500
|
Posted: 29 Jan 2007 11:07:44 pm Post subject: |
|
|
Arcane Wizard wrote: Get a full string of input from a buffered input stream and use Pattern.split().
Quote: Arcane Wizard wrote: javac is somehow missing from my java installation on my laptop you know that the JRE doesn't install the JDK by default, correct? Yes. Both installed. No javac or any path variables. Meh.
[post="96048"]<{POST_SNAPBACK}>[/post]
it never adds the path variables I always do it manually, (not trying to sound patronizing here but....) did you actually look for javac, or did you just type javac at the command prompt and wonder why it didn't work? |
|
Back to top |
|
|
Arcane Wizard `semi-hippie`
Super Elite (Last Title)
Joined: 02 Jun 2003 Posts: 8993
|
Posted: 30 Jan 2007 02:48:04 am Post subject: |
|
|
My laptop has had the path variables set for ages and Windows has only been reinstalled on it once, before advanced Java classes during which I used Window´s path vars extensively for all Java versions/flavors/servers I had installed. Now there is only one dir with Java stuff which holds a full jre but a seemingly (like half the files are missing) only partially installed/uninstalled jdk. I think I remember an update screwing up and I remember uninstalling reinstalling uninstalling reinstalling etc to no avail.
And yes my monitor was turned on when I checked these things.
Last edited by Guest on 30 Jan 2007 02:48:33 am; edited 1 time in total |
|
Back to top |
|
|
todlangweilig
Advanced Member
Joined: 14 Feb 2006 Posts: 470
|
Posted: 30 Jan 2007 04:45:14 am Post subject: |
|
|
Quote: Get a full string of input from a buffered input stream and use Pattern.split(). Is this something I can try in my program or is it something else? |
|
Back to top |
|
|
Arcane Wizard `semi-hippie`
Super Elite (Last Title)
Joined: 02 Jun 2003 Posts: 8993
|
Posted: 30 Jan 2007 07:29:25 am Post subject: |
|
|
?
If you can use Scanner you have 1.5. It's still in 1.5. So if you can use Scanner then you can use it.
Last edited by Guest on 30 Jan 2007 07:30:21 am; edited 1 time in total |
|
Back to top |
|
|
elfprince13 Retired
Super Elite (Last Title)
Joined: 11 Apr 2005 Posts: 3500
|
Posted: 30 Jan 2007 08:06:02 am Post subject: |
|
|
Arcane Wizard wrote: My laptop has had the path variables set for ages and Windows has only been reinstalled on it once, before advanced Java classes during which I used Window´s path vars extensively for all Java versions/flavors/servers I had installed. Now there is only one dir with Java stuff which holds a full jre but a seemingly (like half the files are missing) only partially installed/uninstalled jdk. I think I remember an update screwing up and I remember uninstalling reinstalling uninstalling reinstalling etc to no avail.
And yes my monitor was turned on when I checked these things.
*errhemm* ;)
todlangweilig wrote: Quote: Get a full string of input from a buffered input stream and use Pattern.split(). Is this something I can try in my program or is it something else?
you could indeed use it with Scanner, or any String for that matter, but I still recommend you learn to write your own terminal input functions/classes |
|
Back to top |
|
|
Arcane Wizard `semi-hippie`
Super Elite (Last Title)
Joined: 02 Jun 2003 Posts: 8993
|
Posted: 30 Jan 2007 09:38:16 am Post subject: |
|
|
elfprince13 wrote: Their site is protected against session hijacking. Point? |
|
Back to top |
|
|
todlangweilig
Advanced Member
Joined: 14 Feb 2006 Posts: 470
|
Posted: 30 Jan 2007 09:57:52 pm Post subject: |
|
|
Quote: you could indeed use it with Scanner, or any String for that matter, but I still recommend you learn to write your own terminal input functions/classes
Could you help me write this? I'm completely clueless where to start.
For the time being I will use the Scanner class, but I could switch over to my own terminal input class at a latter time. This project is due tommorow, so I'm short on time. |
|
Back to top |
|
|
elfprince13 Retired
Super Elite (Last Title)
Joined: 11 Apr 2005 Posts: 3500
|
Posted: 31 Jan 2007 02:22:14 pm Post subject: |
|
|
todlangweilig wrote: Quote: you could indeed use it with Scanner, or any String for that matter, but I still recommend you learn to write your own terminal input functions/classes
Could you help me write this? I'm completely clueless where to start.
For the time being I will use the Scanner class, but I could switch over to my own terminal input class at a latter time. This project is due tommorow, so I'm short on time.
[post="96142"]<{POST_SNAPBACK}>[/post]
Java teachers have gotten lazy with 1.5 You create an InputStreamReader from System.in, create a BufferedReader from that, and then read input from the BufferedReader into a string. Use the online API reference, or read this: http://www.devdaily.com/java/edu/pj/pj010005/pj010005.shtml
@AWW: I was suggesting you redownload it |
|
Back to top |
|
|
|