12 Replies - 339 Views - Last Post: 13 November 2012 - 03:56 PM Rate Topic: -----

#1 jjackson44  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 13-November 12

Occurance in numbers program help

Posted 13 November 2012 - 09:35 AM

I am having trouble getting this to work. I am able to enter integers but that is all I am able to do. I am not getting any output from it. Any help would be great! Thank you!
Here is the program

import java.util.HashMap;
	import java.util.Iterator;
	import java.util.Map;
	import java.util.Scanner;
	 
	 
	public class JOJ10070a19 {
	 
	    public static void main(String[] args) {
	     
	        Map<Integer, Integer> list = new HashMap<Integer, Integer>();
	         
	        int s = 0;
	         
	        do{
	            Scanner input = new Scanner(System.in);
	             
	            //ask for user's input
	            System.out.print("Enter the numbers: ");
	             
	            //obtain user's input
	            s = input.nextInt();
	             
	            for(int i = 0; i < list.size(); i++){
	            list.put(s, i);
	             int key = i;
	                if (list.containsKey(key)) {
	                   int occurrence = list.get(key);
	                    occurrence++;
	                   list.put(key, occurrence);
	                } else {
	                    list.put(key, 1);
	                }
	            }
	             
	        } while (s != 0);
	 
	       Iterator iterator = list.keySet().iterator();
	        
	       while (iterator.hasNext()) {
	           int key = (Integer) iterator.next();
	           int occurrence = list.get(key);
	           if(occurrence >= 2)
	            System.out.println(key + " occur " + occurrence + " time(s).");
	        }
	 
	    }
	 
	}

This post has been edited by jon.kiparsky: 13 November 2012 - 09:44 AM
Reason for edit:: added [code] tags


Is This A Good Question/Topic? 0
  • +

Replies To: Occurance in numbers program help

#2 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7651
  • View blog
  • Posts: 12,907
  • Joined: 19-March 11

Re: Occurance in numbers program help

Posted 13 November 2012 - 09:56 AM

public class JOJ10070a19 {


I don't like the class name. I ought to be able to tell what this class represents by its name, but this char salad doesn't tell me much. And since your description of the problem doesn't tell me what it is you're actually trying to do, there's not a lot of help I can offer. Please use your favorite search engine to locate Eric S. Raymond's essay on "How to ask questions the smart way" and read it.

That being said, this only produces output if "occurence" ge 2. That only happens if you look up a value in a map and find a number n, where n ge 2.

The iteration of the list looks reasonable, though it would be easier to read if you used the enhanced for loop:

for (Integer key: list.keySet()){
    occurrence = list.getKey();
     if(occurrence >= 2)
	            System.out.println(key + " occur " + occurrence + " time(s).");
  }


So not seeing any real problems there, I look back to the routine that populates the list.

(why are these in the same method?)

And I see this, which looks like it's the cause of your woes:

