Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 14 of 14

Thread: Arraylits for handling errors?

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Location
    Stockholm
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Arraylits for handling errors?

    Hey!
    Im new to java...and programming in general. Im trying to make a program where the user can input 3 fields. I want the program to tell the user what fields the user forgot to fill in.
    Can I use an arraylist for that? Help would be greatly appreciated It doesnt work like iwaHere is my code:

    for (;;) {
    				try {
     
     
     
    					int svar = JOptionPane.showConfirmDialog(Main.this, reg,
    							"Registrera deltagare",
    							JOptionPane.OK_CANCEL_OPTION);
     
    					if (svar != OK_OPTION)
    						return;
     
    					ArrayList<String> errorlog = new ArrayList<String>();
     
    					String name = reg.getName();
    					String country = reg.getCountry();
    					String age = reg.getAge();
     
    					if (age.length()==0); 
    						errorlog.add("Ålder");
    					if (name.length()==0)
    						errorlog.add("Namn");
    					if (country.length()==0) {
    						errorlog.add("Land");
    						System.out.println(age);
    					JOptionPane.showMessageDialog(Main.this, "You forgot to fill in: "+errorlog,
    							"Problem", JOptionPane.ERROR_MESSAGE); } 
    					else {
    					int age2 = Integer.parseInt(age);	
    					People p = new People(name, country, age2);
    					persons.add(p);
    					startnr++;
    					break;
    					}
    				} catch (NumberFormatException e) {
     
    					JOptionPane.showMessageDialog(Main.this, "Only numbers in this field, please.",
    							"Fel", JOptionPane.ERROR_MESSAGE);
    				}
     
    			}


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Arraylits for handling errors?

    What is the reg variable? It looks like a class with methods that are used to get user input.

    I want the program to tell the user what fields the user forgot to fill in.
    The code already does something like that with the errorlog variable. What is the problem with how that works?
    The posted code can't be compiled for testing. Can you post a small complete program that compiles, executes and shows the problem.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Location
    Stockholm
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Arraylits for handling errors?

    Quote Originally Posted by Norm View Post
    What is the reg variable? It looks like a class with methods that are used to get user input.


    The code already does something like that with the errorlog variable. What is the problem with how that works?
    The posted code can't be compiled for testing. Can you post a small complete program that compiles, executes and shows the problem.
    Hey, thanks for the answer. reg is the form from where i get user input.
    The problem is that it doesnt work correct. Ill try to explain...

    If i type nothing, it works correct (tells me i forgot name, country and age)
    If i type only name, it works correct (tells me i forgot country and age)

    If i type name and age, it doesnt work (tells me i forgot country and age)
    If i type country and age, it doesnt work (it doesnt tell me i forgot the name at all)


    etc... is it something wrong with the brackets or is it something else?

    Please help!

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Arraylits for handling errors?

    If i type name and age, it doesnt work (tells me i forgot country and age)
    If i type country and age, it doesnt work (it doesnt tell me i forgot the name at all)
    Try debugging the code by adding some println statements to print out the values of the three variables that the data is read into so you can see what the computer is seeing. The wrong answers you are getting must be because the data is not what you are expecting.
    Be sure to put delimiters around the variables so you can see all of their value:
    System.out.println("varName="+ varName + "<");
    Replace varName with the name of each of the 3 variables.

    is it something wrong with the brackets
    You should use {}s with all if statements. It's safer.

    When does the code execute the JOptionPane statement that shows the error message? It's inside of an if statement. When is that if statement true?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Nov 2012
    Location
    Stockholm
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Arraylits for handling errors?

    Where should I write the system.out.print?


    I know this is going to be a lot of code...but im pasteing it here anyway. I think you can help me better if you see all of it.

    Here it goes:

    import static javax.swing.JOptionPane.OK_OPTION;
     
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
     
    public class Main extends JFrame {
    	int startnr = 1;
    	JTextArea textarea = new JTextArea();
    	ArrayList<People> persons = new ArrayList<People>();
     
    	Main() {
    		super("DSV Kista Marathon");
    		setLayout(new BorderLayout());
     
    		JPanel north = new JPanel();
    		north.add(new JLabel("DSV Marathon"));
    		add(north, BorderLayout.NORTH);
     
    		JPanel south = new JPanel();
    		JButton registerNew = new JButton("Ny");
    		registerNew.addActionListener(new RegisterListener());
    		JButton show = new JButton("Visa");
    		show.addActionListener(new ShowListener());
    		JButton time = new JButton("Tid");
    		time.addActionListener(new TimeListener());
    		south.setLayout(new FlowLayout(FlowLayout.LEFT));
    		south.add(registerNew);
    		south.add(show);
    		south.add(time);
    		add(south, BorderLayout.SOUTH);
     
    		JPanel east = new JPanel();
    		JPanel eastSouth = new JPanel();
    		eastSouth.setLayout(new BoxLayout(eastSouth, BoxLayout.Y_AXIS));
    		east.setLayout(new BorderLayout());
    		east.add(eastSouth, BorderLayout.SOUTH);
    		eastSouth.add(new JLabel("Sortering"));
     
    		ButtonGroup knappgrupp = new ButtonGroup();
    		JRadioButton sortStartNr = new JRadioButton("Startnr", true);
    		JRadioButton sortName = new JRadioButton("Namn");
    		JRadioButton sortAge = new JRadioButton("Ålder");
    		JRadioButton sortTime = new JRadioButton("Tid");
    		knappgrupp.add(sortStartNr);
    		knappgrupp.add(sortName);
    		knappgrupp.add(sortAge);
    		knappgrupp.add(sortTime);
     
    		eastSouth.add(sortStartNr);
    		eastSouth.add(sortName);
    		eastSouth.add(sortAge);
    		eastSouth.add(sortTime);
    		add(east, BorderLayout.EAST);
     
    		JScrollPane scroll = new JScrollPane(textarea);
     
    		textarea.setEditable(false);
    		add(scroll);
     
    		setVisible(true);
    		setSize(400, 500);
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
     
    	}
     
    	class Register extends JPanel {
    		JTextField nameField = new JTextField(10);
    		JTextField countryField = new JTextField(10);
    		JTextField ageField = new JTextField(3);
     
    		public Register() {
     
     
    			setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
    			JLabel rubrik = new JLabel ("Startnr: "+startnr); 
    			JPanel row1 = new JPanel(new FlowLayout(FlowLayout.LEFT));
    			add(rubrik);
    			row1.add(new JLabel("Namn:"));
    			row1.add(nameField);
    			add(row1);
     
     
    			JPanel row2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
    			row2.add(new JLabel("Land: "));
    			row2.add(countryField);
    			add(row2);
     
    			JPanel row3 = new JPanel(new FlowLayout(FlowLayout.LEFT));
    			row3.add(new JLabel("Ålder:"));
    			row3.add(ageField);
    			add(row3);
     
     
     
     
     
    		}
     
    		public String getName() {
    			return nameField.getText();
    		}
     
    		public String getCountry() {
    			return countryField.getText();
    		}
     
    		public String getAge() {
    			return ageField.getText();
    		} 
     
    		/*public int getAge() {
    			return Integer.parseInt(ageField.getText());
    		} */
    	}
     
    	class RegisterListener implements ActionListener {
    		public void actionPerformed(ActionEvent event) {
    			Register reg = new Register();
     
    			for (;;) {
    				try { 
     
     
     
    					int svar = JOptionPane.showConfirmDialog(Main.this, reg,
    							"Registrera deltagare",
    							JOptionPane.OK_CANCEL_OPTION);
     
    					if (svar != OK_OPTION)
    						return;
     
    					ArrayList<String> errorlog = new ArrayList<String>();
     
    					String name = reg.getName();
    					String country = reg.getCountry();
    					String age = reg.getAge();
     
    					if (name.length()==0) 
    						errorlog.add("Namn"); 
    					if (country.length()==0) 
    						errorlog.add("Land");	
    					if (age.length()==0) {
    						errorlog.add("Ålder"); 
    					JOptionPane.showMessageDialog(Main.this, "Du glömde: "+errorlog,
    							"Fel", JOptionPane.ERROR_MESSAGE); } 
    					else {
    					int age2 = Integer.parseInt(age);	
    					People p = new People(name, country, age2);
    					persons.add(p);
    					startnr++;
    					break;
    					}
    				}catch (NumberFormatException e) {
     
    					JOptionPane.showMessageDialog(Main.this, "Du måste skriva siffror",
    							"Fel", JOptionPane.ERROR_MESSAGE);
    				}
     
    			}
     
     
    		}
     
    	}
     
    	class ShowListener implements ActionListener {
    		public void actionPerformed(ActionEvent event) {
     
    		}
     
    	}
     
    	class TimeListener implements ActionListener {
    		public void actionPerformed(ActionEvent event) {
    			System.out.println(persons);
    		}
    	}
     
    	public static void main(String[] args) {
    		new Main();
    	}
     
    }


    --- Update ---

    I've done what you told me...it seems that the if (country.length()==0) statement doesnt work...because it saves the country variable even if the length is 0...
    Here is the system.out.print you told me to do if i type Name: Steve, Country: (empty), Age: 28

    Name/Namn: Steve
    Country/Land:
    Age/Ålder: 28

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Arraylits for handling errors?

    it saves the country variable even if the length is 0...
    Can you explain what you mean here when you say "saves"?

    When does the code in post #5 execute the JOPtionPane statement that shows the error message?
    What is the condition tested in the if statement that controls its execution?

    When do you want the error message to be displayed? Does the code do that?


    One problem with the code is that it is poorly formatted.
    There is a hidden } at the end of a line. All }s should be on a line by themselves and vertically inline below the start of the line that has the matching { so you can easily see the nesting level of the logic.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Nov 2012
    Location
    Stockholm
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Arraylits for handling errors?

    Quote Originally Posted by Norm View Post
    Can you explain what you mean here when you say "saves"?

    When does the code in post #5 execute the JOPtionPane statement that shows the error message?
    What is the condition tested in the if statement that controls its execution?

    When do you want the error message to be displayed? Does the code do that?
    By saves i mean creates a object of the class People and adds that to the array persons.

    Im really appreciating your help. Im going to record a video that illustrates the problem

    EDIT: http://www.youtube.com/watch?v=3pT9Z...ature=youtu.be

    Tell me if the quality is to bad, i think you get the idea.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Arraylits for handling errors?

    Did you miss this?
    One problem with the code is that it is poorly formatted.
    There is a hidden } at the end of a line. All }s should be on a line by themselves and vertically inline below the start of the line that has the matching { so you can easily see the nesting level of the logic.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Nov 2012
    Location
    Stockholm
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Arraylits for handling errors?

    Oh...ok, could this be the problem? What line is this?
    Here is the video illustrating the problem:

    Problem - YouTube

    Change to HD to see text better!

  10. #10
    Junior Member
    Join Date
    Nov 2012
    Location
    Stockholm
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Arraylits for handling errors?

    Hmmm..i cant seem to post new posts anymore...
    Trying again:

    Problem - YouTube

  11. #11
    Junior Member
    Join Date
    Nov 2012
    Location
    Stockholm
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Arraylits for handling errors?

    by save i mean create object of the class people and add to the persons arraylist.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Arraylits for handling errors?

    Did you see my posts #6 & #8? Did you fix your code as suggested there?

    What about the questions I asked in post #6?
    When does the code in post #5 execute the JOPtionPane statement that shows the error message?
    What is the condition tested in the if statement that controls its execution?

    When do you want the error message to be displayed? Does the code do that?
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Nov 2012
    Location
    Stockholm
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Arraylits for handling errors?

    No, I cant find that bracket. What line is it?

    When does the code in post #5 execute the JOPtionPane statement that shows the error message?
    When user press OK (ie. when svar is = OK_OPTION).

    What is the condition tested in the if statement that controls its execution?
    I guess i just answered this, or didnt I? My english is not that good.

    When do you want the error message to be displayed? Does the code do that?
    When any field is left empty. Yes, it works sometimes and sometimes not. See post#3.

    --- Update ---

    I've tried the code a little bit more now.
    It seems that aslong as i enter an age. The program doesnt care about the two fields above...

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Arraylits for handling errors?

    When any field is left empty
    How can you determine that? What fields/variables can be tested in an if statement to know if any field has been left empty?

    No, I cant find that bracket.
    Use your IDE's Find function. Find each } in turn. Move any that is not on a line by itself to be on a separate line by itself aligned below the start of the line with the matching {.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 3
    Last Post: March 6th, 2012, 03:50 AM
  2. [SOLVED] exception handling
    By Topflyt in forum Exceptions
    Replies: 5
    Last Post: December 22nd, 2011, 12:00 PM
  3. [SOLVED] Exception Handling
    By Melawe in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 21st, 2011, 07:39 PM
  4. [SOLVED] Handling Errors / calling Error-Catching Methods
    By movsesinator in forum Object Oriented Programming
    Replies: 4
    Last Post: April 6th, 2010, 05:35 AM
  5. Exception handling
    By AnithaBabu1 in forum Exceptions
    Replies: 6
    Last Post: August 27th, 2008, 09:37 AM