提示コードの下部のmain関数の////コメント部のコードで以下の例外コードが発生するのですが何が原因なのでしょうか?
参考サイト: https://software.fujitsu.com/jp/manual/manualfiles/M070190/B1WN8861/01Z201/msg33/msg06166.htm
参考サイト: https://www.it-mure.jp.net/ja/java/javautilnosuchelementexception%EF%BC%9A%E8%A1%8C%E3%81%8C%E8%A6%8B%E3%81%A4%E3%81%8B%E3%82%8A%E3%81%BE%E3%81%9B%E3%82%93/940340246/
例外コード
[
Directory: Exception in thread "main" java.util.NoSuchElementException
at java.base/java.util.Scanner.throwFor(Scanner.java:937)
at java.base/java.util.Scanner.next(Scanner.java:1478)
at Main.main(Main.java:159)
]
java
1 2 3import java.io.IOException; 4import java.io.File; 5 6import javax.imageio.ImageIO; 7 8import java.awt.Color; 9import java.awt.Graphics; 10import java.awt.image.BufferedImage; 11 12import java.util.ArrayList; 13import java.util.Scanner; 14import java.util.List; 15 16import org.apache.pdfbox.pdmodel.PDDocument; 17import org.apache.pdfbox.pdmodel.PDPage; 18import org.apache.pdfbox.pdmodel.PDPageContentStream; 19import org.apache.pdfbox.pdmodel.common.PDRectangle; 20import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject; 21 22 23 24public class Main 25{ 26 27 //画像クラス 28 static class Image 29 { 30 //コンストラクタ 31 public Image(int w,int h,String n) 32 { 33 width = w; 34 height = h; 35 path = n; 36 } 37 38 39 public int width; //横 40 public int height; //縦 41 public String path; //パス 42 } 43 44 //ファイルの画像パスを取得 45 static public void GetDirectory(File dir,List<Image> list) 46 { 47 48 System.out.println("==================== ファイル読み込み ==================== "); 49 50 int i = 0; 51 for(; i < dir.listFiles().length; i++) 52 { 53 boolean w = false; 54 try 55 { 56 BufferedImage b = ImageIO.read(dir.listFiles()[i]); 57 Image img = new Image(b.getWidth(),b.getHeight(),dir.listFiles()[i].getPath()); 58 59 list.add(img); 60 61 b = null; 62 } 63 catch(NullPointerException e) 64 { 65 66 w = true; 67 System.out.println("###### Warning: 未対応のファイル形式です: " + dir.listFiles()[i].getPath() + " ######"); 68 69 } 70 catch (IOException e) 71 { 72 73 e.printStackTrace(); 74 } 75 76 if ( w == false) 77 { 78 System.out.println(list.get(list.size() - 1).path); 79 } 80 //System.out.println("###################################"); 81 //System.out.println("Width: " + list.get(list.size() -1).width); 82 //System.out.println("Height: " + list.get(list.size() -1).height); 83 //System.out.println("Path: " + list.get(list.size() -1).path); 84 //System.out.println("###################################\n"); 85 } 86 87 System.out.println("ページ数: " + i); 88 89 90 91 System.out.println(); 92 //System.out.println(); 93 } 94 95 96 //PDFファイルを生成 97 static void GeneratePDF(List<Image> list,List<String> pathList,File fileName) 98 { 99 try 100 { 101 PDDocument document = new PDDocument(); //ドキュメント 102 List<PDPage> page = new ArrayList<>(); //ページ 103 104 System.out.println("==================== ページ生成 ==================== "); //画面表示 105 106 //ページ生成 107 for(Image image : list) 108 { 109 PDRectangle rec = new PDRectangle(); 110 rec.setUpperRightX(0); 111 rec.setUpperRightY(0); 112 rec.setLowerLeftX(image.width); 113 rec.setLowerLeftY(image.height); 114 115 System.out.println(image.path + " --- size ---> ("+ image.width + " , " + image.height + ")"); 116 117 page.add(new PDPage(rec)); 118 document.addPage(page.get(page.size() -1)); 119 120 } 121 122 System.out.println("\n==================== 書き込み ===================="); 123 124 //画像焼き付け 125 for(int i = 0; i < list.size(); i++) 126 { 127 128 PDImageXObject xImage = PDImageXObject.createFromFile(list.get(i).path,document); 129 PDPageContentStream stream = new PDPageContentStream(document,page.get(i)); 130 stream.drawImage(xImage, 0,0); 131 132 System.out.println( "[成功]: "+ new File(list.get(i).path).getName()); 133 stream.close(); 134 } 135 136 System.out.println("==================== PDF生成中 ====================\n"); 137 document.save(fileName.getParent() + "\" + fileName.getName() + ".pdf"); 138 System.out.println("--->: " + fileName.getParent() + "\" + fileName.getName() + ".pdf"); 139 System.out.println("\n==================== 完了 ===================="); 140 141 document.close(); 142 143 } 144 catch(IOException e) 145 { 146 e.printStackTrace(); 147 } 148 } 149 150 151 public static void main(String args[]) 152 { 153 while(true) 154 { 155///////////////////////////////////////////////////////////////////////////// 156 Scanner scanner = new Scanner(System.in); 157///////////////////////////////////////////////////////////////////////////// 158 List<Image> imageList = new ArrayList<>(); 159 160 System.out.print("\nDirectory: "); 161 String fileName = scanner.next(); 162 163 File f = new File(fileName); 164 165 System.out.println(); 166 GetDirectory(f,imageList); //画像パス読み込み 167 168 169 List<String> fileList = new ArrayList<>(); 170 GeneratePDF(imageList,fileList,f); //PDFファイルを生成 171 172 173 174 175 scanner.close(); 176 177 } 178 } 179} 180
回答1件
あなたの回答
tips
プレビュー