0

In my program im reading numbers and signs until user gives us empty line. EDIT: Basicly program should simulate adding/removing numbers from stack(array of 10 elements) and there are two basic operations(adding: + and in next line a number and taking off: for every succesfully added number program should print :) when action is impossible(out of range of an array) print: :( and for possible removal print the number;(as below)

*SAMPLE:*

INPUT:
+
1
+
2
+
3
+
4
+
5
+
6
+
7
+
8
+
9
+
0
+
1
-
-
-
-
-
-
-
-
-
-
-
OUTPUT:
:)
:)
:)
:)
:)
:)
:)
:)
:)
:)
:(
0
9
8
7
6
5
4
3
2
1
:(
import java.util.*;
public class Zadanie3 {

public static void main(String[] args) {
    // TODO Auto-generated method stub
     Scanner input = new Scanner(System.in);
     String znak;
     
     char helper;
     int stack[]=new int[10];
     
     int i =-1;
     List<String> outcome = new ArrayList<>();
     
    
    while (input.hasNext()){
    
        znak=input.nextLine();
        if(znak.isEmpty()){
            break;
        }
        
        if(znak.charAt(0)=='+' && i<9){
            znak=input.nextLine();
            if(znak.isEmpty()){
                break;
            }
        i++;
        stack[i]=Integer.parseInt(znak);
        outcome.add(":)");
        
        }else if(znak.charAt(0)=='-' && i>=0 && i<=9){
        outcome.add(String.valueOf(stack[i]));
        i--;
        }
        else{
            outcome.add(":(");
            
        }
        znak=input.nextLine();
        
        if(znak.isEmpty()){
            break;
        }
        
        if(znak.charAt(0)=='+' && i<9){
            znak=input.nextLine();
            if(znak.isEmpty()){
                break;
            }
        i++;
        stack[i]=Integer.parseInt(znak);
        outcome.add(":)");
        
        }else if(znak.charAt(0)=='-' && i>=0 && i<=9){
        outcome.add(String.valueOf(stack[i]));
        
        i--;
        
        }
        
        else{
            outcome.add(":(");
        }
        
        }
      
    
    for(String s: outcome) {
        System.out.println(s);
    }
    
    
    
    
    
    
    
    
    
}

}

After entering empty line input still doesnt stop - i've tried using input.isEmpty() in while and if's but it didnt work too.(As you can see i added multiple if statements, after each input but somehow they dont react when after few values I give empty space instead. Removing hasNext() from while and replacing it by isEmpty(), Equals() gives the same result.)

Community
  • 1
  • 1
user3231387
  • 13
  • 1
  • 5
  • 5
    `!input.equals("")` input is a Scanner - how can this be true? – Scary Wombat Nov 07 '18 at 07:53
  • change the code from `znak.equals("")` to `if(znak.isEmpty())` inside `while` loop to break from loop and remove condition `&& !input.equals("")`, this does not make any sense – greengreyblue Nov 07 '18 at 07:57

2 Answers2

1

To make small working example consider

Scanner input = new Scanner(System.in);

    while (input.hasNextLine()){   // test for new input

       String znak=input.nextLine();  // get input
       if(znak.isEmpty()){      // see if empty
           break;
       }           
       System.out.println(znak);
    }
Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
0

Change the code from if(znak.equals("")) to if(znak.isEmpty()) inside while loop in multiple places to break from loop and remove condition && !input.equals("") in while loop, I do not think it makes sense to have this condition.

Try below

import java.util.*;
public class Zadanie3 {
public static void main(String[] args) {
        // TODO Auto-generated method stub
         Scanner input = new Scanner(System.in);
         String znak = input.nextLine();
         if(znak.isEmpty()){                               
                System.exit(0);                     
            }
         char helper;
         int stack[]=new int[10];

         int i =-1;
         List<String> outcome = new ArrayList<>();


        while (input.hasNext() ){

            znak=input.nextLine();
            if(znak.isEmpty()){
                break;
            }

            if(znak.charAt(0)=='+' && i<9){
                znak=input.nextLine();
            if(znak.isEmpty()){
                break;
            }
            i++;
            stack[i]=Integer.parseInt(znak);
            outcome.add(":)");

            }else if(znak.charAt(0)=='-' && i>=0 && i<=9){
            outcome.add(String.valueOf(stack[i]));
            i--;
            }
            else{
                outcome.add(":(");

            }
            znak=input.nextLine();

            if(znak.isEmpty()){
                break;
            }

            if(znak.charAt(0)=='+' && i<9){
                znak=input.nextLine();
            if(znak.isEmpty()){
                break;
            }
            i++;
            stack[i]=Integer.parseInt(znak);
            outcome.add(":)");

            }else if(znak.charAt(0)=='-' && i>=0 && i<=9){
            outcome.add(String.valueOf(stack[i]));

            i--;

            }

            else{
                outcome.add(":(");
            }

            }


        for(String s: outcome) {
            System.out.println(s);
        }



    }

}
greengreyblue
  • 389
  • 3
  • 12
  • Changing to znak.isEmpty() gives no effect.( In both situations, when its in the loop only or in all If’s. ..) – user3231387 Nov 07 '18 at 13:55
  • After entering 4 elements: + 1 + 2 and then enter, program is not reacting for given empty line. – user3231387 Nov 07 '18 at 13:59
  • could you edit your question with possible inputs and expected outputs and I feel this is not the platform to provide complete code – greengreyblue Nov 07 '18 at 14:09
  • I do not understand reason for `while (input.hasNext())` either – greengreyblue Nov 07 '18 at 14:15
  • I changed my first post, input.hasNext() - i thought that allows to generate multiple inputs untill user stops giving them ;q and i couldnt find any other way to do it. – user3231387 Nov 07 '18 at 16:40
  • Change it to `while(true)` and see how it behaves and you have break statement anyway in your loop if empty input. – greengreyblue Nov 08 '18 at 08:45
  • Changing to while(true) worked ! But now the other problem is that I cant have infinite loop (the webiste on which i need to upload the program returns problem with compilation error in that case because process is not stopping immadiately.) Is there any other way to write that loop in java? Can I somehow include that case in the loop declaration so the loop will stop itself instead of using break? – user3231387 Nov 09 '18 at 21:03