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 3 of 3

Thread: programs 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 programs 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?

    [highlight = Java]
    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 + ")");


    }
    }
    [/highlight]

    --- 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)


  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: programs reads file and prints runs of the same numbers in the form (number of times * number)

    Please don't repost your question multiple times in the forum. Your unnecessarily dividing the discussion is not fair to the volunteers who help out here and is not appreciated.

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

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

    Duplicate of http://www.javaprogrammingforums.com...-*-number.html

    Thread locked

Similar Threads

  1. 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
  2. Replies: 3
    Last Post: October 19th, 2011, 02:17 PM
  3. 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
  4. Replies: 3
    Last Post: January 28th, 2011, 09:37 AM
  5. Replies: 1
    Last Post: October 16th, 2010, 03:32 PM

Tags for this Thread