Jump to content

Java: Help Assessing a Problem

AjAlien

Greetings,

I am not that great with programming so far, although I continue to try, and I have come across a problem figuring out how exactly to tackle this problem.

 

Problem: I have two integer variables - "odd" and "even." I have a user inputted string via scanner that must remove or skip all white spaces. I have to keep track of the location of all non-white space characters, example being an input value of "f oo" being read as "foo" with input value of "f" being "1" and so on. Also, what seems tricky to me the most, is update the "odd" or "even" variable based if the location is odd or even.

 

I'd appreciate the help!

CPU - I7 4770k @ 4.3GHz GPU - EVGA Nvidia GeForce GTX 780 Motherboard - ASUS Maximus VI Extreme RAM - 16GB of Corsair Dominator Platinum @ 2133MHz  Case - Corsair 800D PSU - Corsair AX1,200i CPU Cooler - Custom Liquid CPU Cooler SSD - Samsung 840 Pro 256GB Hard Drive - Western Digital Black 1TB.

Link to comment
Share on other sites

Link to post
Share on other sites

For tracking odd and even numbers you'll need to use the modulus operator (%).

For counting the value of times spaces are in a string see http://stackoverflow.com/questions/275944/java-how-do-i-count-the-number-of-occurrences-of-a-char-in-a-string

For removing whitespace you can either use a Regex (used to match character sequences) or the 'Replace' function. Replace in this case is the better option (because of readability) to use because but I thought I should tell you about Regex's anyways.


stringYouWantToModify = stringYouWantToModify.replace(" ", "");

The first parameter of Replace is the value you want to replace, in this case a space. The second parameter is the value you want to replace it to, in this case nothing. This line of code will remove all spaces from 'stringYouWantToModify'.

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, AjAlien said:

Problem: I have two integer variables - "odd" and "even." I have a user inputted string via scanner that must remove or skip all white spaces. I have to keep track of the location of all non-white space characters, example being an input value of "f oo" being read as "foo" with input value of "f" being "1" and so on. Also, what seems tricky to me the most, is update the "odd" or "even" variable based if the location is odd or even.

So there are a few ways to do this and I think that @fringie really hit the nail on the head. Perhaps the easiest way to do this though is something like the following:
 

  • Don't bother removing all the spaces from a string first.
  • Simply iterate through the string
  • If the character is not a space then do the following, otherwise start over
  • Increment a counter
  • If the counter is even, then even is equal to that character
  • If the counter is odd, then odd is equal to that character

In python it would look like this (think of this like psuedo code):

even = None
odd = None
character_counter = 0

user_string = input("Please enter a string: ")

for user_character in user_string:
	if user_character != space:
    	character_counter += 1
        
        if character_counter % 2 == 0:
        	even = user_character
            
        else:
        	odd = user_character

It's important to note that python allows you to treat a string as a char array. In java you simply have to convert the users string into a character array first or use the "string.chartat()" method.

ENCRYPTION IS NOT A CRIME

Link to comment
Share on other sites

Link to post
Share on other sites

first thing you should do is remove the spaces with a method (there is a string method for this). Java has a core library used to convert one data type to another. It usually is a method like datatype.toInteger() or datatype.tofloat() for example where datatype is the data type like string, char or others. It is very important that you catch the exception incase it is invalid so you can either get the user to correct or do something else you want it to do. You can convert one data type to another as long as there is some compatibility. Than just do a modulo (%) with 2 and if its 1 it is odd and if it is 0 its even.

 

Also java's own documentation is somewhat tolerable compared to C++ so its worth going through it and seeing what the string class offers.

Link to comment
Share on other sites

Link to post
Share on other sites

11 hours ago, straight_stewie said:

So there are a few ways to do this and I think that @fringie really hit the nail on the head. Perhaps the easiest way to do this though is something like the following:
 

  • Don't bother removing all the spaces from a string first.
  • Simply iterate through the string
  • If the character is not a space then do the following, otherwise start over
  • Increment a counter
  • If the counter is even, then even is equal to that character
  • If the counter is odd, then odd is equal to that character

In python it would look like this (think of this like psuedo code):


even = None
odd = None
character_counter = 0

user_string = input("Please enter a string: ")

for user_character in user_string:
	if user_character != space:
    	character_counter += 1
        
        if character_counter % 2 == 0:
        	even = user_character
            
        else:
        	odd = user_character

It's important to note that python allows you to treat a string as a char array. In java you simply have to convert the users string into a character array first or use the "string.chartat()" method.

problem with this code is that it doesn't keep track of the location of the whitespace as requested. For that I would loop range(len(user_string)) then store the number in a array.

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×