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

• October 2nd, 2010, 04:35 PM
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:-bd!

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!
• October 2nd, 2010, 04:44 PM
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.
• October 2nd, 2010, 05:09 PM
Norm
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.?
• October 2nd, 2010, 05:31 PM
Re: I need help! Re: strings, nextLine, while and more..
Quote:

Originally Posted by Norm
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.
Code Java:

```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);} } }}```
• October 2nd, 2010, 06:11 PM
Norm
Re: I need help! Re: strings, nextLine, while and more..
Quote:

I am not getting the right results.
Please explain. Show what the program does now and explain what you want it to do.
• October 2nd, 2010, 07:57 PM
Re: I need help! Re: strings, nextLine, while and more..

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.
• October 2nd, 2010, 08:07 PM
Norm
Re: I need help! Re: strings, nextLine, while and more..
Quote:

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
• October 2nd, 2010, 08:23 PM
Re: I need help! Re: strings, nextLine, while and more..
Quote:

Originally Posted by Norm
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

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.
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.
• October 2nd, 2010, 08:27 PM
Norm
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.
• October 2nd, 2010, 08:40 PM
Re: I need help! Re: strings, nextLine, while and more..
Quote:

Originally Posted by Norm
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.
• October 2nd, 2010, 08:42 PM
Norm
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.
• October 2nd, 2010, 08:42 PM
Re: I need help! Re: strings, nextLine, while and more..
Code :

```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); } } } }```
• October 2nd, 2010, 08:45 PM
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.
• October 2nd, 2010, 08:50 PM
Norm
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.
• October 2nd, 2010, 08:57 PM
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?
• October 2nd, 2010, 08:59 PM
Norm
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);
• October 2nd, 2010, 09:10 PM
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 {}
• October 2nd, 2010, 09:11 PM
Re: I need help! Re: strings, nextLine, while and more..
thanks for the help
• October 5th, 2010, 03:07 PM
javapenguin
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,

Code :

```{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.
• October 5th, 2010, 03:51 PM
Tjstretch
Re: I need help! Re: strings, nextLine, while and more..
Failed I just realized you figured it out.