-2

I want to make a program that copies strings into a vector until the hold string is set to a certain value, at which point the program should print out the elements of the array.

I'm not sure how the in.nextline() function works, so it could be that I'm not erasing previous entries from hold, or is it that my if(hold=="0") check is simply not valid java?

import java.util.*;
import java.util.Scanner;
public class startingPoint 
{
    public static void main(String roark[])
    {
        String hold;
        boolean finished=false;
        Scanner in = new Scanner(System.in);
        Vector<String> vec = new Vector<String>();
        while(finished==false){
        System.out.print("Enter the string you'd like to save, or enter 0 to print out saved strings\n");
        hold=in.nextLine();
        if(hold=="0"){
            for(int looper=0;looper<vec.size();looper++){
                System.out.print(vec.get(looper));
                System.out.print("\n");
            }
            finished=true;
        }else{
            vec.add(hold);
        }
    }

}

}

Amila
  • 5,195
  • 1
  • 27
  • 46
Roark
  • 1
  • 1

1 Answers1

1

You need to use equals method:

if("0".equals(hold){
Amila
  • 5,195
  • 1
  • 27
  • 46