Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By santa

Thread: If Else Statement in While Loop

  1. #1
    rockintyler is offline Member
    Join Date
    Feb 2012
    Posts
    5
    Rep Power
    0

    Default If Else Statement in While Loop

    Hello,

    I'm in a Java programming course for college and can't figure out how to get an if else statement to work in a while loop. The goal is to read some numbers from a text file called data.txt file and split them into even and odds. I'm then supposed to add the even numbers with the evens and the odd numbers with all the other odd numbers then output the result (I know how to output).

    Like I said I just can NOT figure out how to use an if else statement in a while loop (supposed to use an EOF loop) to split them up. If there's a better way on how to do this please let me know.

    Thanks for any help and if more information is needed to help please say so.

    Here's the code I have:
    Java Code:
    public static void Problem3(){
    	int sumeven = 0, sumodd=0;
    	String even, odd;
    	
    	try{
    	FileReader fr= new FileReader("data2");	
    	Scanner sc = new Scanner(fr);
    	
    	while (sc.hasNextInt())
    	{if (sc.nextInt()%2==0);
    	sumeven+=sumeven;	
    	else
    		sumodd+=sumodd;}
    	
    	
    	}//end try
    	catch (Exception e){
    		System.out.println(e.getMessage());	
    	}//end catch
    		
    	}//end of problem 3

    Note: I receive an error from the code "else" saying to delete this token.

  2. #2
    santa's Avatar
    santa is offline Senior Member
    Join Date
    Nov 2009
    Location
    Sweden
    Posts
    208
    Rep Power
    5

    Default Re: If Else Statement in While Loop

    Remove the ; after the if statement... line 10
    Last edited by santa; 02-23-2012 at 11:30 PM.
    Fubarable likes this.

  3. #3
    rockintyler is offline Member
    Join Date
    Feb 2012
    Posts
    5
    Rep Power
    0

    Default Re: If Else Statement in While Loop

    Thanks santa. Problem solved.

    I still can't believe I overlook small things like that still I've been looking at this code for about 1 hour now trying to figure it out.

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

Similar Threads

  1. problem with if statement in while loop of result set
    By kishan.java in forum New To Java
    Replies: 6
    Last Post: 04-09-2011, 04:07 PM
  2. Help with loop statement
    By arvind1508 in forum New To Java
    Replies: 2
    Last Post: 02-23-2011, 04:39 PM
  3. Vote - Favorite Loop Statement
    By angryboy in forum New To Java
    Replies: 24
    Last Post: 02-15-2009, 08:15 PM
  4. Need help with a loop statement
    By sunshine39 in forum New To Java
    Replies: 7
    Last Post: 11-03-2008, 03:42 AM
  5. Least To Greates[ if statement and for loop]
    By kris09 in forum New To Java
    Replies: 1
    Last Post: 08-08-2008, 06:34 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
  •