java - Using Scanner, and nextLine for ending programm -


i supposed create program keeps reading user input command line until user types quit shall use indexof position of space characters.

i tried following:

import java.util.scanner;  import java.lang.string; public class aufgabe6b {      public static void main(string[] args) {         scanner scanner=new scanner(system.in);          string a;         system.out.println("bitte eingabe machen!");         while(true){              a=scanner.nextline();         if("quit".equals(a)){             break;         }         }         system.out.println(a.indexof(" "));     } } 

while a.indexof giving me position of first " " founds, still have issue scanner , quit.

if type:

hello test quit, doesnt quit. if type quit, breaks of queue. if type quit hello ist test, doesnt quit.

i supposed use indexof , scanner nextline method. possible, did wrong?

one option be:

while(true) {   a=scanner.nextline();   int j = a.indexof("quit");   if (j >= 0)       break; } 

if word 'quit' present, indexof method return positive value.

the problem in code here: if("quit".equals(a))

for condition true, 'a' must equal "quit", comparing other string different return false.

hope solves :)

edit: find number of occurences:

public static int occurencefinder(string source, string pattern) {     int counter = 0;     int index = source.indexof(pattern);     if (index == -1) return 0;     counter++;     while (true) {         index = source.indexof(pattern, index + 1);         if (index == -1) return counter;         counter++;     }  } 

edit : find positions

public static linkedlist<integer> positionfinder(string source, string pattern) {         linkedlist<integer> list = new linkedlist<integer>();         int index = source.indexof(pattern);         if (index == -1) return list;         list.add(index);         while (true) {             index = source.indexof(pattern, index + 1);             if (index == -1) return list;             list.add(index);         }      } 

Comments

Popular posts from this blog

jQuery Mobile app not scrolling in Firefox -

c++ - How to add Crypto++ library to Qt project -

how to receive file in java(servlet/jsp) -