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

Thread: Newbie needs help with program assignment

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

    Default Newbie needs help with program assignment

    I have to write a program that reads from a file. The file has 15 sets of numbers per line and 10 lines. I then have to output the total of all numbers in each line as well as sum of all even numbers and sum of all odd numbers. Also have to output how many zeros in each line. I have the program totaling the lines, and know the if statement to use to find even, odd, as well as zeros. the issue is that i assigned each number in a row to 15 different variables and for the life of me cant figure out how to use if else statement to seperate the even, odd and zeros. I got so frustrated that i did 15 different if else statements, but got borked results after first line. I hope i havent confused you all worse than i am.
    here is my code. it is in the building stage so ignore some of the variables and comments. the output is perfect as for string minipulation that the teacher wants, just need to get the logic right

    Java Code:
    //********************************************************************
    //  Lincoln.java       Java Foundations
    //
    //  Demonstrates the basic structure of a Java application.
    //********************************************************************
    import java.util.Scanner;
    import java.io.*;
    
    public class Numbers
    {
       //-----------------------------------------------------------------
       //  Prints a presidential quote.
       //-----------------------------------------------------------------
       public static void main (String[] args) throws IOException
       {
          int num1,num2,num3,num4,num5,num6,num7,num8,num9,num10,num11,num12,num13,num14,num15;
    	  int total, count, even, odd, zeros, countz, notTab;
          String str;
    
          Scanner fileScan, lineScan;
    
    fileScan = new Scanner (new File("Numbers.txt"));
    count = 0;
    countz = 0;
    
    
    
          while (fileScan.hasNext())
          {
    		   even = 0;
    		    odd = 0;
    
    		  str = fileScan.nextLine();
    
    
    		  lineScan = new Scanner(str);
    		  lineScan.useDelimiter(" ");
    
    
    
    		  num1 = lineScan.nextInt();
    		  num2 = lineScan.nextInt();
    		  num3 = lineScan.nextInt();
    		  num4 = lineScan.nextInt();
    		  num5 = lineScan.nextInt();
    		  num6 = lineScan.nextInt();
    		  num7 = lineScan.nextInt();
    		  num8 = lineScan.nextInt();
    		  num9 = lineScan.nextInt();
    		  num10 = lineScan.nextInt();
    		  num11 = lineScan.nextInt();
    		  num12 = lineScan.nextInt();
    		  num13 = lineScan.nextInt();
    		  num14 = lineScan.nextInt();
    		  num15 = lineScan.nextInt();
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    		 total = num1 + num2 + num3 + num4 + num5 + num6 + num7 + num8 +
    		 num9 + num10 + num11 + num12 + num13 + num14 + num15;
    
    		 count++;
    		 {
    		 if (odd > 9 || odd < 0)
    			System.out.println("Record: " + count + "\tTotal: " + total + "\tEvens: " +
    		    even + "\tOdds: " + odd + "\tZeros: " + countz);
    		 else
    		    System.out.println("Record: " + count + "\tTotal: " + total + "\tEvens: " +
    		    even + "\tOdds: " + odd + "\t\tZeros: " + countz);
    
    
    		}
    
    
    	}
       }
    }
    Last edited by Norm; 02-25-2012 at 05:17 PM. Reason: added code tags

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Newbie needs help with program assignment

    Please explain what your problem is?

    Do you know about arrays? Your program looks like the perfect play to use arrays.

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

    Default Re: Newbie needs help with program assignment

    I know a little about arrays but we havent got that far yet

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Newbie needs help with program assignment

    That's strange. I don't know why you'd get this assignment that requires using an array.
    Having 15 variables with names like you have done is not the way programs are written.
    DarrylBurke likes this.

Similar Threads

  1. My assignment array program will not run
    By BenH in forum New To Java
    Replies: 1
    Last Post: 01-29-2012, 02:00 AM
  2. newbie needs help in GUI assignment
    By creedkorn in forum New To Java
    Replies: 19
    Last Post: 04-19-2011, 10:11 PM
  3. While Loop Problem with Java Assignment Program
    By welsh_rocker in forum New To Java
    Replies: 9
    Last Post: 01-12-2011, 01:55 PM
  4. First Assignment and already stuck....Newbie Question
    By Danieldcc in forum New To Java
    Replies: 5
    Last Post: 09-24-2010, 11:44 PM
  5. Help with my first program...newbie
    By giganews35 in forum New To Java
    Replies: 6
    Last Post: 12-01-2008, 07:16 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
  •