-2

Hey guys i am a recent mover from c so i don't have a deep knowledge about java. My problem is that when i run the console it seems do while is not working as the condition i tell it to but working opposite of the condition.for additional information i use eclipse as my IDE.

full code:

public class kahfi {

static int choice;
static String judul = null ;
static String judulterbit;
static String penulis;
static String jenisbuku;
static String genre;


static int halamannovel ;
static int halamancerpen ;
static int harganovel;
static int hargacerpen;
static int pendapatannovel;
static int pendapatancerpen;


public static void pilihan1(){

    Scanner scanmenu2 = new Scanner(System.in);

    System.out.println("Judul:");
    judul = scanmenu2.next();

    System.out.println("Penulis:[nama mohon disambungkan jika panjang dan terpisah]");
    penulis = scanmenu2.next();

    System.out.println("novel/cerpen? ");
    jenisbuku = scanmenu2.next();

    if(jenisbuku.equals("novel")){ 



     do{
        System.out.println( "jumlah halaman novel[100-300]  :" );
         halamannovel = scanmenu2.nextInt();

     }while(halamannovel >=  100 && halamannovel <= 300 );

    System.out.println("Pilih genre [fiksi,nonfiksi,humor ,dll]: ");
    genre = scanmenu2.next(); 
    System.out.println("data telah tersimpan");
    System.out.println("=========================");

  }else if(jenisbuku.equals("cerpen")){


     do{
        System.out.println("jumlah halaman cerpen[5-30]  :");  
         halamancerpen = scanmenu2.nextInt();

     }while(halamancerpen >= 5 && halamancerpen <= 30 );

    System.out.println("Pilih genre [fiksi,nonfiksi,humor ,dll]: ");
    genre = scanmenu2.next(); 
    System.out.println("data telah tersimpan");
    System.out.println("=========================");


   }else{
       System.out.println("=========================");
      System.out.println("salah input ");
     System.out.println("=========================");
   }

}

public static void pilihan2(){

    System.out.println("Buku yang belum terbit :");
    System.out.println(judul);
    System.out.println("=========================");

    harganovel = halamannovel * 500;
    hargacerpen = halamancerpen * 200;

    System.out.println("harga novel "+(harganovel));
    System.out.println("harga cerpen "+(hargacerpen));
    System.out.println("=========================");

}

public static void pilihan3(){

    judulterbit = new String(judul);

    System.out.println("=========================");
    System.out.println("uang yang di dapatkan : ");
    Random randomno = new Random();

    pendapatannovel = randomno.nextInt(100000) * halamannovel;
    pendapatancerpen = randomno.nextInt(100000) * halamancerpen;

    System.out.println("pendapatan novel : +" + pendapatannovel);
    System.out.println("pendapatan cerpen : +" + pendapatancerpen);
    System.out.println("=========================");

}

public static void pilihan4(){
    System.out.println("=========================");
    System.out.println("buku yang sudah terbit :\n"+(judulterbit));
    System.out.println("=========================");
}


public static void main(String[] args) {


    do{
    System.out.println("Menu\n====");
    System.out.println("1.Menulis buku \n2.Melihat daftar buku yang belum terbit\n3.Menerbitkan buku\n4.Melihat daftar buku yang sudah diterbitkan\n5.Exit.");
    System.out.println("pilihan anda : ");
    Scanner scanmenu = new Scanner(System.in);
    choice = scanmenu.nextInt();

    switch(choice) {


    case 1 :

        pilihan1();

       break;

    case 2 :

        pilihan2();

        break;

    case 3 :

        pilihan3();

       break;
    case 4 :

        pilihan4();

       break;
    case 5  :
        System.exit(0);
        break;

        }
    }while(choice!=5);

    }   

}

the problem seems to be in this side . Iam pretty sure the condition are right ,but when i input "85" in halamannovel it continues instead of repeating and when i input 100 it repeats . I tried to change the condition but it made it worse.It also applies to halamancerpen.

 do{
        System.out.println( "jumlah halaman novel[100-300]  :" );
         halamannovel = scanmenu2.nextInt();

     }while(halamannovel >=  100 && halamannovel <= 300 );

    System.out.println("Pilih genre [fiksi,nonfiksi,humor ,dll]: ");
    genre = scanmenu2.next(); 
    System.out.println("data telah tersimpan");
    System.out.println("=========================");

  }else if(jenisbuku.equals("cerpen")){


     do{
        System.out.println("jumlah halaman cerpen[5-30]  :");  
         halamancerpen = scanmenu2.nextInt();

     }while(halamancerpen >= 5 && halamancerpen <= 30 );

Any suggestion or solution would be a life saver. Thank you for your time!

FYI i tried in 3 different eclipse .

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
  • 6
    Please provide a minimal verifiable example. We don't need the whole code. http://stackoverflow.com/help/mcve – marstran Jan 12 '17 at 10:16
  • Hint: please read about creating a [mcve] and also understand that English is the primary language here; and that using non-english wording for identifiers and print statements simply makes it less likely for you to receive helpful feedback. Thus: instead of pushing your whole program, put up an [mcve] using English words only. Besides: there really is not much be much difference between such looping constructs between C and Java. – GhostCat Jan 12 '17 at 10:17
  • Eclipse is a text editor. It doesn't make any difference to how the code runs. – JJJ Jan 12 '17 at 10:22
  • hint: raher use a while statement than the do-while (it depends when the statement is evaluated...) .85 would be evaluate as "false" in your statements, so it is "normal" that is does not loop. – Loic Mouchard Jan 12 '17 at 10:24

2 Answers2

0

You are using 2 Scanner objects on the same System.in stream. Instead, you should reuse the existing scanner otherwise they will interfere with each other. See for instance the following: How to use multiple Scanner objects on System.in?

Community
  • 1
  • 1
jytou
  • 510
  • 4
  • 7
0

Its expected to continue when you enter 85.

In your exit criteria, you have given:

while(halamannovel >=  100 && halamannovel <= 300 );

This means that any number within 100(100 included) and 300(300 included) will make your loop repeat. The number range is 100-300.

If you enter 85, it is lesser than 100, and thus the while returns false and the loop breaks.

Hope it helps!

  • that what actually happend but i taught i was making condition which 100-300 can only be accepted and continue, while something below 100 and above 300 repeats the loop.Any suggestion on fixing the condition? – Kahfi Alghiffari Jan 12 '17 at 10:32
  • If you want number lesser than 100 and greater than 300 to repeat the loop, just change the while condition, like this: while(halamannovel <= 100 || halamannovel >= 300 ); – Sourav Purakayastha Jan 12 '17 at 10:37
  • hey appleman your answer was close to the solution the solution was while(halamannovel < 100 || halamannovel > 300 ); thanks so much bro! – Kahfi Alghiffari Jan 12 '17 at 10:46