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

Thread: 2 Lines Stuffs.

  1. #1
    Member
    Join Date
    Apr 2012
    Posts
    60
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Default 2 Lines Stuffs.

    So, i'm doing this program that takes 2 lines of input, the first line being a number that signifies how many numbers are going to be in the next line. I then have to find which number in the second line occurs the most often. Sounds simple. However, it is giving me problems. I am thinking with the whole read 2 lines thing. I thought I knew how, but my method of doing it didnt work as I thought it should, and ended up in not knowing when to stop collecting data from the scanner in the loop, even though I told it when. Buttt anyway, I looked for solutions online and came upon BufferedReader, which I know nothing about but looked it up and gave it a try. Now I am stuck with this mess of code that I cannot make sense of. The number it prints for occurring the most is 0, even when 0 is not in the second line. I do not know where to go from here as I cant find where the problem lies. I am really hoping for someone to just suggest a different solution to read the input. Unless you can fix this. I'll shut up and post the code now.

    PS: even though it says if it reads "e" to stop, it really stops after you hit enter twice. If I change this it does not work the same. I have no idea why.

    import java.util.*;
    import java.io.*;
    public class SandwichCounting
    {
    public static void main(String [] args) throws IOException
    {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
     
        System.out.println("Please enter the total number of orders, hit enter, then list the order numbers with spaces inbetween each one, then press enter twice when done.: ");
     
        int num = in.read();
        int [] list = new int [num];
     
        int n = 0;
        int[] number = new int [num];
        int [] ammount = new int [num];
        while (in.ready())
        {
        if (in.readLine().equalsIgnoreCase("e"))
            break;
     
        list[n] = in.read();
        n++;
        }
        for(int i = 0; i < num -1; i++)
        {
        for(int k = i+1; k < num -1; k++)
        {
            if(list[i] == list[k])
            {
                number[i] = list[i];
            }
        }
     
        for (int p = 0; p < num; p++)
        {
            if(number[i] == list[p])
            ammount[i]++;
        } 
       }
     
       int order = 0;
       int temp = ammount[0];
     
       for (int  i = 0; i < ammount.length; i++)
       {
           if(ammount[i] > temp)
           temp = ammount[i];
     
           order = number[i];
        }
     
        System.out.println("The most popular sandwich was #" + order);
    }
    }


  2. #2
    Member
    Join Date
    Apr 2012
    Posts
    160
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Default Re: 2 Lines Stuffs.

    Couldn't you simply just store the entire line in a String object, tokenize the string, parse each token into an integer, then determine the number of times each number occurs?

  3. The Following User Says Thank You to Parranoia For This Useful Post:

    Saintroi (May 9th, 2012)

  4. #3
    Member
    Join Date
    Apr 2012
    Posts
    60
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Default Re: 2 Lines Stuffs.

    Possibly so, but how do you tokenize a string? Ive only been doing Java since January, so there's a lot I don't know about! Haha.

  5. #4
    Member
    Join Date
    Apr 2012
    Posts
    60
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Default Re: 2 Lines Stuffs.

    I'll go ahead and post this; this is the code I used at first and what I like to use, but it does not work like I need it to, as you'll see if you run it.

    import java.util.*;
    public class SandwichCounting
    {
    public static void main(String [] args)
    {
        Scanner in = new Scanner(System.in);
     
        System.out.println("Please enter the total number of orders, hit enter, then list the order numbers with spaces inbetween each one, then press enter twice when done.: ");
     
        int num = Integer.parseInt(in.next());
        String [] list1 = new String [num];
     
        int n = 0;
        int[] number = new int [num];
        int [] ammount = new int [num];
     
        while (in.hasNextLine())
        {
     
            if (in.next().equals(""))
            break;
     
            list1[n] = in.next();
            n++;
          }
     
        int [] list = new int [num];
     
        for (int i = 0; i < num; i++)
        {
            list[i] = Integer.parseInt(list1[i]);
        }
     
     
        for(int i = 0; i < num -1; i++)
        {
        for(int k = i+1; k < num -1; k++)
        {
            if(list[i] == list[k])
            {
                number[i] = list[i];
            }
        }
     
        for (int p = 0; p < num; p++)
        {
            if(number[i] == list[p])
            ammount[i]++;
        } 
       }
     
       int order = 0;
       int temp = ammount[0];
     
       for (int  i = 0; i < ammount.length; i++)
       {
           if(ammount[i] > temp)
           temp = ammount[i];
     
           order = number[i];
        }
     
        System.out.println("The most popular sandwich was #" + order);
    }
    }

  6. #5
    Member
    Join Date
    Apr 2012
    Posts
    160
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Default Re: 2 Lines Stuffs.

    Quote Originally Posted by Saintroi View Post
    Possibly so, but how do you tokenize a string? Ive only been doing Java since January, so there's a lot I don't know about! Haha.
    Simple. Use the StringTokenizer class
    StringTokenizer (Java 2 Platform SE v1.4.2)

  7. The Following User Says Thank You to Parranoia For This Useful Post:

    Saintroi (May 9th, 2012)

  8. #6
    Member
    Join Date
    Apr 2012
    Posts
    60
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Default Re: 2 Lines Stuffs.

    Thanks a ton! Even though that page said StringTokenizers were outdated and to use the String.split method, I took your advice and got it to read. and semi-work! It reads the lines now, but perhaps you can help me with potentially making a new design to actually do what the program needs to do. The system im using doesnt seem to be working. Which involves trying to go through and put the numbers that do occur more than once into an array, then figuring out how many times they occur. Then I have to print the number that occurs the most. I cant say I know whats wrong with mine, but after some println()s, it just seems like this system isnt going to work. Can you suggest another?

    import java.util.*;
    public class SandwichCounting
    {
    public static void main(String [] args)
    {
        Scanner in = new Scanner(System.in);
     
        System.out.println("Please enter the total number of orders, hit enter, then list the order numbers with spaces inbetween each one, then press enter when done.: ");
     
        int num = Integer.parseInt(in.next());
        String line = "";
        Scanner in2 = new Scanner(System.in);
        int[] number = new int [num];
        int [] ammount = new int [num];
        line = in2.nextLine(); 
     
     
          StringTokenizer st = new StringTokenizer(line);
        int [] list = new int [num];
     
        for (int i = 0; st.hasMoreTokens(); i++)
        {
            list[i] = Integer.parseInt(st.nextToken());
        }
     
     
        for(int i = 0; i < num; i++)
        {
        for(int k = i+1; k < num; k++)
        {
            if(list[i] == list[k])
            {
                number[i] = list[i];
            }
     
            System.out.println(number[i]);
        }
     
        for (int p = 0; p < num; p++)
        {
            if(number[i] == list[p])
            ammount[i]++;
        } 
       }
     
       int order = 0;
       int temp = ammount[0];
     
       for (int  i = 0; i < ammount.length; i++)
       {
           if(ammount[i] > temp)
           temp = ammount[i];
     
           order = number[i];
        }
     
        System.out.println("The most popular sandwich was #" + order);
    }
    }

    Thanks!

  9. #7
    Junior Member
    Join Date
    May 2012
    Posts
    11
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: 2 Lines Stuffs.

    while (in.ready())
        {
        if (in.readLine().equalsIgnoreCase("e"))
            break;
     
        list[n] = in.read();
        n++;
        }

    Why are you using in.ready() as a conditional, and why break it when "in.readLine().equalsIgnoreCase("e")"???

    Perhaps using num as a maximum, and count with a for loop?

  10. #8
    Member
    Join Date
    Apr 2012
    Posts
    160
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Default Re: 2 Lines Stuffs.

    Use a while loop to get the tokens instead.

    ArrayList<Integer> numbers = new ArrayList<Integer>();
     
    while(st.hasMoreTokens()) {
         numbers.add(Integer.parseInt(st.nextToken()));
    }

    Then you could store the values, along with their count in a Map

  11. The Following User Says Thank You to Parranoia For This Useful Post:

    Saintroi (May 9th, 2012)

  12. #9
    Member
    Join Date
    Apr 2012
    Posts
    60
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Default Re: 2 Lines Stuffs.

    That makes sense. I will try that as soon as I get a chance tomorrow. And squeakbox, ready was the conditional you have to use for a BufferedReader, and I intended for the user to enter e when done, since it didnt work when I tried to use enter. Even though I know the input is going to only be two lines. But anyway, that is old code. Ive changed the program a good bit since then, please look at my last post to get the most recent code.

  13. #10
    Member
    Join Date
    Apr 2012
    Posts
    60
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Default Re: 2 Lines Stuffs.

    Hmm... ive never used a map before, but I looked it up and it seems a bit weird. Could you give me a basic explanation as to how to do what I need to do with it?

Similar Threads

  1. Perpendicular Lines
    By captain in forum Object Oriented Programming
    Replies: 1
    Last Post: March 5th, 2012, 08:46 PM
  2. Reversing lines using LinkedList
    By velop in forum What's Wrong With My Code?
    Replies: 7
    Last Post: February 14th, 2011, 06:10 AM
  3. Problem with Output # of lines
    By coyboss in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 7th, 2011, 10:21 PM
  4. How would you get a JTextArea to know how many lines it had?
    By javapenguin in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 20th, 2011, 07:58 PM
  5. My lines won't work!!!
    By The Mewzytion in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 2nd, 2010, 10:24 AM