• Post Reply Bookmark Topic Watch Topic
  • New Topic

Data Structures Class Problem  RSS feed

 
Christopher Calderon
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I am new to programming. I am having trouble with an assignment in my data structures class. Here is the problem:


    1.Read a sentence from the console.
    2.Break the sentence into words using the space character as a delimiter.
    3.Iterate over each word, if the word is a numeric value then print its value doubled, otherwise print out the word, with each output on its own line.

I am not sure where to start in doubling the numeric value. I understand that the sentence is a string and I must first convert the string to an int. Here is what I have so far:




And when I run the program this is the output:

run:
I
have
read
8
books
BUILD SUCCESSFUL (total time: 0 seconds)

I have done everything but double that value, which I am sure is the main point of this exercise. I am inclined to think that using an "if" statement within the while loop is the way go. I know I need to write some code that does the following: If a number is found within the sentence, convert to int(or perhaps double) and double its value.

If someone could point me in the right direction, it would be much appreciated.

Thank you

 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 37256
519
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christopher,
Welcome to CodeRanch!

Think about how you identify if a String is a number. Is this a number?
8

Yes. Why? What did you look for?

Now is this a number:
123

Yes. Why? What did you look for?

Finally, is this a number
12r

No. Why not?

Try to express this in English first as a reply to this post. That's the first step in coding something.
 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

You are going to have problems with that loop because implicitly the console always can take more input, so hasNext() will always return true. You can close the input with ctrl‑D (Unix/Mac/Linux) or ctrl‑Z (Windows), but if you do that, you can never use or reopen System.in. Ouch!

My suggestion: Did you know that you can pass a String to the constructor of a Scanner? Read a single sentence with nextLine() (do not read anything else before the nextLine call and don't push enter halfway through the sentence). Pass that sentence to a second Scanner, then see what you can do with the second Scanner object. There are lots of other ways you can achieve the same result. If you are trying to get numbers, go through the methods of Scanner and you should find something to help you.
 
Christopher Calderon
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would look for specific characters. Numbers. I know how to do this using hasNextInt().
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 37256
519
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christopher Calderon wrote:I would look for specific characters. Numbers. I know how to do this using hasNextInt().

That's a good way to do it. You can use an if statement to get an int token instead of a string one.
 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The hasNextInt() method will return true if you pass 123 or 1,234,567 or −1234567890. If you are looking for integers, that will probably do what you want
 
Christopher Calderon
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After sleeping on it I believe I got the result I was looking for! Thanks for pointing me in the write direction. Here is what I came up with:



I tested different numbers and it seemed to work fine. Thanks.
 
Stephan van Hulst
Saloon Keeper
Posts: 7821
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can skip the parseInt() step. Scanner has a nextInt() method.
 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christopher Calderon wrote:. . . . Here is what I came up with: . . . Thanks.
Looking better Now you need to get that input from the keyboard; I have already made a suggestion how to do it. And as Stephan says, don't use parseInt.

Have you tested it with -2147483648 or 2000000000? You cannot say it is working normally until you try that sort of number. Tell us what happens if you use those two.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!