12 Replies - 296 Views - Last Post: 03 February 2013 - 08:37 AM Rate Topic: -----

#1 laklaker  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 01-October 12

Problem using [repeating it's output]

Posted 02 February 2013 - 03:32 AM

So I'm doing a program that would require to enter a number and enter it's alphabet the output should be like this.

Output (Should be):
|a
Next input b:
|b|a

So happens that its like this:
Output (My program output):
|a
Next input b:
|b

        do{
            System.out.println("\n\nPlease enter your choice from the following menu:");
            System.out.println("1. Enter number");
            System.out.println("2. Exit");
            choice = Integer.parseInt(reader.readLine());
        switch (choice){
            case 1:
            System.out.print("Enter number: ");
            find=Integer.parseInt(reader.readLine());
            
            for(i=0;i<anNumber.length;i++){
                if(find == anNumber[i]){
                    anFinals[i] = anLetters[i];
                    System.out.print("| "+anFinals[i]);
                }
            }
            case 2:
            break;
        }   
        }while (choice > 0 && choice < 2);



Is This A Good Question/Topic? 0
  • +

Replies To: Problem using [repeating it's output]

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: Problem using [repeating it's output]

Posted 02 February 2013 - 04:34 AM

My interpretation of your post is that you want to program the output to be cumulative, to remember all previous output and add the 'new' output to the previous. Correctly me if I'm wrong.

In order to 'remember', your program needs another variable, call it

String output = "|";

Build the new output with each new user selection:

// adds the user's choice to the previous output
output += anFinals[i] + "|" ;

Then print the output rather than the user's choice:

// prints the updated output:
System.out.print( output );

You may have to adjust how/where the dividers are added, but you'll figure it out.
Was This Post Helpful? 0
  • +
  • -

#3 laklaker  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 01-October 12

Re: Problem using [repeating it's output]

Posted 02 February 2013 - 05:13 AM

So I tried it

        do{
            System.out.println("\n\nPlease enter your choice from the following menu:");
            System.out.println("1. Enter number");
            System.out.println("2. Exit");
            choice = Integer.parseInt(reader.readLine());
        switch (choice){
            case 1:
            System.out.print("Enter number: ");
            find=Integer.parseInt(reader.readLine());
            
            for(i=0;i<anNumber.length;i++){
                if(find == anNumber[i]){
                    search = anFinals[i] + "|";
                    System.out.print(search);
                }
            }       
            case 2:
            break;
        }   
        }while (choice > 0 && choice < 2);



The output was only:
|
and next was :
|
Was This Post Helpful? 0
  • +
  • -

#4 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: Problem using [repeating it's output]

Posted 02 February 2013 - 05:25 AM

It's important that you add the search variable to itself to capture and BUILD on its previous value:

search += anFinals[i] + "|";

Notice the '+=' operator.
Was This Post Helpful? 0
  • +
  • -

#5 laklaker  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 01-October 12

Re: Problem using [repeating it's output]

Posted 02 February 2013 - 05:50 AM

Yeah. Thanks for that anyway. The problem. It's adding the new on on the bottom.

The output:
|B|

Next will be a:
|B|a|

It should be like this:
|a||B|

Thank you for your big help GregBrannon!
Was This Post Helpful? 0
  • +
  • -

#6 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: Problem using [repeating it's output]

Posted 02 February 2013 - 05:55 AM

Use your imagination/creativity and THINK of a way to use what you now know to get the results you need.
Was This Post Helpful? 0
  • +
  • -

#7 laklaker  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 01-October 12

Re: Problem using [repeating it's output]

Posted 02 February 2013 - 06:25 AM

So I'm still trying on it but I still have an error.

       String a;
       top = -1;
       anNumber[0]=1;
            for(i=0;i<anNumber.length;i++){
                anNumber[i]=i+1;
            }
            
        do{
                System.out.println("\n\nPlease enter your choice from the following menu:");
                System.out.println("1. Enter number");
                System.out.println("2. Exit");
                choice = Integer.parseInt(reader.readLine());
                top++;
            switch (choice){
                case 1:
                System.out.print("Enter number: ");
                find=Integer.parseInt(reader.readLine());
                
                for(i=top;i<=anNumber.length;i--){
                    if(find == anNumber[i]){
                        output += anLetters[i] + "|";
                        System.out.print(output);
                    }
                }       
                case 2:
                break;
            }   
        }while (choice > 0 && choice < 2);



It says out of bound.
Was This Post Helpful? 0
  • +
  • -

#8 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: Problem using [repeating it's output]

Posted 02 February 2013 - 06:48 AM

Please post entire error messages when asking for help with errors, copied and pasted just as they occur at your end. Then, for snippets like yours, point to the line the error message is referring to.

In your case, "out of bounds" probably is an IndexOutOfBounds error, so this loop:

for(i=top;i<=anNumber.length;i--){
if(find == anNumber[i])

(and possibly any parts thereafter) is suspect. Why? Because myArray[myArray.length] is not a valid element, and that's what the for loop is allowing. Change to:

i < anNumber.length
Was This Post Helpful? 0
  • +
  • -

#9 laklaker  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 01-October 12

Re: Problem using [repeating it's output]

Posted 03 February 2013 - 03:15 AM

So at first I just copied the code in this program.

import java.io.BufferedReader;

import java.io.InputStreamReader;

import java.io.IOException;

 

public class ArrayStacks

{

public static void main(String[] args)

{

BufferedReader dataln=new BufferedReader (new InputStreamReader(System.in));

String c="", data="";

String[] arrays = new String[5];

int top=-1, b=0;

 

do{

System.out.println("Stack Main Menu");

System.out.println("\n1. Push");

System.out.println("2. Pop");

System.out.println("3. Stack Top");

System.out.print("?: ");

 

try{

c=dataln.readLine();

}

catch( IOException e ){

System.out.println("WRONG");

}

 

b=Integer.parseInt(c);

 

if(b==1){ //stack push

if(top==4){//stack full

System.out.println("\n");

System.out.println("Stack Overflow ");

}

else{

System.out.println("\n");

System.out.println("Stack Push ");

System.out.print("?: ");

try{

data=dataln.readLine();

}

catch( IOException e ){

System.out.println("WRONG");

}

top++;


arrays[top]=data;

System.out.println("\nStack");

System.out.println("");

for(int i=top; i>=0; i--){

System.out.println("" + arrays[i]);

}

}

}

System.out.println("\n\n");

}while(b!=4);

 

}

}



My code is

       top = -1;
       anNumber[0]=1;
            for(i=0;i<anNumber.length;i++){
                anNumber[i]=i+1;
            }
            
        do{
                System.out.println("\n\nPlease enter your choice from the following menu:");
                System.out.println("1. Enter number");
                System.out.println("2. Exit");
                choice = Integer.parseInt(reader.readLine());
                top++;
            switch (choice){
                case 1:
                System.out.print("Enter number: ");
                find=Integer.parseInt(reader.readLine());
                for(i=top;i<anNumber[i];i--){
                    if(find == anNumber[i]){
                        output += anLetters[i] + "|";
                        System.out.print(output);
                    }
                }       
                case 2:
                break;
            }   
        }while (choice > 0 && choice < 2);



The error is "java.lang.ArrayIndexOutOfBoundsException: -1".
Was This Post Helpful? 0
  • +
  • -

#10 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: Problem using [repeating it's output]

Posted 03 February 2013 - 04:27 AM

So (somewhere) the program is trying to access the -1 index of an array, which never exists.

I gather you were using the first bit of code you posted as an example, right? Look at line 98 of the first bit of code. Think about what that for loop is doing: i must be starting as a large number, decreasing by one each time through the loop, and it can get no smaller than 0. Now look at line 17 in the second bit you posted. See any differences? Does the for loop construct make sense?

This post has been edited by GregBrannon: 03 February 2013 - 04:28 AM

Was This Post Helpful? 0
  • +
  • -

#11 laklaker  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 01-October 12

Re: Problem using [repeating it's output]

Posted 03 February 2013 - 08:20 AM

The problem now is The error is "java.lang.ArrayIndexOutOfBoundsException: 26". oh gosh. x.x
Was This Post Helpful? 0
  • +
  • -

#12 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: Problem using [repeating it's output]

Posted 03 February 2013 - 08:24 AM

Can't provide specific help without updated code, but the general solution is the same as before: bound the index variable, usually the loop control variable, so that it can't bust the boundaries of the array being inspected. Give it some thought.
Was This Post Helpful? 0
  • +
  • -

#13 laklaker  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 01-October 12

Re: Problem using [repeating it's output]

Posted 03 February 2013 - 08:37 AM

I updated it into like this.


                for(int c=top;c<anNumber[i];c--){
                    if(find == anNumber[c]){
                        output += anLetters[c] + "|";
                        System.out.print(output);
                    }
                }       



Was This Post Helpful? 0
  • +
  • -

Page 1 of 1