1

I am a beginner-moderate java writer, and I have a problem that I just cannot understand, not even the cause.

I have some stuff in a class, a filewriter and -reader object, a scanner(system.in), and some strings, longs and ints. The idea is to easily write an .html document, and I nailed it already, but I created a for () loop around a bit of code containing scan.nextInt();, and it just skipped it when running in the console.

A tiny part of the code:

int amlinks = scan.nextImt();
for (int i = 0; i < amlinks; i++) {
  //refer file link1
  //link1, ln12
  System.out.print("[htmlbuilder.process]: text1 = ");
  String link1= new String(scan.nextLine());
  //refer text link1
  //text1, line12, ln12
  System.out.print("[htmlbuilder.process]: text2 = ");
  String text1 = new String(scan.nextLine());
  String link = new String("    <p><a href='" + link1 + ".html'>" + text1 + "</a>");
  writer.write(link);
}

Some handy facts:

The class is called htmlbuilder;

amlinks is just a variable for the for () loop;

link1 and text1 variables are the required Strings for the links;

link variable will eventually be written in the file,

writer:

Writer writer = null;
write = writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileID + ".html"), "utf-8"));

with fileID as the name of the file;

the //info before says what is inside the next piece of code.

I hope you understand what I am doing, I do know I have quite an own way of coding...

Thanks for help!

PS: Just ask some questions if you don't understand anything

  • 1
    Possibly related: [Scanner is skipping nextLine() after using next() or nextFoo()?](https://stackoverflow.com/q/13102045) – Pshemo May 23 '18 at 20:16
  • Also `scan.nextLine()` already returns `String` so what is the point of `new String(scan.nextLine())`? – Pshemo May 23 '18 at 20:17
  • 1
    "The class is called `htmlbuilder`" it is good practice to respect Java naming convention and start classes with uppercase. It makes your code easier to read when you know that `foo` is variable/field/method while `Foo` is name of type. So instead of `htmlbuilder` it should be `HtmlBuilder` – Pshemo May 23 '18 at 20:19
  • How exactly did the program behave when it 'skipped' the `Scanner.nextInt()`? It currently see no way why it should do it, but maybe the bug you are looking for is a different one that results in the same (or similar) behavior. – Izruo May 23 '18 at 20:21
  • @Pshemo I'm a beginner, remember – user-1561363397 May 23 '18 at 20:24
  • I wasn't trying to sound harsh so I hope you don't see my comments that way. Just trying to point out potential problems – Pshemo May 23 '18 at 20:26
  • @Izruo Same, I don't have a clue. The program just skipped it, the reaction was that is printed the first question, and without waiting for a variable to be filled in, it just printed the next line – user-1561363397 May 23 '18 at 20:26
  • Anyway to get proper help (instead of guesses) we would need to see [MCVE] (a.k.a. [SSCCE](http://sscce.org)) – Pshemo May 23 '18 at 20:27
  • Bytheway, I also tried putting the exact piece of code, just the for loop, in a test class, but this actually did not skip it, so I think this also might be an error from the compiler or java itself – user-1561363397 May 23 '18 at 20:28
  • While it is possible that it is some error in Java itself 99.9999% of the times (or more) it is problem with code, not language. So before thinking "this tool is wrong" it is waaaaaaay safer to start from thinking that "I am probably misusing something". – Pshemo May 23 '18 at 20:31
  • @Pshemo I'll send the complete code tomorrow... – user-1561363397 May 23 '18 at 20:31
  • No no no, please no. Take your time and create ***minimal*** (but still valid) example which you could actually post here so it could be helpful for others (this is main goal of this site and reason we are spending our time here). Remove all unnecessary stuff which is not related to the problem which you are asking. But from my experience beginners fall into a trap described in question from my first comment so take your time and try solution provided there. – Pshemo May 23 '18 at 20:35
  • `scan.nextImt()` should be `scan.nextInt()`. – Dorado May 23 '18 at 20:46

1 Answers1

0

when you both use nextInt() and nextLine() of the one,please add

  Object.nextLine();

Like that


somthing=scan.nextInt();
somthing=scan.nextInt();
...
scan.nextLine();
somthing=scan.nextLine();
somthing=scan.nextLine();
...

to make sure that your input items are read by the real next line, nextInt() methods means your cursor is still in the current line.