• Post Reply Bookmark Topic Watch Topic
  • New Topic

Arrays and Text in them  RSS feed

 
Greenhorn
Posts: 9
Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a specific question about how to get Java to look at the data in an array and allow a certain scenario through, but block another.

The assignment is to create a form (swing) that allows the user to input a message, then encode the message into numbers using the ASCII letter equivalent.

For example: the user can input "Hello World" and encode it to numbers 8 5 12 12 15 0 23 15 18 12 4. A "space" is equal to a "0" and it adjusts so that instead of number 65 being A... A is 1, B is 2, C is 3 etc.. Additionally, the string is converted into all caps right off the bat.

On the other side, the user can input numbers 8,5,12,12,15... etc and get the reverse back to Hello World.

The problem I am left with by the instructor is when a user enters spaces in the numbers such as 8, 5, 12, or accidentally puts a space in between numbers such as 8,5,1 2, 2 7, etc.

It needs to accept a space before or after the number, but not in between..

Below is the method that is doing the Decoding by entering the numbers into an array based on where the comma (",") is in the string, and then adding 64, and converting it into the ASCII letter. Anything other than 0 (space) or 1-26 (alphabet) should present a "?".

So, back to my original problem... How can I get it to look at the data in the array and handle the different possible places for the space? Any assistance would be appreciated.



 
Saloon Keeper
Posts: 3330
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String.trim() will remove leading and trailing spaces.
Integer.parseInt(nums[i].trim())
 
Jeff Lanoye
Greenhorn
Posts: 9
Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That did it. Thanks!  The instructor said it was Super Easy, but I was scratching my head.
 
Marshal
Posts: 56608
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Lanoye wrote:. . . It needs to accept a space before or after the number, but not in between.. . . .
You have two problems. One is how do you split on comma with or without spaces and the other is how to remove spaces. I would say that entering 1, 2 3, 4, 5 6, 7 is an error on the users' part and they deserve to have the program crash. You can either split on comma maybe followed by space, or you can remove the spaces from the resultant Strings.
  • 1: Go through the Java™ Tutorials and you can learn about regular expressions because the argument to the String#split() method is a regular expression (that is why they call it regex). It is not difficult to create a regex for comma with or without adjacent spaces. There are regular expression tutorial websites which may show examples of expressions ready made.
  • 2: Go through other methods of the String class and see if you can find a method to replace all instances of space with something else. Or remove spaces from the ends of the text.
  • I think that second suggestion will be easier.
     
    Campbell Ritchie
    Marshal
    Posts: 56608
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    A few minutes ago, I wrote:. . . I think that second suggestion will be easier.
    Yes, you can implement it with the trim() method. I ought to learn to read the th‍read before writing anything.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!