Results 1 to 6 of 6
  1. #1
    Exception is offline Member
    Join Date
    Sep 2009
    Posts
    3
    Rep Power
    0

    Exclamation String and while loop

    Hey guys, this is my first time posting, registered 5 mins ago.

    I am trying to make a program which allows the user to input letters, from A to F. when the user enters an empty sentence, the program is to print statistics to each letter. i.e:

    Enter letters: ABAB
    Enter letters: CBBBC
    Enter letters: DEDF
    Enter letters: AA
    Enter letters:

    Statistics:
    A: 4
    B: 5
    C: 2
    D: 2
    E: 1
    F: 1


    my program looks like this, so far;
    import java.util.Scanner;

    Java Code:
    public class Karakterstatistikk {
      
      public static void main(String args[]) {
        
        String karakterer = "";
        
        int primitiv = Integer.parseInt(karakterer);
        
        Scanner tastatur = new Scanner(System.in);
        
    
          System.out.println("Karakterstatistikk.");
          System.out.println("Innlesing avsluttes med tom linje.");
          System.out.print("Angi karakterer for student:");
          karakterer += tastatur.next();
          System.out.println();
        
          
          while (user.input > 0 ) {
            System.out.print("Angi karakterer for student:");
            karakterer += tastatur.next();
            System.out.println();
            System.out.println(karakterer);
            System.out.println(primitiv);
    It's the while loop which gives me problems. I need the right condition. I want the while loop to run, as long as the string expands. if the user enters an empty sentence, the program should exit the while loop and print the statistics. Any thoughts about what condition I should set in the while loop?

    I am from norway btw, therefore I have a lot of the program written in norwegian.

    Thanks.
    Last edited by Exception; 09-22-2009 at 05:29 PM.

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Write your code in English. It's the defacto language for programming. You will also get more help that way.
    What is the behaviour of your code now?
    What should happen if I put a character that is neither blank nor A-F?

  3. #3
    Exception is offline Member
    Join Date
    Sep 2009
    Posts
    3
    Rep Power
    0

    Default solved it

    Hey, thanks for the reply, I'll post it in English next time ;)

    Here is the solved puzzle;

    Java Code:
    /* Muliggjør bruk av tastatur */
    import java.util.Scanner;
    
    /* Navn på programmet */
    public class Karakterstatistikk {
      
      /* Main metoden */
      public static void main(String args[]) {
        
        /* Programmets variabler */
        String karakterer = "";
        char tegn;
        int a = 0;
        int b = 0;
        int c = 0;
        int d = 0;
        int e = 0;
        int f = 0;
        
        /* Aktiverer tastaturet og ber brukeren taste inn de første karakterene, som senere aktiverer While løkken */
        Scanner tastatur = new Scanner(System.in);
        System.out.println("Karakterstatistikk.");
        System.out.println("Innlesing avsluttes med tom linje.");
        System.out.print("Angi karakterer for student (store bokstaver):");
        
        /* Ber brukeren om å taste inn karakterer, helt til brukeren taster inn en tom setning */
        while (tastatur.hasNextLine()) {
          String karakterlinje = tastatur.nextLine();
          karakterer += karakterlinje;
          
          /* Når brukeren taster inn en tom setning aktiveres denne if-setningen, og break; komanndoen avslutter while løkken */
          if (karakterlinje.equals("")) {
            break;
          }
          
          /* Ber bruker om å taste inn de neste karakterene */
          System.out.print("Angi karakterer for student(store bokstaver):");
        }
        
        /* Tittelen til karakterstatistikken */
        System.out.println("Oversikt over karakterer:");
        
        /* For løkke med switch setning inni. Denne teller opp hvilke og hvor mange karakterer som ble gitt */
        for(int teller = 0 ; teller < karakterer.length() ; teller++){
          tegn = karakterer.charAt(teller);
          switch (tegn) {
            case 'A':
              a++; break;
            case 'B':
              b++; break;
            case 'C':
              c++; break;
            case 'D':
              d++; break;
            case 'E':
              e++; break;
            case 'F':
              f++; break;
            default:
              System.out.println(tegn + " er ikke A-F! ");
          }
        }
        
        /* Utskrift av karakterstatistikk */
        System.out.println("A: " + a);
        System.out.println("B: " + b);
        System.out.println("C: " + c);
        System.out.println("D: " + d);
        System.out.println("E: " + e);
        System.out.println("F: " + f);
      }
    }
    It's still partially in English, but the structure is the same as any language, just follow the bread crums :P
    Last edited by Exception; 09-22-2009 at 05:30 PM.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  5. #5
    Dieter's Avatar
    Dieter is offline Member
    Join Date
    Sep 2009
    Posts
    91
    Rep Power
    0

    Default

    Quote Originally Posted by Exception View Post

    I am from norway btw, therefore I have a lot of the program written in norwegian.
    Weird language ;D
    Programming today is a race between software engineers striving to build bigger and better idiot proof programs,and the Universe trying to produce bigger and better idiots...

  6. #6
    Exception is offline Member
    Join Date
    Sep 2009
    Posts
    3
    Rep Power
    0

Similar Threads

  1. Using string to terminate loop
    By mrblippy in forum New To Java
    Replies: 3
    Last Post: 04-23-2009, 07:16 AM
  2. Entering string in a loop with method
    By Konex in forum New To Java
    Replies: 3
    Last Post: 03-01-2009, 08:33 AM
  3. [SOLVED] Ending for loop with a String
    By jdiesel in forum New To Java
    Replies: 3
    Last Post: 01-08-2009, 03:11 AM
  4. i need assistance with a string triggered loop please!
    By Phobos0001 in forum New To Java
    Replies: 9
    Last Post: 11-14-2007, 03:44 PM
  5. terminating a while loop with a string
    By tkdvipers in forum New To Java
    Replies: 3
    Last Post: 07-10-2007, 12:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •