Java Help


Joined
May 12, 2006
Messages
347
Age
39
Location
Killeen, TX
Website
Visit site
I'm pretty new to programming and I don't really frequent many forums where I can get this kind of help.

I can't for the life of me figure out why the println statements aren't working. It compiles fine.

Code:
//Michael Justman
//IT210
//Module3: Programming Assignment 3 - Problem 1
class PA3_prob1
	{
	public static void main(String args[]) {}
		Integer n;
		Integer rtotal;
		{
			int n = (int)Math.floor(Math.random()*100000+1);
			String randomString = String.valueOf(n);
			for (int i=0;i<randomString.length();i++)
				{
					char c = randomString.charAt(i);
					n = Character.valueOf(c);
					rtotal =+ n;
				}
		
			System.out.println ("Random: " + randomString);
			System.out.println ("Total:  " + rtotal);
		}
	}
 
1) I don't think you need the {} at the end of the first line of the main method definition. Just a single { to open the code block for the method body.

2) I'm not sure why you have put part of your method in a {} block.

3) I'm really quite surprised this compiles - looks to me like you have an empty method body for main, some fields declared with default scope within the class and then a block of code sitting in there. I didn't know you could just dump a block of code in like that.

If I'm right about (3) then your main method is running but it is empty and so does nothing.
 
This is I think what you actually wanted to do and how I'd probably write it:

Code:
      //Michael Justman
      //IT210
      //Module3: Programming Assignment 3 - Problem 1
      //Modified by Peter_R @ gp32x.de
      class PA3_prob1_fixed {
              public static void main(String args[]) {
                      String randomString = String.valueOf((int) Math.floor(Math.random()*100000+1));
                      int rtotal = 0;
      
                      for(int i=0;i<randomString.length();i++) {
                          rtotal =+ Character.valueOf(randomString.charAt(i));
                      }
                      
                      System.out.println ("Random: " + randomString);
                      System.out.println ("Total:  " + rtotal);
              }
      }
 
Last edited by a moderator:
I appreciate it. It certainly doesn't help that I'm "in the field" for the US Army and I'm trying to do my programming homework in nano over ssh on my Android phone, because I can't install the JDK on my work computer. Haven't had a chance to look at it again yet, but again, thank you.
 
Justman said:
because I can't install the JDK on my work computer.
You might want to check this : http://forums.sun.com/thread.jspa?threadID=5211728

also google for "portable JDK"
 
Last edited by a moderator:
Back
Top