	            for(int i = 0; i < list.size(); i++){
	            list.put(s, i);



The logic here is problematic for several reasons, and you should step through it on a whiteboard until you understand what it is you're actually doing here. But what happens if list.size() is zero? Can anything ever be added to an empty list in your code?
Was This Post Helpful? 1
  • +
  • -

#3 jjackson44  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 13-November 12

Re: Occurance in numbers program help

Posted 13 November 2012 - 12:21 PM

View Postjon.kiparsky, on 13 November 2012 - 09:56 AM, said:

public class JOJ10070a19 {


I don't like the class name. I ought to be able to tell what this class represents by its name, but this char salad doesn't tell me much. And since your description of the problem doesn't tell me what it is you're actually trying to do, there's not a lot of help I can offer. Please use your favorite search engine to locate Eric S. Raymond's essay on "How to ask questions the smart way" and read it.

That being said, this only produces output if "occurence" ge 2. That only happens if you look up a value in a map and find a number n, where n ge 2.

The iteration of the list looks reasonable, though it would be easier to read if you used the enhanced for loop:

for (Integer key: list.keySet()){
    occurrence = list.getKey();
     if(occurrence >= 2)
	            System.out.println(key + " occur " + occurrence + " time(s).");
  }


So not seeing any real problems there, I look back to the routine that populates the list.

(why are these in the same method?)

And I see this, which looks like it's the cause of your woes:

	            for(int i = 0; i < list.size(); i++){
	            list.put(s, i);



The logic here is problematic for several reasons, and you should step through it on a whiteboard until you understand what it is you're actually doing here. But what happens if list.size() is zero? Can anything ever be added to an empty list in your code?




I am sorry to say, I obviously do not have a clue what is going on in this class. I am struggling and need help. As you can probably see this code is a piece of crap. I do not need smart answers from people. If you are willing to help me then please do I really appreciate it but if your gonna be a smart @ss about it then please do not comment on here. Thank you

This post has been edited by jon.kiparsky: 13 November 2012 - 12:31 PM
Reason for edit:: Removed double post

Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7651
  • View blog
  • Posts: 12,907
  • Joined: 19-March 11

Re: Occurance in numbers program help

Posted 13 November 2012 - 12:32 PM

I'll leave you to fend for yourself, then. Good luck.
Was This Post Helpful? 0
  • +
  • -

#5 ChillyWilly  Icon User is offline

  • New D.I.C Head

Reputation: 16
  • View blog
  • Posts: 41
  • Joined: 21-October 11

Re: Occurance in numbers program help

Posted 13 November 2012 - 12:42 PM

What is this program suppose to do?
Was This Post Helpful? 0
  • +
  • -

#6 jjackson44  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 13-November 12

Re: Occurance in numbers program help

Posted 13 November 2012 - 12:46 PM

You are supposed to enter integers 1-100 and the output would be it replying with the occurance of numbers.
Was This Post Helpful? 0
  • +
  • -

#7 ChillyWilly  Icon User is offline

  • New D.I.C Head

Reputation: 16
  • View blog
  • Posts: 41
  • Joined: 21-October 11

Re: Occurance in numbers program help

Posted 13 November 2012 - 12:58 PM

View Postjjackson44, on 13 November 2012 - 12:46 PM, said:

You are supposed to enter integers 1-100 and the output would be it replying with the occurance of numbers.


So the user is suppose to enter a number between 1-100 each time the user is prompted, and you want the numbers that was entered to be displayed along with how many times each number occurs? So if the user enters integers 1,5,62,5 you want the display to show that the number 1 was entered once, 5 was entered twice, 62 was entered once?
Was This Post Helpful? 0
  • +
  • -

#8 jjackson44  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 13-November 12

Re: Occurance in numbers program help

Posted 13 November 2012 - 01:06 PM

View PostChillyWilly, on 13 November 2012 - 12:58 PM, said:

View Postjjackson44, on 13 November 2012 - 12:46 PM, said:

You are supposed to enter integers 1-100 and the output would be it replying with the occurance of numbers.


So the user is suppose to enter a number between 1-100 each time the user is prompted, and you want the numbers that was entered to be displayed along with how many times each number occurs? So if the user enters integers 1,5,62,5 you want the display to show that the number 1 was entered once, 5 was entered twice, 62 was entered once?



I found a similar programming question. I have been following everything said on this post but people quit commenting on it so I am stuck and need help completing it. Here is the link with the similar post, http://www.dreaminco...umbers-entered/
I know I sound like an idiot but this class was nothing like I thought it was going to be. I understand nothing that is going on and I just want to try and pass the class with a decent grade and never take programming again unless somehow I learn and understand what I am doing lol
Was This Post Helpful? 0
  • +
  • -

#9 ChillyWilly  Icon User is offline

  • New D.I.C Head

Reputation: 16
  • View blog
  • Posts: 41
  • Joined: 21-October 11

Re: Occurance in numbers program help

Posted 13 November 2012 - 01:13 PM

View Postjjackson44, on 13 November 2012 - 01:06 PM, said:

I found a similar programming question. I have been following everything said on this post but people quit commenting on it so I am stuck and need help completing it. Here is the link with the similar post, http://www.dreaminco...umbers-entered/
I know I sound like an idiot but this class was nothing like I thought it was going to be. I understand nothing that is going on and I just want to try and pass the class with a decent grade and never take programming again unless somehow I learn and understand what I am doing lol


With the provided link:

I'm seeing that you took someone else's code, changed the name of the class to something that hurts my eyes to look at, re-posted it in a new thread, asked us to read this code, figure out what it suppose to do, fix it and allow you to hand this in to pass your class. NOOOOOOOOOOOOOOOOOOOO!
Was This Post Helpful? 1
  • +
  • -

#10 ChillyWilly  Icon User is offline

  • New D.I.C Head

Reputation: 16
  • View blog
  • Posts: 41
  • Joined: 21-October 11

Re: Occurance in numbers program help

Posted 13 November 2012 - 01:26 PM

^ ChillyWilly don't play that!
Was This Post Helpful? 0
  • +
  • -

#11 jjackson44  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 13-November 12

Re: Occurance in numbers program help

Posted 13 November 2012 - 02:15 PM

View PostChillyWilly, on 13 November 2012 - 01:13 PM, said:

View Postjjackson44, on 13 November 2012 - 01:06 PM, said:

I found a similar programming question. I have been following everything said on this post but people quit commenting on it so I am stuck and need help completing it. Here is the link with the similar post, http://www.dreaminco...umbers-entered/
I know I sound like an idiot but this class was nothing like I thought it was going to be. I understand nothing that is going on and I just want to try and pass the class with a decent grade and never take programming again unless somehow I learn and understand what I am doing lol


With the provided link:

I'm seeing that you took someone else's code, changed the name of the class to something that hurts my eyes to look at, re-posted it in a new thread, asked us to read this code, figure out what it suppose to do, fix it and allow you to hand this in to pass your class. NOOOOOOOOOOOOOOOOOOOO!


I emailed that person and was trying to figure out how they finished the code because I cant seem to get it to work. I would love to learn programming but it is so far in the semester and I am completely lost and need help. I need to learn how to make programs but I just don't understand it. Could you please guide me through the rest of it?
Was This Post Helpful? 0
  • +
  • -

#12 ChillyWilly  Icon User is offline

  • New D.I.C Head

Reputation: 16
  • View blog
  • Posts: 41
  • Joined: 21-October 11

Re: Occurance in numbers program help

Posted 13 November 2012 - 02:36 PM

Here is great advice:

1. Ditch this code and start over. If you are new to programming it is way harder to read someone else's code and try to make it do something else. There are so many different ways to write a program and if you don't know the ropes it can get very confusing.

2. write out in English what you want it to do from a computers stand point. Figure out what data structures to use and what will benefit you

3. Write it in java.

In your case you want a user to input numbers and display the occurrence of the numbers.

Step 1. Get the input from the user and put it into whatever data structure you like (Array,Map,Table,String,ArrayList)

Step 2. Iterate the data structure and display the contents.

No one can help you if you don't know what the program is suppose to do and better yet how you want to do it.

Forget that code you posted because it's useless to you if you don't know what it is doing. What do you want this new program to do?

This post has been edited by ChillyWilly: 13 November 2012 - 02:38 PM

Was This Post Helpful? 1
  • +
  • -

#13 nunc  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 131
  • Joined: 20-November 11

Re: Occurance in numbers program help

Posted 13 November 2012 - 03:56 PM

First of all, don't copy somebody elses code. Changing variable names won't prevent you from getting caught cheating.

Second, you will get absolutely no help if you act smart with the people on the forums. They don't have to help you, and they take time out of their busy days to try and help you anyways. You should at least be grateful.

Third, as far as the algorithm is concerned, just keep an array with 100 indexes, each index representing a counter for the total amount of times that index ( plus 1 ) was input.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1