• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Creating a method that retrieves unique integers followed by a whitespace in a array

 
Greenhorn
Posts: 10
Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good morning everyone,

I'm currenting trying to create one out of four methods. The method I'm currently on is in regards to the subject above. Below is my code so far:



I've been working with java for a few months now. Seems it would of been best to have started using this language the moment it was introduced. I'm really having trouble with the white spaces concept. Do I need to pares a string or something in order to even get started on the right path. Any advice would be much appreciated.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sherwin Slaughter wrote:I'm really having trouble with the white spaces concept.  



What "white spaces concept" are you referring to?

Henry
 
Sherwin Slaughter
Greenhorn
Posts: 10
Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:

Sherwin Slaughter wrote:I'm really having trouble with the white spaces concept.  



What "white spaces concept" are you referring to?

Henry



I have to ensure white spaces(instead of a comma to separate integers) appear after the user inputs an integer within the array.
 
Saloon Keeper
Posts: 15488
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can learn Java at any time. It's actually probably easier to learn it now, than it is starting with 1.1 and then moving through the versions.

The main problem here is that the names you're using in your method don't really match what you're trying to do: The method is called getDate(), but you're not returning a date. You're not even returning anything. The parameter is called set, but it's not a set, and if it were, it's not clear what it is a set of.

You're retrieving ints from the keyboard, but it's not clear what the ints are supposed to represent.

You had best describe what kind of input you expect the program to deal with, and what the expected result should be. Then come up with a descriptive method name that explains succinctly what it does, and give the method parameter a name that clearly shows what it contains.
 
Stephan van Hulst
Saloon Keeper
Posts: 15488
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sherwin Slaughter wrote:I have to ensure white spaces(instead of a comma to separate integers) appear after the user inputs an integer within the array.


It's not clear what it is you're getting the ints from in the first place. Is kbd a Scanner? If so, it already deals with white space. If you want to add spaces to your array, then it doesn't make sense for it to be an int[], because you can't store spaces in an int[] (and I'm not sure why you'd want to in the first place).

If the result of the method has to represent a date, you might want to use the java.time.LocalDate class, of which you can create an instance using LocalDate.of(int year, int month, int dayOfMonth).
 
Sherwin Slaughter
Greenhorn
Posts: 10
Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:You can learn Java at any time. It's actually probably easier to learn it now, than it is starting with 1.1 and then moving through the versions.

The main problem here is that the names you're using in your method don't really match what you're trying to do: The method is called getDate(), but you're not returning a date. You're not even returning anything. The parameter is called set, but it's not a set, and if it were, it's not clear what it is a set of.

You're retrieving ints from the keyboard, but it's not clear what the ints are supposed to represent.

You had best describe what kind of input you expect the program to deal with, and what the expected result should be. Then come up with a descriptive method name that explains succinctly what it does, and give the method parameter a name that clearly shows what it contains.



Copy that. I'll reword some aspects of this method.
 
Sherwin Slaughter
Greenhorn
Posts: 10
Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:

Sherwin Slaughter wrote:I have to ensure white spaces(instead of a comma to separate integers) appear after the user inputs an integer within the array.


It's not clear what it is you're getting the ints from in the first place. Is kbd a Scanner? If so, it already deals with white space. If you want to add spaces to your array, then it doesn't make sense for it to be an int[], because you can't store spaces in an int[] (and I'm not sure why you'd want to in the first place).

If the result of the method has to represent a date, you might want to use the java.time.LocalDate class, of which you can create an instance using LocalDate.of(int year, int month, int dayOfMonth).



My apologizes. I left a lot of information out. Yes, its utilizing scanner. I typed date instead of data by mistake. I'll have to reword this a bit in order for it to make sense. Sorry about that.
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sherwin Slaughter wrote:
I have to ensure white spaces(instead of a comma to separate integers) appear after the user inputs an integer within the array.



Sherwin Slaughter wrote:
My apologizes. I left a lot of information out. Yes, its utilizing scanner. I typed date instead of data by mistake. I'll have to reword this a bit in order for it to make sense. Sorry about that.



The Scanner class allows you to configure the delimiters. So, if you want to allow commas, or anything else, to separate integers, you can do it by configuring the delimiter.

Henry
 
Sherwin Slaughter
Greenhorn
Posts: 10
Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
*Updated Code*
Good morning everyone,


1.) The summary of this program is for it to used to perform simple set manipulation.

                                Example set: 5, 9, 16, 9, 3, 0, 5

    Example program execution set: 0, 3, 5, 9, 16                *Note: no negative numbers.

2.) Program Outline

- I'll ask a user to give me two sets of integers. The sets can be 20 or less in total.
- The integers will be sorted in the two sets in ascending order using any sorting algorithm.
- Caclulate and display the intersection of the two sets
- .............................. the difference: first set - second set
- .............................. the difference: second set - first set
- Ask the user if they want to repeat the program again with two different sets. Y or y or a negative number to terminate.


3.) Limitations

