Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Java (http://www.programmingforums.org/forum17.html)
-   -   Number in Java (http://www.programmingforums.org/showthread.php?t=15467)

RainMan Mar 23rd, 2008 4:00 PM

Number in Java
 
Is there any way to have a user input a set of numbers in the form of 0...whatever. i.e like 0...100 and have java take every number between 0 to 100.? I'm asking because as a personal project I am trying to write a program that will evaluate if a set is an algebraic group or not. I'm aware that this has probably been done and better than I will be able to do it but i think it will be a productive exercise for me. I would like the user to be able to enter a set of any size without having to enter in each individual number. Especially since you can have groups that are infinitely large. I haven't figured out how to deal with infinite groups yet. any thoughts on that would be a good thing too.

What I have come up with idea wise so far is to use an array to deal with the numbers, however i only know how to pass the array elements as arguments that the user has to input individually. Then the operations are just simple comparisons and evaluations.

Another thought I have had is to make it so that the program will determine what sets of the entered values are groups, but that involves getting the code to throw out values and keep others to return them, which might be a bit over my head at this point. I am really just focusing on getting the basic program going right now any pointers in the right direction would be appreciated.

Grich Mar 23rd, 2008 10:17 PM

Re: Number in Java
 
Look into the String Tokenizer. It will divide the input string from the user, then you can do whatever you want with the data.

titaniumdecoy Mar 23rd, 2008 10:47 PM

Re: Number in Java
 
You'll get more help if you're more specific about the problem you are having. Decide on the format you will require, show sample input strings, and how you are trying to process them.

RainMan Mar 24th, 2008 12:30 AM

Re: Number in Java
 
The exact problem I am having is that I can't find out if there is a way to ask the user to enter a set of integers of the form 0...some integer say 10 in the form 0...10 and have java take it to mean all the integers from 0 to 10. as in 0 1 2 3 4 5 6 7 8 9 10 . this is common notation in the math world.

the closest approximation i can think of in code
:

  1. System.out.print( "please enter the first and last integers of the set in the form a...b: ");
  2. something in here that actually takes the set and passes it to an array without needing several hundred or thousand variables declared.

and then have java put those values into an array. rather than have a set with 1000 integers in it and having something like
:

  1. System.out.print(" please first  integer: ");
  2. int a = input.nextInt();
  3.  
  4. System.out.print("please enter  second integer: ");
  5. int b = input.nextInt();
  6. .
  7. .
  8. .
  9. .
  10. .
  11. 1000 times



What i came up with is entering the first value and then the last value and trying to put it into a for loop with an array.

:

  1.         public double setAlength( double al )
  2.         {
  3.                 al = last - first;
  4.         }
  5.         public double setGroupArray( double garray[])
  6.         {
  7.  
  8.                 groupArray = garray;
  9.  
  10.                 for( double counter = 0; counter < alength; counter++)
  11.                 {
  12.                         garray[counter] = first + counter;
  13.  
  14.                 }


this seems clumsy though and i don't really think i have it set up right yet, a big problem with this is that some algebraic groups do not start at 0 or a positive number which would cause issues when you get into the negative numbers. However i'm not asking you to tell me how to write the code, i'm asking if anyone knows if there is something in java to accept entering mathematical sets in this nature..and if there is then where i might find out about it. Any critiques and suggestions of the code will be accepted in a gracious manner if you feel like offering them..but I don't ask people to do my work for me. i will succeed or fail on my own. The string tokenizer will work the data after it's been input into the program, but i'm trying to change the way the data is input into the program...if i'm understanding what i read about it correctly, if i'm not feel free to say so. If i was just going to confine it to easily entered sets for groups it wouldn't be an issue, but easily entered sets for groups are easily validated or invalidated by hand. i don't know about anyone else but a program that is supposed to make life easier that makes you input several thousand numbers doesn't really seem to be worth it.

titaniumdecoy Mar 24th, 2008 2:14 AM

Re: Number in Java
 
Consider that the number of elements in the array will be abs(first-last) + 1.

I will try to keep this fairly vague since it seems you want to solve this problem yourself. For the loop, I would suggest using two variables as counters: one as an index into the array from 0 to the number of elements in the array - 1; and another for counting from the first to the last number in the set. (There are, as always, numerous ways to achieve the same goal.)

As for allowing user input in the format a...b, you could use StringTokenizer or Pattern and Matcher for regular expression matching.

Fall Back Son Mar 31st, 2008 7:38 PM

Re: Number in Java
 
Quote:

Originally Posted by RainMan (Post 142853)
The exact problem I am having is that I can't find out if there is a way to ask the user to enter a set of integers of the form 0...some integer say 10 in the form 0...10 and have java take it to mean all the integers from 0 to 10.

There are multiple ways you can do this.

If you don't insist on using that format, you can accomplish the task easily by requesting the user to enter the first and last numbers in the set. Or you can let them enter it however they want and just treat the set as the values between the first value they entered and the last value they entered. You don't have to "deal with" infinite ranges (in most applications) because for any given value, you can just check if its between the two values the user entered -- if so, its part of the set.

If you do insist on using that format, look into the classes Matcher and Pattern. Reading about Regular Expressions will be particularly helpful to you. Regular Expressions allow you to match input based on a pattern... which is exactly what you want to do. Here's the link to a tutorial from Sun... I've read it myself, its very helpful:

http://java.sun.com/docs/books/tutor...gex/index.html


All times are GMT -5. The time now is 4:49 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC