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

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

1. ## 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. ## 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.

3. ## 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. ## Re: I need help! Re: strings, nextLine, while and more..

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.
```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);}
}
}}```

5. ## 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. ## 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.

7. ## 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. ## Re: I need help! Re: strings, nextLine, while and more..

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.

9. ## 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.

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

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.

11. ## 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. ## 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. ## 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. ## 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. ## 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. ## 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);

17. ## 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. ## Re: I need help! Re: strings, nextLine, while and more..

thanks for the help

19. ## 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. ## Re: I need help! Re: strings, nextLine, while and more..

Failed I just realized you figured it out.