Results 1 to 4 of 4
  1. #1
    Michael2 is offline Member
    Join Date
    Jul 2012
    Posts
    7
    Rep Power
    0

    Default coding error-please help

    Here's the code:

    class AddUpIntegers
    {
    public static void main (String[] args )
    {
    Scanner scan = new Scanner( System.in );
    int N, sumAll = 0, sumEven = 0, sumOdd = 0;

    System.out.print( "Enter limit value: " );
    N = scan.nextInt();

    int count = 1;
    while ( count <= N )
    {
    sumAll = sumAll + count ;

    if ( count % 2 == 0 )
    sumEven = sumEven + count ;

    else
    sumOdd = sumOdd + count;

    count = count + 1 ;
    }

    System.out.print ( "Sum of all : " + sumAll );
    System.out.print ( "\tSum of even: " + sumEven );
    System.out.println( "\tSum of odd : " + sumOdd );
    }
    }

    I'm running Java 6u7 on Win XP 5.1. I'm getting 2 errors in the line that reads Scanner scan = new Scanner( System.in );

    I'd paste the screenshot but I don't know how. Basically, there are carrots below the capital S in scanner (both of them). I tried adding dots, deleting blank spaces, and even cutting out the word "new" but none of those things worked. I copied this directly from my text so I don't understand why it isn't working. Please tell me what I need to do.

  2. #2
    Zyril is offline Senior Member
    Join Date
    Oct 2011
    Location
    Sweden
    Posts
    124
    Rep Power
    0

    Default Re: coding error-please help

    You are not saying what type of errors you are receiving but are you sure you have imported the Scanner object from java.util?

    Java Code:
    import java.util.Scanner;
    This is essential to make the code run. What IDE are you using?

    Here is working code:
    Java Code:
    import java.util.Scanner;
    
    class AddUpIntegers{
    	
    	 public static void main (String[] args ){
    		 
    		 Scanner scan = new Scanner( System.in );
    		 int N, sumAll = 0, sumEven = 0, sumOdd = 0;
    		
    		 System.out.print( "Enter limit value: " );
    		 N = scan.nextInt();
    		
    		 int count = 1;
    		 while ( count <= N ){
    			 
    			 sumAll = sumAll + count ;
    			
    			 if ( count % 2 == 0 )
    			 sumEven = sumEven + count ;
    			
    			 else
    			 sumOdd = sumOdd + count;
    			
    			 count = count + 1 ;
    		 }
    
    		 System.out.print ( "Sum of all : " + sumAll );
    		 System.out.print ( "\tSum of even: " + sumEven );
    		 System.out.println( "\tSum of odd : " + sumOdd );
    		 
    	 }
     }

  3. #3
    Michael2 is offline Member
    Join Date
    Jul 2012
    Posts
    7
    Rep Power
    0

    Default Re: coding error-please help

    Thanks, Zyr. That worked. I can't believe I didn't get that part. Lesson learned.

  4. #4
    Zyril is offline Senior Member
    Join Date
    Oct 2011
    Location
    Sweden
    Posts
    124
    Rep Power
    0

    Default Re: coding error-please help

    You are saying you get red "carrots" below your code, signaling to you that there is something wrong with it. What program are you coding in? Eclipse or Netbeans will tell you what is wrong with the code, and also suggest solutions to make the code compile. If you are not using one of these programs, I suggest you look into it.

    One can always argue that writing code in Notepad is a better way to go since you have to learn more stuff the hard way, but I for one have progressed my learning so much faster by getting the built in help from Eclipse.

Similar Threads

  1. A newbie to Java needing help on coding error
    By Turo in forum New To Java
    Replies: 5
    Last Post: 08-28-2011, 06:11 AM
  2. Error in coding
    By bamagirl31 in forum New To Java
    Replies: 3
    Last Post: 05-11-2011, 07:31 PM
  3. Replies: 4
    Last Post: 10-25-2010, 04:49 PM
  4. coding help
    By accies76 in forum New To Java
    Replies: 5
    Last Post: 11-12-2008, 09:15 PM
  5. Error in my coding
    By one198 in forum New To Java
    Replies: 2
    Last Post: 10-13-2007, 06:07 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
  •