Results 1 to 13 of 13
  1. #1
    Suzanne1187 is offline Member
    Join Date
    Feb 2009
    Posts
    11
    Rep Power
    0

    Default Scanner questions

    So I have class....

    public int rangeInScanner(Scanner stream)

    if user enter an unkown amount of ints into Scanner, how do I write a class to return the rage of the numbers, biggest - smallest.

    p.s. the numbers entered will be in random order and may be negative

  2. #2
    Join Date
    Mar 2009
    Posts
    4
    Rep Power
    0

    Default

    "Unknown amount" is sort of undefined in your question, does the user know how many ints they will enter, or is the program unaware.
    You can do this with a simple algorithm, or use arrays and the Array class. this may help

    import java.util.Arrays;
    import java.util.Scanner;

    public class HighLow {

    public static void main(String[] args) {
    int set;
    int [] Set;
    int elem;

    Scanner sc = new Scanner(System.in);

    System.out.println("how many numbers would you like to enter?");

    set = sc.nextInt();

    Set = new int [set];

    for (int i = 0; i < set; i++)
    {
    System.out.print("Enter number " + (i+1)+" :");
    elem = sc.nextInt();
    Set[i] = elem;
    }

    Arrays.sort(Set);
    System.out.println("\n");
    System.out.println("Set of numbers in order ");

    for (int i =0; i < Set.length; i++)
    {
    System.out.print(Set[i] + ", ");
    }

    System.out.println("\n");
    System.out.println("Largest int: " + Set[Set.length - 1]);
    System.out.println("Smallest int: " + Set[0]);

    }

    }

  3. #3
    Suzanne1187 is offline Member
    Join Date
    Feb 2009
    Posts
    11
    Rep Power
    0

    Default

    The program is unaware how many ints will be entered.

    Also while loops must be used, no arrays

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Think about how you would solve this problem "by hand" ie without a computer. In particular if someone told you a list of numbers and you knew at the end you were going to be asked the biggest and smallest number what would you have to remember?

    Once you have a definite idea of what you are going to do, you can begin writing your code.

  5. #5
    Join Date
    Mar 2009
    Posts
    4
    Rep Power
    0

    Default

    ok. If a while loop is used, there must be some sort of condition that allows the user to break from loop. ( i.e "enter 0 to exit"). and what exactly does the output have to be.

  6. #6
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    7

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Quote Originally Posted by mtyoung View Post
    while (sc.hasNextInt()) {
    // do something
    }
    Possible, although "A scanning operation may block waiting for input. "

    More importantly the OP has yet to show some thought - or resultant clue - about what is involved in determining a range.

  8. #8
    sandeepsai39 is offline Member
    Join Date
    Feb 2009
    Posts
    96
    Rep Power
    0

    Default

    hi
    u can achive this using ArrayList ,but I am using Objects instead of Integer values .we can get integer values very easily where we want ,ok.

    import java.io.InputStreamReader;
    import java.util.ArrayList;
    import java.util.Scanner;

    public class ScannerExample {
    public ScannerExample(){
    Scanner sc=new Scanner(System.in);
    System.out.println("Please enter the Integer Numbers and Enter any Character from A-Z to Escape from input ");
    rangeScanner(sc);
    }

    public static void rangeScanner(Scanner sc) {
    ArrayList number = new ArrayList();
    while(sc.hasNextInt()){
    number.add(sc.nextInt());

    }
    // sorts in ascending order
    Object[] sorted = number.toArray();
    java.util.Arrays.sort(sorted);

    // sorted[index] returns the result in the form of
    // objects.If u want to get the result as integers use
    // Integer.parseInt(sorted[ndex].toString())
    System.out.println("minimum number is " + sorted[0]);
    System.out.println("maximum number is " + sorted[sorted.length-1]);
    System.out.println("Range of numbers is " + sorted[0]+ "to" + sorted[sorted.length-1]);

    }
    public static void main(String[] args) {
    ScannerExample ex=new ScannerExample();
    }
    }

  9. #9
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    All this sorting seems a bit silly.

    In order to sort you have to compare everything with at least a fair number of other things - the result is going to be O(n*log n) at best. But finding the range doesn't involve all these comparisons.

    Think about walking along a beach at low tide. Looking around it is often obvious from the wave action what the range of the tide is. To achieve this each wave did not need to be compared to a number of others. Rather, each wave contributed it's own "information" to the tidal range by means of a single comparison with some aspect of the beach state at the time. Making the process O(n).

    If nature can achieve such efficiency, I'm sure programmers can. But only by stopping and thinking for a bit as I suggested in reply #4. If you were listening to a person reading a list of numbers and you had to reply with the range, would you really have to write them all down in a list, set or whatever, so you could sort them later?

  10. #10
    sandeepsai39 is offline Member
    Join Date
    Feb 2009
    Posts
    96
    Rep Power
    0

    Default

    Thanku .As u said it is unnecessary to use sorting algorthiam in my code.

  11. #11
    sandeepsai39 is offline Member
    Join Date
    Feb 2009
    Posts
    96
    Rep Power
    0

    Default

    hi i am reposting my code without using sorting algoritham .Please give your
    suggestions on my code. I think this time i am good in my code.

    import java.util.Scanner;

    public class ScannerExample {
    public ScannerExample(){
    Scanner sc=new Scanner(System.in);
    System.out.println("Please enter the Integer Numbers and Enter any Character from A-Z to Escape from input ");
    rangeScanner(sc);
    }

    public static void rangeScanner(Scanner sc) {
    int max=0;
    int min=0;
    int temp= 0;

    while(sc.hasNextInt()){
    temp = sc.nextInt();
    if(temp>max){
    min = max;
    max = temp;
    }
    else if(min >= temp){
    min =temp;
    }
    }

    System.out.println("minimum number is " + min);
    System.out.println("maximum number is " + max);
    System.out.println("Range of numbers is " + min+ "to" + max);

    }
    public static void main(String[] args) {
    ScannerExample ex=new ScannerExample();
    }
    }
    Last edited by sandeepsai39; 03-11-2009 at 12:11 PM.

  12. #12
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Hi sandeepsai39.

    The general idea seems OK. But there are problems:

    Java Code:
    Please enter the Integer Numbers and Enter any Character from A-Z to Escape from input 
    1
    2
    3
    a
    minimum number is 2
    maximum number is 3
    Range of numbers is 2to3
    The result should, of course, be "Range of numbers is 1 to 3".

    For each int that comes from the scanner you have to test both whether it's bigger than max and whether it's less than min.

  13. #13
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Also that "min=max" business is really weird ;-) But I think I know what was bothering you ... and how to fix it.

    Why did you make max equal to zero? (Answer: you made it small so that when the scanner provided the first int then max would be set to that). So why not start with min very very big? That way min is certain to get adjusted when the first number is read by the scanner.

Similar Threads

  1. questions for 1yr exp
    By rahaman.athiq in forum Java Servlet
    Replies: 2
    Last Post: 11-26-2008, 02:13 AM
  2. I have Questions -_-
    By ChazZeromus in forum New To Java
    Replies: 5
    Last Post: 09-13-2008, 09:08 PM
  3. Just a Few Questions
    By pringle in forum New To Java
    Replies: 21
    Last Post: 01-09-2008, 07:21 PM
  4. questions
    By Gilgamesh in forum New To Java
    Replies: 3
    Last Post: 11-28-2007, 12:18 AM
  5. 3 Questions
    By hiranya in forum AWT / Swing
    Replies: 4
    Last Post: 11-14-2007, 05:57 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •