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

Thread: I need help! Re: strings, nextLine, while and more..

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy I need help! Re: strings, nextLine, while and more..

    FIRST, understand that I'm not merely seeking the answer to this assignment question. I'd like to learn!

    First, I'll overview the question:

    I am to write a program that takes a list of data integers in the range [1,3]. The list is supposed to be white-space separated. With this data set, I am to calculate the mode, the frequency of the mode and the variance (I am given a formula). The program should work with any size data set ending in 0. I.e., 0 indicates the end of user input.

    Here is a sample session I was given in the question:
    Please provide a list of integer data points in range [1-3].
    Indicate the end of your list with 0.
    1 3 2 1 1 1 1 1 2 3 2 1 2 3 2 2 0
    The mode is: 1
    The frequency of the mode is: 7
    The mean is: 1.75
    The variance is: 0.5625

    Can anyone get me started on this? I'm really confused. I know that I'm going to have to use while statements, and to use nextLine.
    Any help would be appreciated!


  2. #2
    Junior Member
    Join Date
    Oct 2010
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I need help! Re: strings, nextLine, while and more..

    After reading up a bit I realized I may be wrong about nextLine. Perhaps I should use nextInt.
    Gosh I'm confused.
    Last edited by rockerade; October 2nd, 2010 at 04:59 PM.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: I need help! Re: strings, nextLine, while and more..

    Do you have the steps that you want your program to take(a design) to solve this problem?
    You need to decide what you want the program to do before writing code.
    What code do you have so far and what questions do you above about how to do the steps for the program.?

  4. #4
    Junior Member
    Join Date
    Oct 2010
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I need help! Re: strings, nextLine, while and more..

    Quote Originally Posted by Norm View Post
    What code do you have so far and what questions do you above about how to do the steps for the program.?
    Sorry. Here it is! Excuse my poor formatting. Note that this code is NOT working properly. I am not getting the right results.
    import java.util.Scanner;
     
    public class SimpleDigitAnalysis {
     public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int number, count1=0, count2=0, count3=0;
            System.out.print("Please provide a list of integer data points in range [1-3]. Indicate the end of your list with 0.");
        number = input.nextInt(); 
     
        while ( number!=0 )
        { {if (number == 1 ) 
          ++count1;
          number = input.nextInt();}
        { if (number == 2)
          ++count2;
        number = input.nextInt();}
        {if (number == 3)
           ++count3;
        number = input.nextInt();} }
     
        {if (count1>=count2 && count2>=count3 || count1>=count3 && count3>=count2)
         {System.out.println("The mode is: 1");
         System.out.println("The frequency of the mode is: " +count1);}
       if (count2>=count1 && count1>=count3 || count2>=count3 && count3>=count1)
       {System.out.println("The mode is 2");
         System.out.println("The frequency of the mode is: " +count2);}
       if (count3>=count1 && count1>=count2 || count3>=count2 && count2>=count1)
       {System.out.println("The mode is 3");
         System.out.println("The frequency of the mode is: " +count3);}
     }
     }}
    Last edited by helloworld922; October 5th, 2010 at 09:24 PM.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: I need help! Re: strings, nextLine, while and more..

    I am not getting the right results.
    Please explain. Show what the program does now and explain what you want it to do.

  6. #6
    Junior Member
    Join Date
    Oct 2010
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I need help! Re: strings, nextLine, while and more..

    I first ask for nextInt

    Then the while statement comes in

    For as long as the nextInt does not equal 0, I read the value (if it is 1, 2 or 3) and add one to the count. When the condition in the while evaluates to true, meaning the number is zero, I proceed to calculate the MODE and the FREQUENCY.

    It's relatively simple calculations. Just figuring out which count occurs the most.

    However, the program does not operate correctly. For example, I enter 1 1 1 0. I SHOULD get a mode of 1 and a frequency of three. However, the program says the mode is one and the frequency is ONE (??). Not sure what's happening.

    For anyone who doesn't know, the mode is simply the number that appears the most. There can be more than one mode. The frequency is the number of times that it appears.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: I need help! Re: strings, nextLine, while and more..

    the program does not operate correctly.
    Please show the console from when you execute the program and explain what is wrong with the output and show what the output should be.


    A note on your coding style. Don't put code on the same line following a { or on the same line before a }
    That makes it hard to see them and hard to change the code.

    When posting code use code tags to preserve formatting. Info here: Java Forums - BB Code List

  8. #8
    Junior Member
    Join Date
    Oct 2010
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I need help! Re: strings, nextLine, while and more..

    Quote Originally Posted by Norm View Post
    Please show the console from when you execute the program and explain what is wrong with the output and show what the output should be.


    A note on your coding style. Don't put code on the same line following a { or on the same line before a }
    That makes it hard to see them and hard to change the code.

    When posting code use code tags to preserve formatting. Info here: Java Forums - BB Code List
    Sorry about that.
    I'm using Dr Java because I was having trouble with the console. Anyways, it's pretty easy to explain what the output is and what it should be.

    Say I enter the following: 1 1 1 0
    The mode should be 1 and the frequency should be 3.
    However, it reads the following:
    The mode is: 1
    The frequency of the mode is: 1

    Another example. I enter: 1 1 2 1 3 3 3 0
    This time, nothing happens. Just a new line. No message, nothing.
    The mode should be 1 (or 3, it's a tie) and the frequency should be 3.

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: I need help! Re: strings, nextLine, while and more..

    Is you last post what the program generated or what you think would happen if the program is executed? Or maybe some of what you remember happened.
    The exact output is important.

    You need to fix the usage of the {}s and repost your code with code tags.
    Last edited by Norm; October 2nd, 2010 at 08:29 PM.

  10. #10
    Junior Member
    Join Date
    Oct 2010
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I need help! Re: strings, nextLine, while and more..

    Quote Originally Posted by Norm View Post
    Is you last post what the program generated or what you think would happen if the program is executed? Or maybe some of what you remember happened.
    The exact output is important.

    You need to fix the usage of the {}s and repost your code with code tags.
    No, that's the exact output. I straight copy and pasted the outcome:
    The mode is: 1
    The frequency of the mode is: 1
    In the second case I got a new blank line. Nothing happened.

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: I need help! Re: strings, nextLine, while and more..

    You don't show what was input. The program starts by printing this message:
    Please provide a list of integer data points in range [1-3]. Indicate the end of your list with 0

    Where is that in the console output that you posted?
    I have no idea what was entered as input to the program.

  12. #12
    Junior Member
    Join Date
    Oct 2010
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I need help! Re: strings, nextLine, while and more..

    import java.util.Scanner;
     
    public class SimpleDigitAnalysis
     {
    public static void main(String[] args) 
    {
    Scanner input = new Scanner(System.in);
    int number, count1=0, count2=0, count3=0;
    System.out.print("Please provide a list of integer data points in range [1-3]. Indicate the end of your list with 0.");
    number = input.nextInt(); 
     
    while ( number!=0 )
    {
     {
    if (number == 1 ) 
    ++count1;
    number = input.nextInt();
    }
    { 
    if (number == 2)
    ++count2;
    number = input.nextInt();
    }
    {
    if (number == 3)
    ++count3;
    number = input.nextInt();
    }
     }
     
    {
    if (count1>=count2 && count2>=count3 || count1>=count3 && count3>=count2)
    {System.out.println("The mode is: 1");
    System.out.println("The frequency of the mode is: " +count1);
    }
    if (count2>=count1 && count1>=count3 || count2>=count3 && count3>=count1)
    {System.out.println("The mode is 2");
    System.out.println("The frequency of the mode is: " +count2);
    }
    if (count3>=count1 && count1>=count2 || count3>=count2 && count2>=count1)
    {System.out.println("The mode is 3");
    System.out.println("The frequency of the mode is: " +count3);
    }
    }
    }
    }

  13. #13
    Junior Member
    Join Date
    Oct 2010
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I need help! Re: strings, nextLine, while and more..

    > run SimpleDigitAnalysis
    Please provide a list of integer data points in range [1-3]. Indicate the end of your list with 0. 1 1 1 0
    The mode is: 1
    The frequency of the mode is: 1
    >

    For example.

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: I need help! Re: strings, nextLine, while and more..

    Why all the extra pairs of {}s? It makes the code hard to read.
    You need to play computer and follow the statements of your program step by step to see where your logic problem is. Especially in the while loop. Take a piece of paper and write down the values of number as they are read in at each point in the code and look at the logic to see what will happen.

  15. #15
    Junior Member
    Join Date
    Oct 2010
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I need help! Re: strings, nextLine, while and more..

    I inserted the {} for the block statements.

    Do you see any flaws in the programming?

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: I need help! Re: strings, nextLine, while and more..

    Yes, that's why I asked you to play computer so you would find the problem.

    The extra {}s are confusing.

    To make it easier to test add/change these lines to your code to get the input from an array vs the user typing it in:


    final String Data = "1 1 2 1 3 3 3 3 2 2 2 2 2 2 0";
    Scanner input = new Scanner(Data); //System.in);
    Last edited by Norm; October 2nd, 2010 at 09:02 PM.

  17. #17
    Junior Member
    Join Date
    Oct 2010
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I need help! Re: strings, nextLine, while and more..

    Thanks for all the help but I found the answer somewhere else. You were right about the {}

  18. #18
    Junior Member
    Join Date
    Oct 2010
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I need help! Re: strings, nextLine, while and more..

    thanks for the help
    Last edited by rockerade; October 3rd, 2010 at 07:18 AM.

  19. #19
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: I need help! Re: strings, nextLine, while and more..

    ++ value increments value first, then runs any loops or whatever

    value++ runs loops and then increments it.

    int number, count1=0, count2=0, count3=0;

    I wasn't aware you could declare an int and initialize it like that way:

    I thought it had to be:

    int number;
    int count1 = 0;
    int count2 = 0;

    etc.

    Anyway, from what I can see of your code, it appears to only ask for one number.

    Why not have the number = input.nextInt() inside the while loop but before the if statements?

    However, if you do that, number will have to be initialized outside the loop.

    Also,

    {if (count1>=count2 && count2>=count3 || count1>=count3 && count3>=count2)
    {System.out.println("The mode is: 1");
    System.out.println("The frequency of the mode is: " +count1);}
    if (count2>=count1 && count1>=count3 || count2>=count3 && count3>=count1)
    {System.out.println("The mode is 2");
    System.out.println("The frequency of the mode is: " +count2);}
    if (count3>=count1 && count1>=count2 || count3>=count2 && count2>=count1)
    {System.out.println("The mode is 3");
    System.out.println("The frequency of the mode is: " +count3);}

    How do you want the && s and ||s to be used in the if statements?

    It could be you don't have the right things in the right sets of parenthesis.

  20. #20
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: I need help! Re: strings, nextLine, while and more..

    Failed I just realized you figured it out.
    Last edited by Tjstretch; October 5th, 2010 at 04:23 PM.

Similar Threads

  1. Replies: 1
    Last Post: August 13th, 2010, 06:58 AM
  2. Strings
    By Leeds_Champion in forum Algorithms & Recursion
    Replies: 3
    Last Post: November 3rd, 2009, 10:09 PM
  3. nextLine problems, any help appreciated
    By Schmitz14 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: October 5th, 2009, 01:23 AM
  4. problem with Scanner nextLine() method
    By mia_tech in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: March 10th, 2009, 04:14 AM
  5. Replies: 2
    Last Post: June 19th, 2008, 03:58 AM