Java problem

Can anybotdy see anything wrong with this Java code?

Page 1 of 1

4 Replies - 551 Views - Last Post: 05 March 2009 - 05:50 PM Rate Topic: -----

#1 dank145  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 05-March 09

Java problem

Posted 05 March 2009 - 05:13 PM

Hi people,

I'm very much a novice with regards to Java and am having trouble with the following code:

public class HelloClient extends JFrame  {
	
 public static void main (String [] args) {


	// get new can value from admin
	String CanInput = JOptionPane.showInputDialog("Enter a new value for a Can:");
	// check if admin input is numeric
		if (CanInput.matches("[\\d.]+")) {
			// Convert to Integer
			int CanValue = Integer.parseInt(CanInput);
		}
		else {
			// else through GUI error box
			JOptionPane.showMessageDialog(null, "Warning", "The value you entered for \"Can\" is invalid, please ensure you input an integer value.", JOptionPane.ERROR_MESSAGE);
		}
		
		System.out.println("your cans value is" + CanValue);
}
}



For some reason it will not recognise CanValue when trying to print it out, is there anything blatantly wring with t his?

Any insight would be much much appreciated

Cheers.

Is This A Good Question/Topic? 0
  • +

Replies To: Java problem

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,904
  • Joined: 06-March 08

Re: Java problem

Posted 05 March 2009 - 05:20 PM

View Postdank145, on 5 Mar, 2009 - 04:13 PM, said:

Hi people,

I'm very much a novice with regards to Java and am having trouble with the following code:

public class HelloClient extends JFrame  {
	
 public static void main (String [] args) {


	// get new can value from admin
	String CanInput = JOptionPane.showInputDialog("Enter a new value for a Can:");
	// check if admin input is numeric
		if (CanInput.matches("[\\d.]+")) {
			// Convert to Integer
			int CanValue = Integer.parseInt(CanInput);
		}
		else {
			// else through GUI error box
			JOptionPane.showMessageDialog(null, "Warning", "The value you entered for \"Can\" is invalid, please ensure you input an integer value.", JOptionPane.ERROR_MESSAGE);
		}
		
		System.out.println("your cans value is" + CanValue);
}
}



For some reason it will not recognise CanValue when trying to print it out, is there anything blatantly wring with t his?

Any insight would be much much appreciated

Cheers.


Variables declared exist only between the enclosing {}
So
		if (CanInput.matches("[\\d.]+")) {
			// Convert to Integer
			int CanValue = Integer.parseInt(CanInput);	// CanValue starts to exist here
		}	  // CanValue disappears here



you will have to move it out of the {}

		   int CanValue;
		if (CanInput.matches("[\\d.]+")) {
			// Convert to Integer
			CanValue = Integer.parseInt(CanInput);
		}
	   // can see CanValue
		System.out.println("your cans value is" + CanValue);


Was This Post Helpful? 0
  • +
  • -

#3 dank145  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 05-March 09

Re: Java problem

Posted 05 March 2009 - 05:36 PM

Ahh I see, you can tell I'm a novice, that code you have provided works fine and the CanValue is recognised but if I include the else statement and then try and print it out it is no longer recognised?

Any ideas?
Was This Post Helpful? 0
  • +
  • -

#4 Nat3TheGreat13  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 162
  • Joined: 09-February 09

Re: Java problem

Posted 05 March 2009 - 05:40 PM

Put it inside the if statement
 if (CanInput.matches("[\\d.]+")) 
{
			// Convert to Integer
		   CanValue = Integer.parseInt(CanInput);
		System.out.println("your cans value is" + CanValue);
  }


Was This Post Helpful? 0
  • +
  • -

#5 dank145  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 05-March 09

Re: Java problem

Posted 05 March 2009 - 05:50 PM

Ah, thats the problem its not actually going to be printed out, I was just using that as an example, I will actually be needing to use it as an argument for a function later down the code!! Is this possible, I am so used to programming in PHP with all its ' I don't care about data types and the like'. Java makes things much more difficult :-|

This post has been edited by dank145: 05 March 2009 - 05:52 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1