Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Dec 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Scanner/array troubles

    This program is to have to user enter numbers in the top text field and then be able to sort them and/or show the min number.

    Ive been working on this code for multiple hours now today, I have it almost done but I cant not get my buildArray class to work properly. Its mostly the scanner method Im having troubles with. I just want to be able to use "," and not have them show up as -1.


    Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    
    public class MainWindow extends JFrame implements ActionListener {
        JScrollPane pane1;
        JScrollPane pane2;
        JTextArea area1;
        JTextArea area2;
        JLabel area1Title;
        JLabel area2Title;
        JButton sort;
        JButton findMins;
        JButton clearArea1;
        JButton clearArea2;
        
        public MainWindow() {
            super("Array Fun");
            setBounds(100, 200, 800, 800);
            getContentPane().setLayout(null);
            setVisible(true);
            
            area1 = new JTextArea();
            pane1 = new JScrollPane(area1);
            pane1.setBounds(10, 10, 400, 300);
            add(pane1);
            
            area1Title = new JLabel("Enter text in the above space", JLabel.CENTER);
            area1Title.setBounds(10, 320, 400, 40);
            add(area1Title);
            
            area2 = new JTextArea();
            area2.setEditable(false);
            pane2 = new JScrollPane(area2);
            pane2.setBounds(10, 400, 400, 300);
            add(pane2);
     
            area2Title = new JLabel("Output text area", JLabel.CENTER);
            area2Title.setBounds(10, 710, 400, 40);
            add(area2Title);
            
            findMins = new JButton("Find Minimums");
            findMins.setBounds(450, 250, 250, 40);
            findMins.addActionListener(this);
            add(findMins);
            
            sort = new JButton("Sort Descending");
            sort.setBounds(450, 320, 250, 40);
            sort.addActionListener(this);
            add(sort);
            
            clearArea1 = new JButton("Clear Input Text Area");
            clearArea1.setBounds(450, 380, 250, 40);
            clearArea1.addActionListener(this);
            add(clearArea1);
            
            clearArea2 = new JButton("Clear Output Text Area");
            clearArea2.setBounds(450, 430, 250, 40);
            clearArea2.addActionListener(this);
            add(clearArea2);
            
            repaint();
        }
        
        private int[] buildArray(String line) {
            int[]arr = new int[line.length()];
            Scanner scan = new Scanner (line);
            scan.useDelimiter (",");
            
            for (int i = 0; i < line.length(); i++){
               
                arr[i] = Character.digit(line.charAt(i),10);
                
               }
                return arr;
        }
        
        private int findMin(int numbers[]) {
        //PRE numbers.length >= 1
            
            int min = numbers [0];
            
            for (int i = 1; i < numbers.length; i++){
                
                if (numbers [i] < min) {
                    min = numbers [i];
                }
            } 
            return min;
        }
        
        private String makeString(int numbers[]) {
        //PRE numbers.length >= 1
        int i = 0;
        String s1 = new String ("");
        while (i < numbers.length){
            s1 = numbers[i] + s1 ;
            i++;
            
        }
        return s1;
    
        }
        
        private void sort(int numbers[]) {
            for (int i = 1; i< numbers.length; i++){
                int j = i-1;
                int temp = numbers[i];
                while (j >= 0 && temp > (numbers[j])){
                  numbers[j+1] = numbers[j];
                  j--;
                }
                numbers[j+1] = temp;
            }
        }
        
        private void sortLines() {
            int result;
            int lineNumber = 1;
            int numbers[];
            String contents = area1.getText();
            Scanner nextLine = new Scanner(contents);
            nextLine.useDelimiter("\n");
            
            String line;
            String outputLine;
            
            while (nextLine.hasNext()) {
                line = nextLine.next();
                numbers = buildArray(line);
                sort(numbers);
                outputLine = "Line "+lineNumber+" Sorted:\t"+makeString(numbers)+'\n';
                area2.append(outputLine);
                lineNumber++;
            }
        }
        
        private void findMins() {
            int lineNumber = 1;
            int numbers[];
            String contents = area1.getText();
            Scanner nextLine = new Scanner(contents);
            nextLine.useDelimiter("\n");
            
            String line;
            String outputLine;
            
            while (nextLine.hasNext()) {
                line = nextLine.next();
                numbers = buildArray(line);
                outputLine = "The minimum value in line "+lineNumber+" is "+ findMin(numbers)+'\n';
                area2.append(outputLine);
                lineNumber++;
            }
        }
            
        public void actionPerformed(ActionEvent e) {
            if ((JButton) e.getSource() == clearArea1)
                area1.setText("");
            else if ((JButton) e.getSource() == clearArea2)
                area2.setText("");
            else if ((JButton) e.getSource() == findMins)
                findMins();
            else
                sortLines();
        }
    }
    Any help is appreciated if I can just get the commas to work I can be done.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,980
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    I wouldn't work with an int[] at all with unknown sized data. Use a List<Integer> since you don't need to resize it. Lists have a bonus as well, they are both sortable by collections and also allow the use of Collections.min on them, so that makes the other methods a little on the useless side.

    I don't know what your input data is. It appears it should take the format of:
    Code:
    x, x, x, x, x
    x, x, x, x, x, x, x
    x, x, x
    x
    ...
    Is that right? Given the findMins method I'd expect you want to provide the above data, and then for each line you want to show the minimum independent of the other lines, is that right?
    BTW, I'd personally use a StringTokenizer instead of the scanner for this.


  •  

    Tags for this Thread

    Posting Permissions

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