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

Thread: Saving information from arrays. I think.

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    22
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Saving information from arrays. I think.

    Hello. I am currently a high-school student. As I am going to apply for Computer Science at my local university after the summer, I have bought the book used in the beginners class for programming. Having solved all the tasks in the first two chapters of the book with relative ease, I found the third chapter to be far more difficult. Here is the task I am struggling with (translated from Norwegian):

    Create a program that allows the user to write in a sequence of integers from 1-9 from the keyboard, and writes out a report about how many times the integers 1-9 occurred. Allow the user to end the sequence with a negative number. For example, if the user writes
     
    8
    0
    2
    8
    8
    9
    5
    9
    -3
     
    the program should write the following:
     
    0 occurs 1 time.
    2 occurs 1 time.
    5 occurs 1 time.
    8 occurs 3 times.
    9 occurs 2 times
     
    Hint: create a table for ten integers that can save the number of times a number has occurred (int[] times = new int[10];)

    Can anybody point me in the right direction here?

    Please note that I have a very basic knowledge of Java. I have only solved very basic problems, the most advanced of which include creating a calculator for the quadratic formula, a program that allows the user to enter how many numbers he wants to find the average of etc. Thus, I need to be fed this information with a tea-spoon.

    EDIT: Just tried this:

    import java.util.*;
    public class Kap3Oppg1 {
    	public static void main(String[] args) {
    		Scanner in = new Scanner(System.in);
    		int[] ganger = new int[10];
    		int input;
    		do {
    			System.out.println("Tast inn neste tall: ");
    			input = in.nextInt();
    			for (int i=0;i<ganger.length;i++){
    				System.out.println("Tallet "+i+" forekom "+ganger+" ganger.");
    			}
    		} while (input >= 0);
     
    	}
     
    }

    I am getting an error due to i<input.length. i<input is just weird. Am I even walking in the right direction?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Saving information from arrays. I think.

    I am getting an error
    Please copy the full text of the error message and paste it here.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jan 2013
    Posts
    22
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Saving information from arrays. I think.

    I changed it from i<input.length to i<ganger.length. I do not get an error anymore. This is how it runs, regardless of which number I enter:

    Tast inn neste tall: 
    3
    Tallet 0 forekom [I@e5b723 ganger.
    Tallet 1 forekom [I@e5b723 ganger.
    Tallet 2 forekom [I@e5b723 ganger.
    Tallet 3 forekom [I@e5b723 ganger.
    Tallet 4 forekom [I@e5b723 ganger.
    Tallet 5 forekom [I@e5b723 ganger.
    Tallet 6 forekom [I@e5b723 ganger.
    Tallet 7 forekom [I@e5b723 ganger.
    Tallet 8 forekom [I@e5b723 ganger.
    Tallet 9 forekom [I@e5b723 ganger.

  4. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Saving information from arrays. I think.

    You're printing the toString() representation of the int array and not any of the items held by the array. Consider using array indices when printing your array items: myArray[i]. It's the [i] part that you're forgetting.

  5. The Following User Says Thank You to curmudgeon For This Useful Post:

    EatMyBible (January 27th, 2013)

  6. #5
    Junior Member
    Join Date
    Jan 2013
    Posts
    22
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Saving information from arrays. I think.

    Thanks to your post, I now see the logical flaw in my program. It currently looks like this:

    import java.util.*;
    public class Kap3Oppg1 {
    	public static void main(String[] args) {
    		Scanner in = new Scanner(System.in);
    		int[] ganger = new int[10];
    		int input;
    		do {
    			System.out.println("Tast inn neste tall: ");
    			input = in.nextInt();
    			for (int i=0;i<ganger.length;i++){
    				System.out.println("Tallet "+i+" forekom "+ganger[i]+" ganger.");
    			}
    		} while (input > 0);
     
    	}
     
    }

    Which naturally makes it run like this:

    Tast inn neste tall: 
    3
    Tallet 0 forekom 0 ganger.
    Tallet 1 forekom 0 ganger.
    Tallet 2 forekom 0 ganger.
    Tallet 3 forekom 0 ganger.
    Tallet 4 forekom 0 ganger.
    Tallet 5 forekom 0 ganger.
    Tallet 6 forekom 0 ganger.
    Tallet 7 forekom 0 ganger.
    Tallet 8 forekom 0 ganger.
    Tallet 9 forekom 0 ganger.

    I see why it runs like this, seeing as the input itself is never used in the output. I am not sure how to proceed here. Can anyone point me in the right direction?

  7. #6
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Saving information from arrays. I think.

    Hello EatMyBible!

    Quote Originally Posted by EatMyBible View Post
    I see why it runs like this, seeing as the input itself is never used in the output. I am not sure how to proceed here. Can anyone point me in the right direction?
    You are right. You need to somehow use the input to make your calculations.
    Since the input is in [0,10) and your array has length 10, you can use input as the index of the array and add one in that index every time it is entered. Then for example array[0] keeps the value of how many times occured 0, array[1] how many times occured 1, etc.

    Hope that makes sense.

  8. The Following User Says Thank You to andreas90 For This Useful Post:

    EatMyBible (January 27th, 2013)

  9. #7
    Junior Member
    Join Date
    Jan 2013
    Posts
    22
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Saving information from arrays. I think.

    This is how I ended up solving the problem:

    import java.util.*;
    public class Kap3Oppg1 {
    	public static void main(String[] args) {
    		Scanner in = new Scanner(System.in);
    		int ganger[] = new int[10];
    		int input = 0;
    		while (true) {
    			System.out.println("Tast inn neste tall: ");
    			input = in.nextInt();
    			if (input < 0 || input > 9)
    				break;
    			    ++ganger[input];
    		}
    		for (int i=0;i<ganger.length;i++){
    			if (ganger[i] == 0)
    				System.out.print("");
    			else
    			System.out.println("Tallet "+i+" forekom "+ganger[i]+" ganger.");
    		}
     
     
    	}
     
    }

    Thanks for all the help.

Similar Threads

  1. Saving login information into a text file in my GUI App
    By lf2killer in forum Java Theory & Questions
    Replies: 5
    Last Post: November 16th, 2012, 09:14 AM
  2. Saving as HTML?
    By LoganC in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 8th, 2012, 10:20 PM
  3. Saving and Loading
    By nitwit3 in forum What's Wrong With My Code?
    Replies: 12
    Last Post: August 2nd, 2011, 01:31 PM
  4. need help with saving data
    By bardd in forum Java Theory & Questions
    Replies: 5
    Last Post: September 19th, 2010, 02:33 PM
  5. [SOLVED] Saving A File?
    By MysticDeath in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: August 1st, 2009, 11:56 AM