- No global variable (exempt: kbd)
- Manage partially filled arrays
- Methods that HAVE to be in the program:
    --public static int getData(int [] set)
         ---must contain whitespace characters after numbers except for the last one (ex 3, 5, 6, 2)
         ---return value will be the number of unique integers placed in set

    --public static int intersection(int [] setA, int sizeA, int [] setB, int sizeB, int [] resultSet)
         ---aspect to have setA & setB arrays to be holding properly created sets in ascending order
         ---return value from the method is the number of elements that were place into resultSet
   
    --public static int difference(int [] setA, int sizeA, int[] setB, int sizeB, int[] resultSet)
         ---calculates the difference (A-B) of the two sets
         ---aspect to have setA & setB arrays to be holding properly created sets in ascending order
         ---return value from the method is the number of elements that were place into resultSet

    --public static void sort(int [] nums, int size)
         ---sorts the elements in nums will have been rearranged into sorted, ascending order. size is the number
             of elements in nums

4.) Key Notes

- Results should be display the contents of a single set all on one line, with each number separated by commas

- the intersection, difference and sort method is prohibited to print anything to the screen.


My updated code so far is contained below. I'm having an issue currently in the "getData" method for filtering duplicate integers and the sorting code still needs to be put in. Any suggestions?




 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You don't seem to be sending the variable temp into the method search(), so you're never finding duplicates.

There are other problems too...
 
Knute Snortum
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What happens if you enter a duplicate number?  Right now you don't set the new number into the array, but you do increment the index.  Is that really what you want?

You are prompting for "at least 20" numbers.  Don't you mean "less than 20"?

What happens when you enter a non-integer?
 
Sherwin Slaughter
Greenhorn
Posts: 10
Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Knute Snortum wrote:What happens if you enter a duplicate number?  Right now you don't set the new number into the array, but you do increment the index.  Is that really what you want?

You are prompting for "at least 20" numbers.  Don't you mean "less than 20"?

What happens when you enter a non-integer?



When you enter a duplicate number has it is coded currently, that number is put in that array (typing for setA or setB). Based off the if statement in get date. If a number is typed that is less than 0, it will not be placed in the set, it stops the loop.

Yes no more than MAXSIZE, but can be less. But only max to 20 unique numbers. I'll change the verbiage.

A error will occur if a non- integer is put in. Not sure how to go about error correction with my experience.
 
Knute Snortum
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Currently you're sending set[usedSize] into the search() method.  Why?  Don't you want to test temp?
 
Sherwin Slaughter
Greenhorn
Posts: 10
Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Knute Snortum wrote:
Currently you're sending set[usedSize] into the search() method.  Why?  Don't you want to test temp?



A grad student was helping us in lab the other day. Yea, it doesn't make sense to have that there. I just "//" it.  The duplication situation is driving me up a wall.
 
Knute Snortum
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

This is what the comments say you should send to the search() method.  Notice the third parameter.  Are you sending in the correct variable?
 
Sherwin Slaughter
Greenhorn
Posts: 10
Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Knute Snortum wrote:
This is what the comments say you should send to the search() method.  Notice the third parameter.  Are you sending in the correct variable?



I have to figure something out. At first I thought I was getting somewhere, but the other methods are not going to be able to show correct output unless the get data method is programmed correctly. The search method is to be used in if statements. The difference methods work, but not the intersection. I believe its because of duplicate numbers issue. So many fine details I need but have never been taught or broken down. It's like you have to teach yourself all of this.
 
Sherwin Slaughter
Greenhorn
Posts: 10
Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Running into duplicate issue for getData method:



Is there something I'm overlooking? Below is the error output:

Please enter no more than 20 unique integers for the set, pressing the ENTER key after each integer:
(*When finished, please enter a negative number)
1
A duplicate does not exist
A duplicate does not exist
A duplicate does not exist
A duplicate does not exist
A duplicate does not exist
A duplicate does not exist
A duplicate does not exist
A duplicate does not exist
A duplicate does not exist
A duplicate does not exist
A duplicate does not exist
A duplicate does not exist
A duplicate does not exist
A duplicate does not exist
A duplicate does not exist
A duplicate does not exist
A duplicate does not exist
A duplicate does not exist
A duplicate does not exist
A duplicate exist
A duplicate exist
A duplicate exist
A duplicate exist
A duplicate exist
A duplicate exist
A duplicate exist
 
Sherwin Slaughter
Greenhorn
Posts: 10
Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was finally able to come up with a solution on the duplicate issue after like 5 hours lol:



Last thing now is the sorting method..........sigh.
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The goofy (sorry, but that's just an honest assessment) API design aside, this illustrates how poor names can necessitate comments which can often be poor stand-ins for clear, expressive code.

The idea behind the "search" method is really better expressed by "contains" -- the comment on line 90 is technically incorrect: the user enters a number, not a character. Both these inaccuracies combine to make the code less clear.  Consider this version, with better names (again, let's just ignore the goofiness of the arguments you have to supply to these methods):

By at least doing this, you can hide the ugliness of the search() name somewhere and replace it with a more appropriately expressive name. This allows you to remove the inaccurate comment that has been made redundant by the more expressive method name.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic