Results 1 to 10 of 10
  1. #1
    enjoy2225 is offline Member
    Join Date
    Oct 2012
    Posts
    7
    Rep Power
    0

    Default Help with while loops

    I have to create a program that uses a scanner to read a .txt file that has a bunch of numbers in it. The goal is to read these numbers, and add up all the even numbers and add up all the odd numbers. I am supposed to use while loops and I am brand new to java, so I don't know where to go from here. Any help?

    Java Code:
    import java.util.Scanner;
    import java.io.*;
    
    
    public class EvenAndOdd
    
    {
       public static void main(String[] args) throws IOException 
    	
        {  
            Scanner inFile = new Scanner(new FileReader("numbers.txt"));  
              
            while (scanner.hasNextLine())  
                System.out.println(scanner.nextLine());
    				
    				int evenNum = 0;
    				int oddNum = 0;
    				
    				if(mod 2 = 0, then = (evenNum);
    				else (oddNum);// What do you do here?
        }  
    }

  2. #2
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    384
    Rep Power
    4

    Default Re: Help with while loops

    You're on the right path already. You use % for mod though (so 3%2==1). Also, have a look at the Integer class for help with converting text to a number.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Help with while loops

    Quote Originally Posted by enjoy2225 View Post
    I have to create a program that uses a scanner to read a .txt file that has a bunch of numbers in it. The goal is to read these numbers, and add up all the even numbers and add up all the odd numbers. I am supposed to use while loops and I am brand new to java, so I don't know where to go from here. Any help?
    Please have a look here: While Loop Tutorial

  4. #4
    SJF
    SJF is offline Senior Member
    Join Date
    Oct 2012
    Posts
    108
    Rep Power
    0

    Default Re: Help with while loops

    Quote Originally Posted by enjoy2225 View Post
    if(mod 2 = 0, then = (evenNum);
    else (oddNum);// What do you do here?
    This is pseudo-code, not Java. This is where your instructor expects you to do some coding.

    '%' is Java modulo. You should have specific questions, not "do my homework for me". If you don't know where to start, that's what T.A.s are for.

  5. #5
    enjoy2225 is offline Member
    Join Date
    Oct 2012
    Posts
    7
    Rep Power
    0

    Default Re: Help with while loops

    I understand this is pseudo-code. I am really new to Java. What I CAN do here is have the scanner look through the numbers.txt file and it compiles and runs and I see all the numbers on the bottom of JGrasp. So I guess that I got that part working. Now I need to have it process each number, determine if it's even, if it is, add it to the evenNum variable, and if it is odd, add it to the oddNum variable. I can't find the information on how to do THAT. I understand the math behind it, if it's remainder when divided by 2 = 0, then it is even. If not it is odd. I don't know what the code syntax should be.

  6. #6
    SJF
    SJF is offline Senior Member
    Join Date
    Oct 2012
    Posts
    108
    Rep Power
    0

    Default Re: Help with while loops

    Java Code:
      if( /*BOOLEAN OPERATION*/ ) {
         //Conditional if stuff  ie. x++, x+= newNum, etc.
      }
      else if( /*ANOTHER CONDITION*/){
        //Stuff to do it that happens
      }
      else{
        //Conditional else stuff
      }
    The if-then and if-then-else Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)

    I don't mean to sound snarky. Your while loop was fine. You need to get the integer out of the file... look at Integer.parseInt() Integer (Java Platform SE 7 )

    You're reading the file and printing it directly to stdout, you need to capture the read line somehow.

  7. #7
    enjoy2225 is offline Member
    Join Date
    Oct 2012
    Posts
    7
    Rep Power
    0

    Default Re: Help with while loops

    after some help from a classmate, I have gotten this far.

    Java Code:
    import java.util.*;
    import java.io.*;
    
    
    public class EvenAndOdd
    
    {
       public static void main(String[] args) throws FileNotFoundException
    	
    		{  
            Scanner inFile = new Scanner(new FileReader("integers.txt"));
             
    		  int evenNum = 0;
    		  int oddNum = 0;	 
    		  int num; 
    			 
            while (inFile.hasNext())
    				
    				
    			if(num % 2 == 0)
    				evenNum = evenNum + num;
    			else
    				oddNum = oddNum + num;
    				
    			num = inFile.nextInt();
    		} 
    
    		System.out.println("Even sum: " + evenNum);//What's going on here? I get an error message
    		System.out.println("Odd sum: " + oddNum);
        }
    }

  8. #8
    SJF
    SJF is offline Senior Member
    Join Date
    Oct 2012
    Posts
    108
    Rep Power
    0

    Default Re: Help with while loops

    What's the error? (I'd start by looking at what the value of num is the first iteration of the loop)
    Also, missing an open brace '{' for your while block now.

  9. #9
    enjoy2225 is offline Member
    Join Date
    Oct 2012
    Posts
    7
    Rep Power
    0

    Default Re: Help with while loops

    the error message says <identifier> expected pointing at the first parentheses in that line.

  10. #10
    enjoy2225 is offline Member
    Join Date
    Oct 2012
    Posts
    7
    Rep Power
    0

    Default Re: Help with while loops

    Got it to work like this.

    Java Code:
    import java.util.*;
    import java.io.*;
    
    
    public class EvenAndOdd
    
    {
       public static void main(String[] args) throws FileNotFoundException
    	
    		{  
            Scanner inFile = new Scanner(new FileReader("integers.txt"));
    		  int evenNum = 0;
    		  int oddNum = 0;	 
    		  int num; 
    		
    		  num = inFile.nextInt();
    		  
            while (inFile.hasNext())
    			{	
    				
    			if(num % 2 == 0)
    				evenNum = evenNum + num;
    			else
    				oddNum = oddNum + num;
    				
    			num = inFile.nextInt();
    			} 
    
    		System.out.println("Even sum: " + evenNum);
    		System.out.println("Odd sum: " + oddNum);
        }
    }

Similar Threads

  1. Need help with while loops
    By tangel in forum New To Java
    Replies: 0
    Last Post: 10-04-2011, 04:37 AM
  2. For Loops
    By Shyamz1 in forum New To Java
    Replies: 4
    Last Post: 09-27-2011, 09:41 PM
  3. Replies: 20
    Last Post: 03-22-2011, 04:02 AM
  4. Help with Loops
    By Spyderpig in forum New To Java
    Replies: 10
    Last Post: 02-17-2011, 08:10 AM
  5. need some help with loops!
    By Chewart in forum New To Java
    Replies: 2
    Last Post: 12-03-2009, 11:32 PM

Posting Permissions

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