Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: program reads file and prints runs of the same numbers in the form (number of times * number)

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default program reads file and prints runs of the same numbers in the form (number of times * number)

    so i need the program to read a file with a list of ints, if there are consecutive equal numbers, such as 10 20 20 20 20 30 30 ... 0, the output should be
    (1 * 10)
    (4 * 20)
    (2 * 30)
    etc, and it will end when a zero is reached

    however im getting an error due to my else if statement and i dont know why?
    can anyone help?

    would an arraylist make i easier?

    import java.util.*;
    import java.io.*;
     
    public class Proj57015 {
     
    public static void main (String [] args) throws FileNotFoundException{
    System.out.println("");
    System.out.println("CMSC 255 - Proj#5 - Jimmy Whitten"); 
     
    String filename = args[0];
    System.out.println("Data File: " + filename);
    System.out.println("");
    Scanner filescan = new Scanner(new File(filename));
     
    int num1 = filescan.nextInt();
    int count = 0;
     
    while (filescan.hasNext()){
    if (num1 == filescan.nextInt())
    count++;
    else if (num1 != filescan.nextInt())
    num1 = filescan.nextInt();
     
    }
    System.out.println("(" + count + "*" + num1 + ")");
     
     
    }
    }

    --- Update ---

    error is:
    exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at Proj57015.main(Proj57015.java:27)
    Last edited by jps; November 8th, 2012 at 06:09 AM. Reason: fixed code tag


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: program reads file and prints runs of the same numbers in the form (number of times * number)

    When using a Scanner object, there should always be a symmetry if you will between your call to hasNextXXX() and the call to nextXXX(). So if you are call while (myScanner.hasNextFoo()) { inside of that loop there should only be one call to extract information out with the Scanner and it should be trying to extract the same type of information that the hasNext checked, here the fake method myScanner.nextFoo().

    In your code above you check for filescan.hasNext(), which checks the scanner for *any* type token. If you do that, then inside of the loop there needs to be only *one* call to next(). If you want to check for and extract ints, then you should use while (filescan.hasNextInt()), and then call filescan.nextInt() only *once* inside of the block.

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: program reads file and prints runs of the same numbers in the form (number of times * number)

    ok that makes sense, but then how would i go about assigning the next number of a sequence if i cant call .nextInt again?

  4. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: program reads file and prints runs of the same numbers in the form (number of times * number)

    You store it in a variable that has been declared before the loop.

    Please edit your post and change your [highlight] tags (where'd you get that?) to the proper [code=java] [/code] tags.

  5. #5
    Junior Member
    Join Date
    Nov 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: program reads file and prints runs of the same numbers in the form (number of times * number)

    sorry, i was wondering why that wasnt working, it said to do that in the "announcements - read if its your first time"

    import java.util.*;
    import java.io.*;
     
    public class Proj57015 {
     
    	public static void main (String [] args) throws FileNotFoundException{
     
    		String filename = args[0];
    		System.out.println("Data File: " + filename);
    		System.out.println("");
    		Scanner filescan = new Scanner(new File(filename));
     
    		int num1 = filescan.nextInt();
    		int count = 0;
     
    		while (filescan.hasNext()){
    			if (num1 == filescan.nextInt())
    				count++;
    			else if (num1 != filescan.nextInt())
    				num1 = filescan.nextInt();
     
    		}
    		System.out.println("(" + count + "*" + num1 + ")");
     
     
    	}
    }

    but if i declared a new int variable outside the loop i couldnt initialize it as anything because id have to use the .nextInt to find when the next run of numbers starts, so wouldnt it just assign the nextInt after my num1 variable?
    Last edited by jslice3; November 8th, 2012 at 02:40 AM.

  6. #6
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: program reads file and prints runs of the same numbers in the form (number of times * number)

    Again, call filescan.nextInt() just *once* inside of the while loop. If this were my code, I'd call the method once and put it into an int variable, and then I could use the int held in the variable as many times as I want.

  7. #7
    Junior Member
    Join Date
    Nov 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: program reads file and prints runs of the same numbers in the form (number of times * number)

    so something like this?

    import java.util.*;
    import java.io.*;
     
    public class Proj57015 {
     
    	public static void main (String [] args) throws FileNotFoundException{ 
     
    		String filename = args[0];
    		System.out.println("Data File: " + filename);
    		System.out.println("");
    		Scanner filescan = new Scanner(new File(filename));
     
    		int num1 = filescan.nextInt(), num2;
    		int count = 0;
     
    		while (filescan.hasNextInt()){
    			num2 = filescan.nextInt();
    			if (num1 == num2)
    				count++;
    			else 
    				...
     
    		}
    		System.out.println("(" + count + "*" + num1 + ")");
    	}
    }
    Last edited by jslice3; November 8th, 2012 at 02:39 AM.

  8. #8
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: program reads file and prints runs of the same numbers in the form (number of times * number)

    Better for you to test your code yourself. Does the code compile? Does the code work? Does it throw exceptions? Also, please don't double-post your questions as that's not playing fair by the forum rules.

  9. #9
    Junior Member
    Join Date
    Nov 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: program reads file and prints runs of the same numbers in the form (number of times * number)

    oh yeah I did, and am still working on it, i was just trying to get a visual layout of what you were saying

  10. #10
    Junior Member
    Join Date
    Nov 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: program reads file and prints runs of the same numbers in the form (number of times * number)

    my input file is ten 20s, five 50s, five 60s, one 20, five 30s, and a 0 in a row
    but my output is
    (1 * 50)
    (1 * 60)
    (1 * 20)
    (1 * 30)
    (0)

    ive read over my code multiple times and still dont know why im getting incorrect results,
    maybe im just missing a simple mistake, but
    any suggestions would be greatly appreciated

    import java.util.*;
    import java.io.*;
     
    public class Proj57015 {
     
    	public static void main (String [] args) throws FileNotFoundException{
     
    		String filename = args[0];
    		System.out.println("Data File: " + filename);
    		System.out.println("");
    		Scanner filescan = new Scanner(new File(filename));
     
    		int num1 = filescan.nextInt(), num2;
    		int count = 1;
     
    		while (filescan.hasNextInt()){
    			num2 = filescan.nextInt();
    			if(num1 == num2)
    				count++;
    			else if(num2 == 0){
    				System.out.println("(0)");
    				System.exit(0);
    			}
    			else if(num1 != num2){
    				count = 1;
    				num1 = num2;
    				System.out.println("(" + count + "*" + num1 + ")");
    			}
    		}
    	}
    }

  11. #11
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: program reads file and prints runs of the same numbers in the form (number of times * number)

    It looks like that code will print something almost every time through the loop.
    I would think you need to do all of the counting first so you know what to print, and then print the totals.
    It does not look possible to get an accurate count with the current design. Write down a series of steps to take to solve the problem and then write code to perform the steps.

Similar Threads

  1. Replies: 2
    Last Post: November 7th, 2012, 10:45 PM
  2. number of times do we need to connect ?
    By bora.pradnya in forum JDBC & Databases
    Replies: 1
    Last Post: January 6th, 2012, 08:16 AM
  3. Replies: 2
    Last Post: November 30th, 2011, 07:35 PM
  4. Help with code that prints number of evens, odds, and 0's
    By trogan234 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 2nd, 2011, 06:50 PM
  5. A program that reads in secounds, and prints out hours minutes
    By CYKO in forum Java Theory & Questions
    Replies: 1
    Last Post: September 13th, 2009, 10:42 PM

Tags for this Thread